mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

ignore unused variables that begin with an _ #40

Closed johnfoconnor closed 9 years ago

johnfoconnor commented 9 years ago

In Lua, "_" is only a convention with no inherent special meaning but it's commonly used to signify that we dont care about that variable

for _, data in pairs(db_entries) do
...

However, sometimes having naming that unused variable can provide helpful context. What are your thoughts on a flag which, when enables, ignores unused variables starting with a _. This is common practice in other languages

for _ref_id, data in pairs(db_entries) do
...
mpeterv commented 9 years ago

This rule can be applied as '--ignore 21/_.*' already, see #11. I'm not sure I want to add a flag as well. How would you name it if I did?

johnfoconnor commented 9 years ago

That's great that we can use this rule already.

that's a good question. I suppose one could unify all the unused options flags u r a s into a single parametrized flag.

ex

--no-unused="redefined+secondaries"

would ignore redefined and secondary variables warnings. We could add a keyword to signify variables with a leading _

mpeterv commented 9 years ago

The problem is because the rule is quite specific there could be variations of it, e.g. with _ as suffix or prefix, ignoring all matching variables or only arguments, etc. So I think adding an option expressing one variation is not enough.

The idea of combining --unused-* options is interesting but orthogonal to expressing the rule better, it'll require an option or a keyword either way. Perhaps we could overload the meaning of the argument differently, could it be a pattern matching variable name? E.g. '--no-unused _.*'. It could work for other options, too, and would make inline options nicer, e.g. 'function f(x, y) --luacheck: no unused arg x'.

johnfoconnor commented 9 years ago

thanks for the reply. I guess an abstraction is a bit premature. Feel free to close this issue as it is addressed via the following

--ignore 212/_.* --ignore 211/_.* --ignore 213/_.*
mpeterv commented 9 years ago

OK. By the way, --ignore 21/_.* should also work.