From a30745d31111ee304285ded971d5a3555b311a91 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sat, 13 Jan 2024 18:05:20 +0900 Subject: [PATCH] detect Template.stats in DiscardValue --- .../scala-2/fix/DiscardCatsEffectIOTest.scala | 2 ++ input/src/main/scala-2/fix/DiscardEffTest.scala | 2 ++ .../main/scala-2/fix/DiscardMonixTaskTest.scala | 2 ++ .../scala-2/fix/DiscardScalaFutureTest.scala | 2 ++ .../src/main/scala-2/fix/DiscardValueTest.scala | 2 ++ rules/src/main/scala/fix/DiscardValue.scala | 17 +++++++++++++++-- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala b/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala index 238732b..ae0fdcf 100644 --- a/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala +++ b/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala @@ -15,6 +15,8 @@ trait DiscardCatsEffectIOTest { def mock: DiscardCatsEffectIOTest + f0[Int] // assert: DiscardCatsEffectIO + def f2[R](implicit ec: ExecutionContext): IO[Int] = { f0[R] // assert: DiscardCatsEffectIO diff --git a/input/src/main/scala-2/fix/DiscardEffTest.scala b/input/src/main/scala-2/fix/DiscardEffTest.scala index 27fdcce..68554aa 100644 --- a/input/src/main/scala-2/fix/DiscardEffTest.scala +++ b/input/src/main/scala-2/fix/DiscardEffTest.scala @@ -14,6 +14,8 @@ trait DiscardEffTest { def mock: DiscardEffTest + f0[Int] // assert: DiscardEff + def f2[R]: Eff[R, Int] = { f0[R] // assert: DiscardEff diff --git a/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala b/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala index a01c29f..75ae0e6 100644 --- a/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala +++ b/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala @@ -14,6 +14,8 @@ trait DiscardMonixTaskTest { def mock: DiscardMonixTaskTest + f0[Int] // assert: DiscardMonixTask + def f2[R]: Task[Int] = { f0[R] // assert: DiscardMonixTask diff --git a/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala b/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala index 4d72b18..77ee1e9 100644 --- a/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala +++ b/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala @@ -15,6 +15,8 @@ trait DiscardScalaFutureTest { def mock: DiscardScalaFutureTest + f0[Int] // assert: DiscardScalaFuture + def f2[R](implicit ec: ExecutionContext): Future[Int] = { f0[R] // assert: DiscardScalaFuture diff --git a/input/src/main/scala-2/fix/DiscardValueTest.scala b/input/src/main/scala-2/fix/DiscardValueTest.scala index 69828f3..63a14ac 100644 --- a/input/src/main/scala-2/fix/DiscardValueTest.scala +++ b/input/src/main/scala-2/fix/DiscardValueTest.scala @@ -16,6 +16,8 @@ trait DiscardValueTest { def mock: DiscardValueTest + f0[Int] // assert: DiscardValue + def f2[R]: Option[Int] = { f0[R] // assert: DiscardValue diff --git a/rules/src/main/scala/fix/DiscardValue.scala b/rules/src/main/scala/fix/DiscardValue.scala index edb067f..6377036 100644 --- a/rules/src/main/scala/fix/DiscardValue.scala +++ b/rules/src/main/scala/fix/DiscardValue.scala @@ -3,8 +3,12 @@ package fix import metaconfig.ConfDecoder import metaconfig.Configured import metaconfig.generic.Surface +import scala.meta.Decl import scala.meta.Defn +import scala.meta.Stat +import scala.meta.Template import scala.meta.Term +import scala.meta.Tree import scala.meta.contrib.XtensionTreeOps import scalafix.Patch import scalafix.lint.Diagnostic @@ -52,6 +56,15 @@ class DiscardValue(config: DiscardValueConfig) extends SemanticRule("DiscardValu } object DiscardValue { + private object BlockOrTemplate { + def unapply(x: Tree): Option[List[Stat]] = PartialFunction.condOpt(x) { + case t: Term.Block => + t.stats + case t: Template => + t.stats + } + } + def typeRef( config: DiscardValueConfig )(implicit doc: SemanticDocument): Patch = { @@ -80,9 +93,9 @@ object DiscardValue { severity: LintSeverity, filter: SemanticType => Boolean )(implicit doc: SemanticDocument): Patch = { - doc.tree.collect { case Term.Block(values :+ _) => // ignore last + doc.tree.collect { case BlockOrTemplate(values :+ _) => // ignore last values.filter { - case _: Defn.Val | _: Defn.Def | _: Term.Assign | _: Defn.Var => + case _: Defn | _: Term.Assign | _: Decl => false case x => x.collectFirst {