-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AsInstanceOf and demoted ArraysEquals to Info
- Loading branch information
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |