Skip to content

Commit

Permalink
UnusedMethodParameter honors @unused annotation now (#440)
Browse files Browse the repository at this point in the history
* Scala 2.13-only for @unused

* Fixes #340. Excusing arguments annotated with @unused
  • Loading branch information
mccartney authored Oct 14, 2020
1 parent 3e64734 commit d7e937a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class UnusedMethodParameter
}
}

private def isParameterExcused(param: ValDef): Boolean =
param.symbol.annotations.exists(_.atp.toString == "scala.annotation.unused")

/**
* For constructor params, some params become vals / fields of the class:
* 1. all params in the first argument list for case classes
Expand Down Expand Up @@ -109,8 +112,9 @@ class UnusedMethodParameter
for {
vparams <- vparamss
vparam <- vparams
} if (!usesParameter(vparam.name.toString, rhs))
} if (!isParameterExcused(vparam) && !usesParameter(vparam.name.toString, rhs)) {
context.warn(tree.pos, self, s"Unused method parameter ($vparam).")
}
case _ => continue(tree)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sksamuel.scapegoat.inspections.unnecessary

import com.sksamuel.scapegoat.InspectionTest
import com.sksamuel.scapegoat.inspections.unneccesary.UnusedMethodParameter

class UnusedMethodParameterTest213 extends InspectionTest {

override val inspections = Seq(new UnusedMethodParameter)

"UnusedMethodParameter in Scala 2.13" - {
"should not report warning" - {
"for unused parameters if they are annotated with @unused (#340)" in {
val code =
"""| import scala.annotation.unused
| class Test {
| def foo(@unused a:String, b: Int): Unit = {
| println(b)
| }
| } """.stripMargin

compileCodeSnippet(code)
compiler.scapegoat.feedback.warnings.size shouldBe 0
compiler.scapegoat.feedback.warns.size shouldBe 0
}
}
}
}

0 comments on commit d7e937a

Please sign in to comment.