scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Compiler warning for unused method parameters, suggest wildcard insted #3952

Closed scabug closed 13 years ago

scabug commented 14 years ago

It would be nice if the compiler could give a warning when method parameters are not used and suggest to use wildcard as parameter name instead.

For example, looking at this method:

def foo(a:Int, b:Int, c:Int):Int = a + b

one can hardly tell if it is a bug or not. Writing it like below would make the intent explicit:

def foo(a:Int, b:Int, _:Int):Int = a + b

Cheers!

scabug commented 14 years ago

Imported From: https://issues.scala-lang.org/browse/SI-3952?orig=1 Reporter: @sbocq

scabug commented 14 years ago

@paulp said: There's the small matter that the underscore isn't legal syntax. I do have a fairly complete private spotting plugin which I'll finish one of these days. Going to call this a duplicate of #441.

scabug commented 14 years ago

@sbocq said: Replying to [comment:1 extempore]:

There's the small matter that the underscore isn't legal syntax. I do have a fairly complete private spotting plugin which I'll finish one of these days. Going to call this a duplicate of #441.

Couldn't this syntax become legal? A plugin will do the job for unused vars but not for unused method parameters because there won't be a way to remove the warning condition.

scabug commented 14 years ago

@paulp said: Replying to [comment:2 sbocq]:

Couldn't this syntax become legal? A plugin will do the job for unused vars but not for unused method parameters because there won't be a way to remove the warning condition.

It could become legal, but then you've far exceeded ticket scope since you're proposing a language change and that requires a SID. You're quite right that half the reason I haven't finished that plugin is that I don't have a good way to deal with the noise generated when they are intentionally unused. It probably ends up as an annotation, which isn't too bad in a lintlike plugin - it's kind of what annotations were designed for.

scabug commented 14 years ago

@sbocq said: Sounds reasonable otherwise I'd write a SID but my lack of knowledge of the compiler internals does not permit it. I'll be looking forward your plugin, thanks for taking that up!

som-snytt commented 6 years ago
$ scala -Ywarn-unused
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.

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

scala> def f(@deprecated x: Int) = 42
<console>:11: warning: @deprecated now takes two arguments; see the scaladoc.
       def f(@deprecated x: Int) = 42
              ^
f: (x: Int)Int

scala> def f(@deprecated("","") x: Int) = 42
f: (x: Int)Int