Skip to content

Commit 6394a39

Browse files
authored
Merge pull request #936 from Jacoby6000/feature/universal-inequality
Add universal inequality cases and tests
2 parents 2489b14 + 7bb08b2 commit 6394a39

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,16 @@ final class DisableSyntax(config: DisableSyntaxConfig)
225225
}
226226
case Term.ApplyInfix(_, t @ Term.Name("=="), _, _)
227227
if config.noUniversalEquality =>
228-
Seq(noUniversalEqualityDiagnostic(t))
228+
Seq(noUniversalEqualityDiagnostic("==", t))
229229
case Term.Apply(Term.Select(_, t @ Term.Name("==")), _)
230230
if config.noUniversalEquality =>
231-
Seq(noUniversalEqualityDiagnostic(t))
231+
Seq(noUniversalEqualityDiagnostic("==", t))
232+
case Term.ApplyInfix(_, t @ Term.Name("!="), _, _)
233+
if config.noUniversalEquality =>
234+
Seq(noUniversalEqualityDiagnostic("!=", t))
235+
case Term.Apply(Term.Select(_, t @ Term.Name("!=")), _)
236+
if config.noUniversalEquality =>
237+
Seq(noUniversalEqualityDiagnostic("!=", t))
232238
}
233239
val FinalizeMatcher = DisableSyntax.FinalizeMatcher("noFinalize")
234240
doc.tree.collect(DefaultMatcher.orElse(FinalizeMatcher)).flatten
@@ -251,8 +257,10 @@ final class DisableSyntax(config: DisableSyntaxConfig)
251257
explain = "Pattern matching in val assignment can result in match error, " +
252258
"use \"_ match { ... }\" with a fallback case instead.")
253259

254-
private def noUniversalEqualityDiagnostic(t: Term.Name): Diagnostic =
255-
Diagnostic("==", config.noUniversalEqualityMessage, t.pos)
260+
private def noUniversalEqualityDiagnostic(
261+
symbol: String,
262+
t: Term.Name): Diagnostic =
263+
Diagnostic(symbol, config.noUniversalEqualityMessage, t.pos)
256264

257265
}
258266

scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntaxConfig.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ case class DisableSyntaxConfig(
7777
)
7878
@ExampleValue("\"use === instead of ==\"")
7979
noUniversalEqualityMessage: String =
80-
"== is unsafe since it allows comparing two unrelated types",
80+
"== and != are unsafe since they allow comparing two unrelated types",
8181
@Description(
8282
"Report error if the text contents of a source file matches a given regex.")
8383
@ExampleValue(

scalafix-tests/input/src/main/scala/test/DisableSyntaxMoreRules.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DisableSyntax.noImplicitObject = true
88
DisableSyntax.noImplicitConversion = true
99
DisableSyntax.noUniversalEquality = true
1010
DisableSyntax.noUniversalEqualityMessage =
11-
"== is not typesafe, use === from cats.Eq instead"
11+
"== and != are not typesafe, use === and =!= from cats.Eq instead"
1212
*/
1313
package test
1414

@@ -91,12 +91,23 @@ Default args makes it hard to use methods as functions.
9191

9292
1 == 2 /* assert: DisableSyntax.==
9393
^^
94-
== is not typesafe, use === from cats.Eq instead
94+
== and != are not typesafe, use === and =!= from cats.Eq instead
9595
*/
9696

9797
1.==(2) /* assert: DisableSyntax.==
9898
^^
99-
== is not typesafe, use === from cats.Eq instead
99+
== and != are not typesafe, use === and =!= from cats.Eq instead
100100
*/
101101

102+
1 != 2 /* assert: DisableSyntax.!=
103+
^^
104+
== and != are not typesafe, use === and =!= from cats.Eq instead
105+
*/
106+
107+
1.!=(2) /* assert: DisableSyntax.!=
108+
^^
109+
== and != are not typesafe, use === and =!= from cats.Eq instead
110+
*/
111+
112+
102113
}

0 commit comments

Comments
 (0)