Skip to content

Feature Request: Do not diagnose unused-local for function parameters #3166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
b-stage opened this issue Apr 25, 2025 · 1 comment
Open

Comments

@b-stage
Copy link

b-stage commented Apr 25, 2025

This is a feature request for a new setting for LLS which would not diagnose unused-local for variables which are parameters of a function. Something like

"Lua.diagnostics.unusedLocalExcludeFuncParam": true

The reason I'd like this feature is because we have a Lua codebase that heavily relies on something similar to virtual functions, where there are many tables that have functions defined that share the same name and list of parameters, but some of these functions don't need to use all of the parameters. Think of something like this

function menu1:on_select_item(item, mouse_button) do_something_with_item(item) end
function menu2:on_select_item(item, mouse_button) do_something_with_mouse_button(mouse_button) end

Of course this could be changed to appeal to the current unused-local diagnostics by changing it to

function menu1:on_select_item(item) do_something_with_item(item) end
function menu2:on_select_item(_, mouse_button) do_something_with_mouse_button(mouse_button) end

but, besides doing the fixup being a lot of work, it makes maintenance of the code base worse because a programmer can't easily know anymore what those _ parmeters are supposed to be. Another alternative would be to add all known function parameter names to "Lua.diagnostics.unusedLocalExclude" (introduced via #619) but of course that also would be sub-optimal, because ignoring generic names like item could defeat the purpose of the diagnostic.

Besides function parameters, having an option to ignore for-loop variables would also be very neat. It's often easier there to substitute the name for _ but still, sometimes that loses important information on what the key/value of a for-loop over pairs(...) is. So something like

"Lua.diagnostics.unusedLocalExcludeForLoopVars": true

would also be very appreciated.

Thank you for your consideration, and of course for making LLS, it's awesome!

@tomlau10
Copy link
Contributor

(unrelated to the feature request)

Our team setup both LLS and luacheck for linting project files, and we use luacheck to check for unused locals.
https://github.com/lunarmodules/luacheck

It has more fine-grained control on which variables to check for unused.
https://luacheck.readthedocs.io/en/stable/warnings.html#unused-variables-2xx-and-values-3xx

For example here is the .luacheckrc that we use:

std = "lua53c"
quiet = 1
codes = true
cache = true
max_line_length = 150
unused_args = false
unused_secondaries = false
ignore = {
    "311",  -- value assigned but unused
    "312",  -- unused argument value
    "431",  -- shadowing upvalue
    "423",  -- shadowing loop variables
    "432",  -- shadowing upvalue argument
    "512",  -- loop executed at most once
    "542",  -- empty if branch
}

Then we kept the severity of LLS's unused-local as hint level only (for the grey out effect in vscode)


Hope this helps a bit before this feature is implemented is LLS 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants