SonarQube Java
  1. SonarQube Java
  2. SONARJAVA-224

Rule: Strings literals should be placed on the left side when checking for equality

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.4
    • Component/s: Rules
    • Labels:
      None
    • Number of attachments :
      0

      Activity

      Hide
      Dinesh Bolkensteyn added a comment -

      Done

      Show
      Dinesh Bolkensteyn added a comment - Done
      Hide
      Fabrice Bellingard added a comment -

      We get false positives on code like:

      if (StringUtils.equals("bar", myVar) {
        // ...
      }
      

      (same with StringUtils#equalsIgnoreCase)

      Also, these are 2 false-negatives:

      public final static String FOO = "";
      
      public void foo(String s) {
        if (s.equals(FOO)) {}
        if (s.equals("Bar" + FOO)) {}
      }
      
      Show
      Fabrice Bellingard added a comment - We get false positives on code like: if (StringUtils.equals( "bar" , myVar) { // ... } (same with StringUtils#equalsIgnoreCase) Also, these are 2 false-negatives: public final static String FOO = ""; public void foo( String s) { if (s.equals(FOO)) {} if (s.equals( "Bar" + FOO)) {} }
      Hide
      Evgeny Mandrikov added a comment -

      About false-negatives - I suppose that would be better to wait availability of symbol table and information about types of expressions in checks (SONARJAVA-167).

      Show
      Evgeny Mandrikov added a comment - About false-negatives - I suppose that would be better to wait availability of symbol table and information about types of expressions in checks ( SONARJAVA-167 ).
      Hide
      Dinesh Bolkensteyn added a comment -

      The false positive is fixed.

      IMO the false negative cases are even arguable.

      If you push this check very far, at some point it will become Findbugs's potential null pointer dereference one.

      Show
      Dinesh Bolkensteyn added a comment - The false positive is fixed. IMO the false negative cases are even arguable. If you push this check very far, at some point it will become Findbugs's potential null pointer dereference one.
      Hide
      Fabrice Bellingard added a comment -

      Tested.

      Show
      Fabrice Bellingard added a comment - Tested.
      Hide
      Freddy Mallet added a comment -

      @Dinesh, could you also make this rule deprecate PMD "Position Literals First In Comparisons" http://jira.sonarsource.com/browse/RSPEC-431

      Show
      Freddy Mallet added a comment - @Dinesh, could you also make this rule deprecate PMD "Position Literals First In Comparisons" http://jira.sonarsource.com/browse/RSPEC-431
      Hide
      Dinesh Bolkensteyn added a comment -

      Done

      Show
      Dinesh Bolkensteyn added a comment - Done
      Hide
      Freddy Mallet added a comment -

      Manually tested !

      Show
      Freddy Mallet added a comment - Manually tested !

        People

        • Assignee:
          Dinesh Bolkensteyn
          Reporter:
          Dinesh Bolkensteyn
        • Votes:
          0 Vote for this issue
          Watchers:
          4 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: