Kotlin / KEEP

Kotlin Evolution and Enhancement Process
Apache License 2.0
3.3k stars 356 forks source link

Variables prefixed with `unused` shouldn't trigger a "never used" warning #300

Closed kluever closed 2 years ago

kluever commented 2 years ago

Background: As part of our larger effort to expand the scope of CheckReturnValue, we'll be spraying var unused = around our codebase to capture the result of function calls to APIs that are marked with @CheckReturnValue.

In Java, ErrorProne has a checker that warns when a variable is declared but not used (very similar to kotlinc). However, the checker has an important carve out: it doesn't issue a warning for variables prefixed with unused.

This scenario also comes up if you're implementing an interface, and forced to implement a function but you don't need to use one of the function parameters. You can avoid the warning by simply naming the function parameter unusedFoo.

I propose updating kotlinc to avoid issuing such a warning if the variable name is prefixed with unused:

java/com/foo/bar/MessageExtensions.kt:11:7: warning: variable 'unused' is never used
kevinb9n commented 2 years ago

(As the person who added this to Error Prone) My rationale is: since the variable name shows clearly that the user is already aware, imho it makes sense to not warn.

Even better: renaming or prefixing with "unused" is a superior alternative to suppression, because if the variable does get used, something will look quite wrong until the variable gets renamed, which is just what you want.

There is the issue that some variables are intentionally named "unused*". I think it is a very small problem.

elizarov commented 2 years ago

Thanks. I've opened KT-52406 Variables prefixed with unused shouldn't trigger a "never used" warning. Let's move the discussion there. It is a pretty minor for a full-blown design discussion. We'll discuss it online with the team.