Skip to content

Files

Latest commit

 

History

History
65 lines (38 loc) · 1.39 KB

ModifiedControlVariable.md

File metadata and controls

65 lines (38 loc) · 1.39 KB

Pattern: Control variable is modified

Issue: -

Description

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.

Such loop would be suppressed:

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

Examples

To configure the check:

<module name="ModifiedControlVariable"/>

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
}

Further Reading