Skip to content

Commit

Permalink
fix for FinalLocalVariableCheck - FOR_EACH_CLAUSE variable is not rep…
Browse files Browse the repository at this point in the history
…orted. issue checkstyle#20
  • Loading branch information
Bhavik3 committed Feb 26, 2015
1 parent 8d5d931 commit 7f24a9f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public class FinalLocalVariableCheck extends Check
/** Scope Stack */
private final FastStack<Map<String, DetailAST>> scopeStack =
FastStack.newInstance();
/** validation of For_EACH_CLAUSE_varible */
private boolean validateEnhancedForLoopVariable;
/**
* set the validation of enhanced For_Variable.
* @param validateEnhancedForLoopVariable true/false.
*/
public final void setValidateEnhancedForLoopVariable(boolean validateEnhancedForLoopVariable)
{
this.validateEnhancedForLoopVariable = validateEnhancedForLoopVariable;
}

@Override
public int[] getDefaultTokens()
Expand Down Expand Up @@ -116,7 +126,7 @@ public void visitToken(DetailAST ast)
}
case TokenTypes.VARIABLE_DEF:
if ((ast.getParent().getType() != TokenTypes.OBJBLOCK)
&& (ast.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE)
&& weatherToCheckEnhancedForLoopVariable(ast)
&& isVariableInForInit(ast))
{
insertVariable(ast);
Expand Down Expand Up @@ -153,8 +163,18 @@ && isVariableInForInit(ast))
default:
}
}

/**
* degtermine weather to check for FOR_EACH_CLAUSE varible.
* @param ast The ast to compare.
* @return true if validation for enhanced for variable is enable.
*/
private boolean weatherToCheckEnhancedForLoopVariable(DetailAST ast)
{
return validateEnhancedForLoopVariable
|| ast.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE;
}

/**
* Checks if current variable is defined in
* {@link TokenTypes#FOR_INIT for-loop init}, e.g.:
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;

import org.junit.Test;

import static com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck.MSG_KEY;
Expand Down Expand Up @@ -96,4 +97,28 @@ public void testFalsePositive() throws Exception
};
verify(checkConfig, getPath("coding/InputFinalLocalVariableCheckFalsePositive.java"), expected);
}
@Test
public void testEnhancedForLoopVariableTrue() throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(FinalLocalVariableCheck.class);
checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");
checkConfig.addAttribute("validateEnhancedForLoopVariable", "true");
final String[] expected = {
"8:20: " + "Variable 'a' should be declared final.",
"15:13: " + "Variable 'x' should be declared final.",
};
verify(checkConfig, getPath("coding/InputFinalLocalVariableEnhancedForLoopVariable.java"), expected);
}
@Test
public void testEnhancedForLoopVariableFalse() throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(FinalLocalVariableCheck.class);
checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");
final String[] expected = {
"15:13: " + "Variable 'x' should be declared final.",
};
verify(checkConfig, getPath("coding/InputFinalLocalVariableEnhancedForLoopVariable.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.puppycrawl.tools.checkstyle.coding;

public class InputFinalLocalVariableEnhancedForLoopVariable {
public void method1()
{
final java.util.List<Object> list = new java.util.ArrayList<>();

for(Object a : list){
}
}

public void method2()
{
final int[] squares = {0, 1, 4, 9, 16, 25};
int x;
for (final int i : squares) {
}

}

}
13 changes: 13 additions & 0 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ number.equals(i + j);
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>
</td>
</tr>
<tr>
<td>validateEnhancedForLoopVariable</td>
<td>Controls weather to check Enhanced For Loop Variable.</td>
<td>
<a
href="property_types.html#boolean">Boolean</a>,
</td>
<td>
<code>
false
</code>
</td>
</tr>
</table>
</subsection>

Expand Down

0 comments on commit 7f24a9f

Please sign in to comment.