Menu

#1377 Add rule for comparing unrelated types

3.x
closed-rejected
None
5
2015-08-25
2015-04-15
Sodiaan
No

FindBugs could have a rule to detect the comparing unrelated types in Scala.

:::scala

val a : String = "s"

val b : Int = 42

if (a != b) println("bug")

Discussion

  • Tagir Valeev

    Tagir Valeev - 2015-08-25
    • status: open --> closed-rejected
    • assigned_to: Tagir Valeev
     
  • Tagir Valeev

    Tagir Valeev - 2015-08-25

    FindBugs is Java static analyzer, not Scala analyzer. Though you can feed FindBugs with scalac-generated bytecode, you will get the enormous number of false-positives and simply irrelevant reports, which makes it practically useless. Thus we are not going to fix something for the Scala code.

    In Java the equivalent code produces a compilation error, thus no need to check it with FindBugs:

    String a = "s";
    Integer b = 42;
    if(a != b) // error: incomparable types: String and Integer
        System.out.println("bug");
    

    If you use equals instead of !=, then FindBugs warns:

    Call to String.equals(Integer) in test.Unrelated.main(String[])

    Also FindBugs warns when you use the Object type:

    Object a = "s";
    Object b = 42;
    if(a != b)
        System.out.println("bug");
    

    FindBugs warning:

    Using pointer equality to compare a Integer with a String in test.Unrelated.main(String[])

    So everything already works for Java and there's nothing to fix.

     

Log in to post a comment.