Skip to content

Commit

Permalink
Added AsInstanceOf and demoted ArraysEquals to Info
Browse files Browse the repository at this point in the history
  • Loading branch information
t1b00 committed Sep 12, 2024
1 parent 76f044c commit 043f233
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
33 changes: 33 additions & 0 deletions input/src/main/scala/fix/AsInstanceOf.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}

}
3 changes: 2 additions & 1 deletion rules/src/main/resources/META-INF/services/scalafix.v1.Rule
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ fix.InterpolationToString
fix.EitherGet
fix.NullParameter
fix.ArraysEquals
fix.ArraysToString
fix.ArraysToString
fix.AsInstanceOf
2 changes: 1 addition & 1 deletion rules/src/main/scala/fix/ArraysEquals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
28 changes: 28 additions & 0 deletions rules/src/main/scala/fix/AsInstanceOf.scala
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 043f233

Please sign in to comment.