Skip to content

Commit

Permalink
add DiscardEff, DiscardMonixTask, DiscardScalaFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jan 11, 2024
1 parent 0018725 commit 36aca0c
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ lazy val inputOutputCommon = Def.settings(
}
},
libraryDependencies += "com.typesafe.slick" %% "slick" % "3.5.0-M5",
libraryDependencies += "io.monix" %% "monix-eval" % "3.4.1",
libraryDependencies += "org.mockito" % "mockito-subclass" % "5.8.0",
libraryDependencies += "org.atnos" %% "eff-core" % "7.0.1"
)

Expand Down
48 changes: 48 additions & 0 deletions input/src/main/scala-2/fix/DiscardEffTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
rule = DiscardEff
*/
package fix

import org.atnos.eff.Eff
import org.mockito.Mockito
import org.mockito.Mockito.verify

trait DiscardEffTest {
def f0[R]: Eff[R, Int]

def f1[R](n: Int): Eff[R, Int]

def mock: DiscardEffTest

def f2[R]: Eff[R, Int] = {
f0[R] // assert: DiscardEff

f1[R](2) // assert: DiscardEff

f1[R](3).map(_ + 4) // assert: DiscardEff

val x1 = f1[R](5)
def x2: Eff[R, Int] = f1[R](5)

Mockito.verify(mock).f1(3)
Mockito.verify(mock).f1[R](3)
Mockito.verify(mock, Mockito.times(2)).f1(9)
Mockito.verify(mock, Mockito.times(2)).f1[R](9)
Mockito.verify(mock, Mockito.never()).f1(-1)
Mockito.verify(mock, Mockito.never()).f1[R](-1)

verify(mock).f0[R]
verify(mock).f1(4)
verify(mock).f1[R](4)
verify(mock, Mockito.times(2)).f1(5)
verify(mock, Mockito.times(2)).f1[R](5)
verify(mock, Mockito.never()).f1(8)
verify(mock, Mockito.never()).f1[R](8)

x1.flatMap(y => x1.map(_ + y)) // assert: DiscardEff

x1 // assert: DiscardEff

x1
}
}
48 changes: 48 additions & 0 deletions input/src/main/scala-2/fix/DiscardMonixTaskTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
rule = DiscardMonixTask
*/
package fix

import monix.eval.Task
import org.mockito.Mockito
import org.mockito.Mockito.verify

trait DiscardMonixTaskTest {
def f0[R]: Task[Int]

def f1[R](n: Int): Task[Int]

def mock: DiscardMonixTaskTest

def f2[R]: Task[Int] = {
f0[R] // assert: DiscardMonixTask

f1[R](2) // assert: DiscardMonixTask

f1[R](3).map(_ + 4) // assert: DiscardMonixTask

val x1 = f1[R](5)
def x2: Task[Int] = f1[R](5)

Mockito.verify(mock).f1(3)
Mockito.verify(mock).f1[R](3)
Mockito.verify(mock, Mockito.times(2)).f1(9)
Mockito.verify(mock, Mockito.times(2)).f1[R](9)
Mockito.verify(mock, Mockito.never()).f1(-1)
Mockito.verify(mock, Mockito.never()).f1[R](-1)

verify(mock).f0[R]
verify(mock).f1(4)
verify(mock).f1[R](4)
verify(mock, Mockito.times(2)).f1(5)
verify(mock, Mockito.times(2)).f1[R](5)
verify(mock, Mockito.never()).f1(8)
verify(mock, Mockito.never()).f1[R](8)

x1.flatMap(y => x1.map(_ + y)) // assert: DiscardMonixTask

x1 // assert: DiscardMonixTask

x1
}
}
49 changes: 49 additions & 0 deletions input/src/main/scala-2/fix/DiscardScalaFutureTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
rule = DiscardScalaFuture
*/
package fix

import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import org.mockito.Mockito
import org.mockito.Mockito.verify

trait DiscardScalaFutureTest {
def f0[R]: Future[Int]

def f1[R](n: Int): Future[Int]

def mock: DiscardScalaFutureTest

def f2[R](implicit ec: ExecutionContext): Future[Int] = {
f0[R] // assert: DiscardScalaFuture

f1[R](2) // assert: DiscardScalaFuture

f1[R](3).map(_ + 4) // assert: DiscardScalaFuture

val x1 = f1[R](5)
def x2: Future[Int] = f1[R](5)

Mockito.verify(mock).f1(3)
Mockito.verify(mock).f1[R](3)
Mockito.verify(mock, Mockito.times(2)).f1(9)
Mockito.verify(mock, Mockito.times(2)).f1[R](9)
Mockito.verify(mock, Mockito.never()).f1(-1)
Mockito.verify(mock, Mockito.never()).f1[R](-1)

verify(mock).f0[R]
verify(mock).f1(4)
verify(mock).f1[R](4)
verify(mock, Mockito.times(2)).f1(5)
verify(mock, Mockito.times(2)).f1[R](5)
verify(mock, Mockito.never()).f1(8)
verify(mock, Mockito.never()).f1[R](8)

x1.flatMap(y => x1.map(_ + y)) // assert: DiscardScalaFuture

x1 // assert: DiscardScalaFuture

x1
}
}
50 changes: 50 additions & 0 deletions input/src/main/scala-2/fix/DiscardValueTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
rule = DiscardValue
DiscardValue.types = [
"scala/Option#"
]
*/
package fix

import org.mockito.Mockito
import org.mockito.Mockito.verify

trait DiscardValueTest {
def f0[R]: Option[Int]

def f1[R](n: Int): Option[Int]

def mock: DiscardValueTest

def f2[R]: Option[Int] = {
f0[R] // assert: DiscardValue

f1[R](2) // assert: DiscardValue

f1[R](3).map(_ + 4) // assert: DiscardValue

val x1 = f1[R](5)
def x2: Option[Int] = f1[R](5)

Mockito.verify(mock).f1(3)
Mockito.verify(mock).f1[R](3)
Mockito.verify(mock, Mockito.times(2)).f1(9)
Mockito.verify(mock, Mockito.times(2)).f1[R](9)
Mockito.verify(mock, Mockito.never()).f1(-1)
Mockito.verify(mock, Mockito.never()).f1[R](-1)

verify(mock).f0[R]
verify(mock).f1(4)
verify(mock).f1[R](4)
verify(mock, Mockito.times(2)).f1(5)
verify(mock, Mockito.times(2)).f1[R](5)
verify(mock, Mockito.never()).f1(8)
verify(mock, Mockito.never()).f1[R](8)

x1.flatMap(y => x1.map(_ + y)) // assert: DiscardValue

x1 // assert: DiscardValue

x1
}
}
43 changes: 43 additions & 0 deletions rules/src/main/scala/fix/DiscardEff.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fix

import scalafix.Patch
import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SemanticDocument
import scalafix.v1.SemanticRule
import metaconfig.ConfDecoder
import metaconfig.Configured
import metaconfig.generic.Surface
import scalafix.lint.LintSeverity

case class DiscardEffConfig(
severity: LintSeverity
)

object DiscardEffConfig {
val default: DiscardEffConfig = DiscardEffConfig(
severity = LintSeverity.Warning,
)

implicit val surface: Surface[DiscardEffConfig] =
metaconfig.generic.deriveSurface[DiscardEffConfig]

implicit val decoder: ConfDecoder[DiscardEffConfig] = {
import DiscardValueConfig.lintSeverityDecoderInstance
metaconfig.generic.deriveDecoder(default)
}
}

class DiscardEff(config: DiscardEffConfig) extends SemanticRule("DiscardEff") {

def this() = this(DiscardEffConfig.default)

override def withConfiguration(config: Configuration): Configured[Rule] = {
config.conf.getOrElse("DiscardEff")(this.config).map(newConfig => new DiscardEff(newConfig))
}

override def fix(implicit doc: SemanticDocument): Patch = DiscardValue.typeRef(
severity = config.severity,
types = Set("org/atnos/eff/Eff#")
)
}
42 changes: 42 additions & 0 deletions rules/src/main/scala/fix/DiscardMonixTask.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fix

import scalafix.Patch
import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SemanticDocument
import scalafix.v1.SemanticRule
import metaconfig.ConfDecoder
import metaconfig.Configured
import metaconfig.generic.Surface
import scalafix.lint.LintSeverity

case class DiscardMonixTaskConfig(
severity: LintSeverity
)

object DiscardMonixTaskConfig {
val default: DiscardMonixTaskConfig = DiscardMonixTaskConfig(
severity = LintSeverity.Warning,
)

implicit val surface: Surface[DiscardMonixTaskConfig] =
metaconfig.generic.deriveSurface[DiscardMonixTaskConfig]

implicit val decoder: ConfDecoder[DiscardMonixTaskConfig] = {
import DiscardValueConfig.lintSeverityDecoderInstance
metaconfig.generic.deriveDecoder(default)
}
}

class DiscardMonixTask(config: DiscardMonixTaskConfig) extends SemanticRule("DiscardMonixTask") {

def this() = this(DiscardMonixTaskConfig.default)
override def withConfiguration(config: Configuration): Configured[Rule] = {
config.conf.getOrElse("DiscardMonixTask")(this.config).map(newConfig => new DiscardMonixTask(newConfig))
}

override def fix(implicit doc: SemanticDocument): Patch = DiscardValue.typeRef(
severity = config.severity,
types = Set("monix/eval/Task#")
)
}
43 changes: 43 additions & 0 deletions rules/src/main/scala/fix/DiscardScalaFuture.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fix

import scalafix.Patch
import scalafix.v1.Configuration
import scalafix.v1.Rule
import scalafix.v1.SemanticDocument
import scalafix.v1.SemanticRule
import metaconfig.ConfDecoder
import metaconfig.Configured
import metaconfig.generic.Surface
import scalafix.lint.LintSeverity

case class DiscardScalaFutureConfig(
severity: LintSeverity
)

object DiscardScalaFutureConfig {
val default: DiscardScalaFutureConfig = DiscardScalaFutureConfig(
severity = LintSeverity.Warning,
)

implicit val surface: Surface[DiscardScalaFutureConfig] =
metaconfig.generic.deriveSurface[DiscardScalaFutureConfig]

implicit val decoder: ConfDecoder[DiscardScalaFutureConfig] = {
import DiscardValueConfig.lintSeverityDecoderInstance
metaconfig.generic.deriveDecoder(default)
}
}

class DiscardScalaFuture(config: DiscardScalaFutureConfig) extends SemanticRule("DiscardScalaFuture") {

def this() = this(DiscardScalaFutureConfig.default)

override def withConfiguration(config: Configuration): Configured[Rule] = {
config.conf.getOrElse("DiscardScalaFuture")(this.config).map(newConfig => new DiscardScalaFuture(newConfig))
}

override def fix(implicit doc: SemanticDocument): Patch = DiscardValue.typeRef(
severity = config.severity,
types = Set("scala/concurrent/Future#")
)
}
Loading

0 comments on commit 36aca0c

Please sign in to comment.