Skip to content

Commit

Permalink
update DiscardValue
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Mar 22, 2024
1 parent 98f97c6 commit 2801b35
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 37 deletions.
10 changes: 10 additions & 0 deletions input/src/main/scala-2/fix/DiscardCatsEffectIOTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
10 changes: 10 additions & 0 deletions input/src/main/scala-2/fix/DiscardEffTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions input/src/main/scala-2/fix/DiscardMonixTaskTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions input/src/main/scala-2/fix/DiscardScalaFutureTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 69 additions & 37 deletions rules/src/main/scala/fix/DiscardValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 2801b35

Please sign in to comment.