SonarQube Java
  1. SonarQube Java
  2. SONARJAVA-175

Rule: don't increment counters in for loops

    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: Squid
    • Labels:
      None
    • Number of attachments :
      0

      Issue Links

        Activity

        Hide
        Dinesh Bolkensteyn added a comment -

        Done

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

        The following case is not detected:

            for (int j = 0; j < 5; j++) {
              j++;
            }
        
        Show
        Fabrice Bellingard added a comment - The following case is not detected: for ( int j = 0; j < 5; j++) { j++; }
        Hide
        Dinesh Bolkensteyn added a comment -

        That case is now also covered!

        Show
        Dinesh Bolkensteyn added a comment - That case is now also covered!
        Hide
        Dinesh Bolkensteyn added a comment -

        False positive on:

          private static Node locateAttrParent(Element elem, Node attr)
          {
        
            Node parent = null;
        
                // This should only be called for Level 1 DOMs, so we don't have to
                // worry about namespace issues. In later levels, it's possible
                // for a DOM to have two Attrs with the same NodeName but
                // different namespaces, and we'd need to get getAttributeNodeNS...
                // but later levels also have Attr.getOwnerElement.
                Attr check=elem.getAttributeNode(attr.getNodeName());
                if(check==attr)
                        parent = elem;
        
            if (null == parent)
            {
              for (Node node = elem.getFirstChild(); null != node;
                      node = node.getNextSibling())
              {
                if (Node.ELEMENT_NODE == node.getNodeType())
                {
                  parent = locateAttrParent((Element) node, attr); // Non-Compliant
        
                  if (null != parent)
                    break;
                }
              }
            }
        
            return parent;
          }
        
        Show
        Dinesh Bolkensteyn added a comment - False positive on: private static Node locateAttrParent(Element elem, Node attr) { Node parent = null ; // This should only be called for Level 1 DOMs, so we don't have to // worry about namespace issues. In later levels, it's possible // for a DOM to have two Attrs with the same NodeName but // different namespaces, and we'd need to get getAttributeNodeNS... // but later levels also have Attr.getOwnerElement. Attr check=elem.getAttributeNode(attr.getNodeName()); if (check==attr) parent = elem; if ( null == parent) { for (Node node = elem.getFirstChild(); null != node; node = node.getNextSibling()) { if (Node.ELEMENT_NODE == node.getNodeType()) { parent = locateAttrParent((Element) node, attr); // Non-Compliant if ( null != parent) break ; } } } return parent; }
        Hide
        Dinesh Bolkensteyn added a comment -

        That should now be fixed.

        Show
        Dinesh Bolkensteyn added a comment - That should now be fixed.
        Hide
        Fabrice Bellingard added a comment -

        Tested!

        Show
        Fabrice Bellingard added a comment - Tested!
        Hide
        Freddy Mallet added a comment - - edited

        @Dinesh, I'm reopening this ticket as this rule also replaces PMD "Jumbled Incrementer": http://jira.sonarsource.com/browse/RSPEC-392 and Checkstyle "Modified Control Variable" http://jira.sonarsource.com/browse/RSPEC-213.

        Could you just double check that this is the case in term of implementation ?

        Example:

        public class JumbledIncrementerRule1 {
          public void foo() {
            for (int i = 0; i < 10; i++) {		
              for (int k = 0; k < 20; i++) {		//Non-Compliant
                System.out.println("Hello");
              }
            }
          }
        }
        
        Show
        Freddy Mallet added a comment - - edited @Dinesh, I'm reopening this ticket as this rule also replaces PMD "Jumbled Incrementer": http://jira.sonarsource.com/browse/RSPEC-392 and Checkstyle "Modified Control Variable" http://jira.sonarsource.com/browse/RSPEC-213 . Could you just double check that this is the case in term of implementation ? Example: public class JumbledIncrementerRule1 { public void foo() { for ( int i = 0; i < 10; i++) { for ( int k = 0; k < 20; i++) { //Non-Compliant System .out.println( "Hello" ); } } } }
        Hide
        Dinesh Bolkensteyn added a comment -

        Done, that case was already covered.

        Show
        Dinesh Bolkensteyn added a comment - Done, that case was already covered.
        Hide
        Freddy Mallet added a comment -

        Manually tested !

        Show
        Freddy Mallet added a comment - Manually tested !

          People

          • Assignee:
            Dinesh Bolkensteyn
            Reporter:
            Fabrice Bellingard
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: