Skip to content

WUnused: Ignore unused params of non-private methods that are members of non-final classlikes #17185

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

Closed
szymon-rd opened this issue Mar 30, 2023 · 4 comments · Fixed by #17223
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Milestone

Comments

@szymon-rd
Copy link
Contributor

Compiler version

3.3.0-RC2

The bug

We should not report unused params in methods that may be overridden, as it's often for the sake of the implementers to use these arguments.

@szymon-rd szymon-rd added itype:bug area:linting Linting warnings enabled with -W or -Xlint labels Mar 30, 2023
@szymon-rd szymon-rd self-assigned this Mar 30, 2023
@odersky
Copy link
Contributor

odersky commented Mar 30, 2023

It's worse than that.

trait A:
  def f(x: Int): Int
final class B extends A:
  def f(x: Int) = 22
final class C extends A:
  def f(x: Int) = x

f in B cannot be overridden, but the parameter should still not be flagged. So the best one can do is to flag unused parameters in local methods or in private methods or in final methods that do not override or implement anything else. At this point it becomes dubious whether the warning makes sense at all, IMO.

@som-snytt
Copy link
Contributor

➜  ~ scala -Wunused
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.

scala> def f(x: Int) = 42
def f(x: Int): Int

scala> def f(x: Int) = println(42)
             ^
       warning: parameter value x in method f is never used
def f(x: Int): Unit

Scala 2 tries to guess whether the RHS is trivial, as a gauge of whether it qualifies as a serious implementation or a placeholder or a stub. There is an open ticket as to whether final is a useful factor; sealed is not considered, where you might say a parameter is used somewhere at least.

I've proposed a -Xlint:non-strict to enable these heuristics to reduce warnings; normal -Xlint would enable reduced warnings by default, but it could be turned off for "strict mode", as some people prefer (or profess to prefer).

Otherwise, as was originally judged when the feature was proposed, a lint for unused parameters is uselessly noisy. I think that was Jason's comment, if it needed to be articulated.

Ideally, just in those cases where it looks like a param is not used to calculate a result, the user must add @unused to communicate that fact. (They are communicating with the reader, not only the compiler.)

@odersky
Copy link
Contributor

odersky commented Mar 31, 2023

I think we should follow Jason and accept that some warnings simply don't make sense.

@szymon-rd
Copy link
Contributor Author

For now (3.3.0 release), I think we should proceed with not reporting the unused parameters for non-private defs at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants