Skip to content

Commit

Permalink
Reduce false positives on comparing unrelated types (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnnei authored Nov 28, 2022
1 parent 7407ec6 commit ad598e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class ComparingUnrelatedTypes
if integralLiteralFitsInType(lit, value.tpe) =>
case Apply(Select(lit @ Literal(_), TermName("$eq$eq" | "$bang$eq")), List(value))
if integralLiteralFitsInType(lit, value.tpe) =>
// Comparing number types like BigDecimal to integer types causes boxing to promote
// the integer to Number to do the comparison.
case Apply(Select(lhs, TermName("$eq$eq" | "$bang$eq")), List(rhs))
if (lhs.tpe <:< typeOf[Number] && rhs.tpe.typeSymbol.isNumericValueClass) ||
(rhs.tpe <:< typeOf[Number] && lhs.tpe.typeSymbol.isNumericValueClass) =>
// -- End special cases ------------------------------------------------------------------

case Apply(Select(lhs, op @ TermName("$eq$eq" | "$bang$eq")), List(rhs)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class ComparingUnrelatedTypesTest extends InspectionTest {
"for float" - {
"compared to zero" in verifyNoWarnings("""object A { val f = 100f; val b = f == 0 }""")
}

"for numbers types comparing to number literals" - {
"number left hand side" in verifyNoWarnings("""object A { val a = BigDecimal(5); val b = a == 0 } """)
"number right hand side" in verifyNoWarnings(
"""object A { val a = BigDecimal(5); val b = 0 != a } """
)
}

"for same enum values" in {
val code = """object Main {
def main(args: Array[String]): Unit = {
Expand Down

0 comments on commit ad598e4

Please sign in to comment.