Uploaded image for project: 'Java'
  1. Java
  2. SONARJAVA-224

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

    Details

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

      Activity

      Hide
      dinesh.bolkensteyn Dinesh Bolkensteyn added a comment -

      Done

      Show
      dinesh.bolkensteyn Dinesh Bolkensteyn added a comment - Done
      Hide
      fabemn OLD - 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
      fabemn OLD - 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
      godin OLD - 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
      godin OLD - 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 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 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
      fabemn OLD - Fabrice Bellingard added a comment -

      Tested.

      Show
      fabemn OLD - Fabrice Bellingard added a comment - Tested.
      Hide
      freddy.mallet 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 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 Dinesh Bolkensteyn added a comment -

      Done

      Show
      dinesh.bolkensteyn Dinesh Bolkensteyn added a comment - Done
      Hide
      freddy.mallet Freddy Mallet added a comment -

      Manually tested !

      Show
      freddy.mallet Freddy Mallet added a comment - Manually tested !

        People

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

          Dates

          • Created:
            Updated:
            Resolved: