Class ModifiedControlVariableCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public final class ModifiedControlVariableCheck
    extends AbstractCheck
    Check for ensuring that for loop control variables are not modified inside the for block. An example is:
     
     for (int i = 0; i < 1; i++) {
         i++;//violation
     }
     
     
    Rationale: If the control variable is modified inside the loop body, the program flow becomes more difficult to follow.
    See FOR statement specification for more details.

    Examples:

     <module name="ModifiedControlVariable">
     </module>
     

    Such loop would be suppressed:

     
     for(int i=0; i &lt; 10;) {
         i++;
     }
     
     

    By default, This Check validates Enhanced For-Loop.

    Option 'skipEnhancedForLoopVariable' could be used to skip check of variable from Enhanced For Loop.

    An example of how to configure the check so that it skips enhanced For Loop Variable is:

     <module name="ModifiedControlVariable">
         <property name="skipEnhancedForLoopVariable" value="true"/>
     </module>
     

    Example:

     
     for (String line: lines) {
         line = line.trim();   // it will skip this violation
     }
     
     
    Author:
    Daniel Grenner, liscju