Skip to content

Commit

Permalink
Added ArraysEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
t1b00 committed Sep 12, 2024
1 parent 0c21c59 commit 76f044c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
23 changes: 23 additions & 0 deletions input/src/main/scala/fix/ArraysToString.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
rule = ArraysToString
*/
package fix

object ArraysToString {
def test(): Unit = {
Array(5).toString // assert: ArraysToString
Array(5).toString() // assert: ArraysToString

val array = Array(1, 2, 3, 4)
array.toString // assert: ArraysToString
array.toString() // assert: ArraysToString

List(5).toString // scalafix: ok;
List(5).toString() // scalafix: ok;

val l = List(1, 2, 3, 4)
l.toString // scalafix: ok;
l.toString() // scalafix: ok;
}

}
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 @@ -50,4 +50,5 @@ fix.WhileTrue
fix.InterpolationToString
fix.EitherGet
fix.NullParameter
fix.ArraysEquals
fix.ArraysEquals
fix.ArraysToString
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 @@ -23,7 +23,7 @@ class ArraysEquals extends SemanticRule("ArraysEquals") {
def isArray(elem: Stat) = Util.matchType(elem, "scala/Array")

doc.tree.collect {
// Corresponds to a == b or a != b
// Corresponds to a == b or a != b or Array(1,2,3) == Array(2,3,4) or Array(1,2,3) != Array(2,3,4)
case t @ Term.ApplyInfix.After_4_6_0(lhs, Term.Name("==" | "!="), _, Term.ArgClause(List(rhs), _)) if isArray(lhs) && isArray(rhs) => Patch.lint(diag(t.pos))
case _ => Patch.empty
}.asPatch
Expand Down
16 changes: 6 additions & 10 deletions rules/src/main/scala/fix/ArraysToString.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
rule = ArraysEquals
rule = ArraysToString
*/
package fix

Expand All @@ -8,24 +8,20 @@ import scalafix.v1._

import scala.meta._

class ArraysToString extends SemanticRule("ArraysEquals") {
class ArraysToString extends SemanticRule("ArraysToString") {

private def diag(pos: Position) = Diagnostic(
"",
"Checks for comparison of arrays using == which will always return false.",
"Checks for explicit toString calls on arrays",
pos,
"Array equals is not an equality check. Use sameElements or convert to another collection type.",
"Calling toString on an array does not perform a deep toString.",
LintSeverity.Warning
)

override def fix(implicit doc: SemanticDocument): Patch = {

def isArray(elem: Stat) = Util.matchType(elem, "scala/Array")

doc.tree.collect {
// Corresponds to a == b or a != b
case t @ Term.ApplyInfix.After_4_6_0(lhs, Term.Name("==" | "!="), _, Term.ArgClause(List(rhs), _)) if isArray(lhs) && isArray(rhs) => Patch.lint(diag(t.pos))
case _ => Patch.empty
// Corresponds to a.toString or a.toString() or Array(1, 2, 3).toString or Array(1, 2, 3).toString()
case t @ Term.Select(qual, Term.Name("toString")) if Util.matchType(qual, "scala/Array") => Patch.lint(diag(t.pos))
}.asPatch
}

Expand Down

0 comments on commit 76f044c

Please sign in to comment.