diff --git a/input/src/main/scala/fix/AsInstanceOf.scala b/input/src/main/scala/fix/AsInstanceOf.scala new file mode 100644 index 0000000..ba6eee3 --- /dev/null +++ b/input/src/main/scala/fix/AsInstanceOf.scala @@ -0,0 +1,33 @@ +/* +rule = AsInstanceOf + */ +package fix + +object AsInstanceOf { + // In the original Scapegoat test code, there are tests with SuppressWarning annotations. + // We chose to not include these as the same could be done for every rule. If a user wants to suppress a rule, they can simply remove it from the configuration. + def test(): Unit = { + val s: Any = "sammy" + println(s.asInstanceOf[String]) // assert: AsInstanceOf + + case class MappingCharFilter(name: String, mappings: (String, String)*) // scalafix: ok; + + val pf: PartialFunction[Any, Unit] = { + case s: String => println(s) // scalafix: ok; + case i: Int if i == 4 => println(i) // scalafix: ok; + case _ => println("no match :(") // scalafix: ok; + } + + sealed trait MyGADT[T] + final case class VariantInt(value: Int) extends MyGADT[Int] + final case class VariantString(value: String) extends MyGADT[String] + + def doStuff[T](gadt: MyGADT[T]): T = { // scalafix: ok; + gadt match { + case VariantInt(value) => value + case VariantString(value) => value + } + } + } + +} diff --git a/rules/src/main/resources/META-INF/services/scalafix.v1.Rule b/rules/src/main/resources/META-INF/services/scalafix.v1.Rule index 59b9740..1e4a2e7 100644 --- a/rules/src/main/resources/META-INF/services/scalafix.v1.Rule +++ b/rules/src/main/resources/META-INF/services/scalafix.v1.Rule @@ -51,4 +51,5 @@ fix.InterpolationToString fix.EitherGet fix.NullParameter fix.ArraysEquals -fix.ArraysToString \ No newline at end of file +fix.ArraysToString +fix.AsInstanceOf \ No newline at end of file diff --git a/rules/src/main/scala/fix/ArraysEquals.scala b/rules/src/main/scala/fix/ArraysEquals.scala index 2ece7c3..1c5c6b4 100644 --- a/rules/src/main/scala/fix/ArraysEquals.scala +++ b/rules/src/main/scala/fix/ArraysEquals.scala @@ -15,7 +15,7 @@ class ArraysEquals extends SemanticRule("ArraysEquals") { "Checks for comparison of arrays using == which will always return false.", pos, "Array equals is not an equality check. Use sameElements or convert to another collection type.", - LintSeverity.Warning + LintSeverity.Info ) override def fix(implicit doc: SemanticDocument): Patch = { diff --git a/rules/src/main/scala/fix/AsInstanceOf.scala b/rules/src/main/scala/fix/AsInstanceOf.scala new file mode 100644 index 0000000..edac025 --- /dev/null +++ b/rules/src/main/scala/fix/AsInstanceOf.scala @@ -0,0 +1,28 @@ +/* +rule = AsInstanceOf + */ +package fix + +import scalafix.lint.LintSeverity +import scalafix.v1._ + +import scala.meta._ + +class AsInstanceOf extends SemanticRule("AsInstanceOf") { + + private def diag(pos: Position) = Diagnostic( + "", + "Checks for use of asInstanceOf.", + pos, + "Use of asInstanceOf is considered a bad practice - consider using pattern matching instead.", + LintSeverity.Warning + ) + + override def fix(implicit doc: SemanticDocument): Patch = { + doc.tree.collect { + // Corresponds to a.asInstanceOf(...) or a.asInstanceOf[...] + case t @ Term.Select(_, Term.Name("asInstanceOf")) => Patch.lint(diag(t.pos)) + }.asPatch + } + +}