From 2801b3589812b520eb76c8213ef4b180ee8d55b6 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Fri, 22 Mar 2024 17:55:31 +0900 Subject: [PATCH] update DiscardValue --- .../scala-2/fix/DiscardCatsEffectIOTest.scala | 10 ++ .../src/main/scala-2/fix/DiscardEffTest.scala | 10 ++ .../scala-2/fix/DiscardMonixTaskTest.scala | 10 ++ .../scala-2/fix/DiscardScalaFutureTest.scala | 10 ++ rules/src/main/scala/fix/DiscardValue.scala | 106 ++++++++++++------ 5 files changed, 109 insertions(+), 37 deletions(-) diff --git a/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala b/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala index ae0fdcf..d5c1331 100644 --- a/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala +++ b/input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala @@ -15,6 +15,16 @@ trait DiscardCatsEffectIOTest { def mock: DiscardCatsEffectIOTest + def g[A](x: Option[IO[A]]): Option[Int] = { + x.map(y => 3) // assert: DiscardCatsEffectIO + x.map(_ => 3) // TODO + x.map(implicit y => 3) + x.map { y => + println(y) + 4 + } + } + f0[Int] // assert: DiscardCatsEffectIO def f2[R](implicit ec: ExecutionContext): IO[Int] = { diff --git a/input/src/main/scala-2/fix/DiscardEffTest.scala b/input/src/main/scala-2/fix/DiscardEffTest.scala index 68554aa..a75add6 100644 --- a/input/src/main/scala-2/fix/DiscardEffTest.scala +++ b/input/src/main/scala-2/fix/DiscardEffTest.scala @@ -12,6 +12,16 @@ trait DiscardEffTest { def f1[R](n: Int): Eff[R, Int] + def g[R, A](x: Option[Eff[R, A]]): Option[Int] = { + x.map(y => 3) // assert: DiscardEff + x.map(_ => 3) // TODO + x.map(implicit y => 3) + x.map { y => + println(y) + 4 + } + } + def mock: DiscardEffTest f0[Int] // assert: DiscardEff diff --git a/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala b/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala index 75ae0e6..cb7e926 100644 --- a/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala +++ b/input/src/main/scala-2/fix/DiscardMonixTaskTest.scala @@ -12,6 +12,16 @@ trait DiscardMonixTaskTest { def f1[R](n: Int): Task[Int] + def g[A](x: Option[Task[A]]): Option[Int] = { + x.map(y => 3) // assert: DiscardMonixTask + x.map(_ => 3) // TODO + x.map(implicit y => 3) + x.map { y => + println(y) + 4 + } + } + def mock: DiscardMonixTaskTest f0[Int] // assert: DiscardMonixTask diff --git a/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala b/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala index 77ee1e9..f026b36 100644 --- a/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala +++ b/input/src/main/scala-2/fix/DiscardScalaFutureTest.scala @@ -13,6 +13,16 @@ trait DiscardScalaFutureTest { def f1[R](n: Int): Future[Int] + def g[A](x: Option[Future[A]]): Option[Int] = { + x.map(y => 3) // assert: DiscardScalaFuture + x.map(_ => 3) // TODO + x.map(implicit y => 3) + x.map { y => + println(y) + 4 + } + } + def mock: DiscardScalaFutureTest f0[Int] // assert: DiscardScalaFuture diff --git a/rules/src/main/scala/fix/DiscardValue.scala b/rules/src/main/scala/fix/DiscardValue.scala index 3f9f911..ab9833a 100644 --- a/rules/src/main/scala/fix/DiscardValue.scala +++ b/rules/src/main/scala/fix/DiscardValue.scala @@ -118,45 +118,77 @@ object DiscardValue { severity: LintSeverity, filter: SemanticType => Boolean )(implicit doc: SemanticDocument): Patch = { - doc.tree.collect { case BlockOrTemplate(values :+ _) => // ignore last - values.filter { - case _: Defn | _: Term.Assign | _: Decl => - false - case x => - x.collectFirst { - case Term.Apply.After_4_6_0(Term.Select(Mockito(), _), _) => - () - case Term.Apply.After_4_6_0(MockitoVerify(), _) => - () - case Term.Apply.After_4_6_0( - Term.Select(MockitoInOrder(), Term.Name("verify")), - _ - ) => - () - }.isEmpty - }.flatMap(x => x.symbol.info.map(x -> _)) - .map { case (x, info) => - PartialFunction - .condOpt(info.signature) { - case m: MethodSignature => - m.returnType - case v: ValueSignature => + doc.tree.collect { + case BlockOrTemplate(values :+ _) => // ignore last + values.filter { + case _: Defn | _: Term.Assign | _: Decl => + false + case x => + x.collectFirst { + case Term.Apply.After_4_6_0(Term.Select(Mockito(), _), _) => + () + case Term.Apply.After_4_6_0(MockitoVerify(), _) => + () + case Term.Apply.After_4_6_0( + Term.Select(MockitoInOrder(), Term.Name("verify")), + _ + ) => + () + }.isEmpty + }.flatMap(x => x.symbol.info.map(x -> _)) + .map { case (x, info) => + PartialFunction + .condOpt(info.signature) { + case m: MethodSignature => + m.returnType + case v: ValueSignature => + v.tpe + } + .filter(filter) + .map { tpe => + Patch.lint( + Diagnostic( + id = "", + message = message(tpe), + position = x.pos, + severity = severity, + ) + ) + } + .asPatch + } + .asPatch + case Term.Function.After_4_6_0( + Term.ParamClause( + params, + None + ), + body + ) => + params.collect { + case p + if p.mods.isEmpty && + body.collect { case t: Term.Name => + t.value == p.name.value + }.isEmpty => + p.name.symbol.info + .map(_.signature) + .collect { case v: ValueSignature => v.tpe - } - .filter(filter) - .map { tpe => - Patch.lint( - Diagnostic( - id = "", - message = message(tpe), - position = x.pos, - severity = severity, + } + .filter(filter) + .map { tpe => + Patch.lint( + Diagnostic( + id = "", + message = message(tpe), + position = p.pos, + severity = severity, + ) ) - ) - } - .asPatch - } - .asPatch + } + .asPatch + }.asPatch }.asPatch } }