Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce false positives on comparing unrelated types #710

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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