From d7e937af41fc902d49a3b1ae4ef9e4c6d1a8f42a Mon Sep 17 00:00:00 2001 From: Greg Oledzki Date: Wed, 14 Oct 2020 22:41:29 +0200 Subject: [PATCH] UnusedMethodParameter honors @unused annotation now (#440) * Scala 2.13-only for @unused * Fixes #340. Excusing arguments annotated with @unused --- .../unneccesary/UnusedMethodParameter.scala | 6 ++++- .../UnusedMethodParameterTest213.scala | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/scala-2.13/com/sksamuel/scapegoat/inspections/unnecessary/UnusedMethodParameterTest213.scala diff --git a/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnusedMethodParameter.scala b/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnusedMethodParameter.scala index 12f8d366..74fad3ed 100644 --- a/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnusedMethodParameter.scala +++ b/src/main/scala/com/sksamuel/scapegoat/inspections/unneccesary/UnusedMethodParameter.scala @@ -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 @@ -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) } } diff --git a/src/test/scala-2.13/com/sksamuel/scapegoat/inspections/unnecessary/UnusedMethodParameterTest213.scala b/src/test/scala-2.13/com/sksamuel/scapegoat/inspections/unnecessary/UnusedMethodParameterTest213.scala new file mode 100644 index 00000000..cf4da348 --- /dev/null +++ b/src/test/scala-2.13/com/sksamuel/scapegoat/inspections/unnecessary/UnusedMethodParameterTest213.scala @@ -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 + } + } + } +}