Skip to content

Commit

Permalink
Merge f565eb2 into 204c073
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkravin committed Jan 26, 2015
2 parents 204c073 + f565eb2 commit 8f62c9f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Expand Up @@ -109,7 +109,8 @@ public void visitToken(DetailAST ast)
}
case TokenTypes.VARIABLE_DEF:
if ((ast.getParent().getType() != TokenTypes.OBJBLOCK)
&& (ast.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE))
&& (ast.getParent().getType() != TokenTypes.FOR_EACH_CLAUSE)
&& isFirstVariableInForInit(ast))
{
insertVariable(ast);
}
Expand Down Expand Up @@ -146,6 +147,25 @@ public void visitToken(DetailAST ast)
}
}

/**
* Checks if current variable is defined first in
* {@link TokenTypes#FOR_INIT for-loop init}, e.g.:
* <p>
* <code>
* for (int i = 0, j = 0; i < j; i++) { . . . }
* </code>
* </p>
* <code>i</code> is first variable in {@link TokenTypes#FOR_INIT for-loop init}
* @param variableDef variable definition node.
* @return true if variableDef is first variable in {@link TokenTypes#FOR_INIT for-loop init}
*/
private static boolean isFirstVariableInForInit(DetailAST variableDef)
{
return variableDef.getParent().getType() != TokenTypes.FOR_INIT
|| variableDef.getPreviousSibling() == null
|| variableDef.getPreviousSibling().getType() != TokenTypes.COMMA;
}

/**
* Determines whether an AST is a descendant of an abstract or native method.
* @param ast the AST to check.
Expand Down
Expand Up @@ -82,4 +82,17 @@ public void testNativeMethods() throws Exception
};
verify(checkConfig, getPath("coding/InputFinalLocalVariableNativeMethods.java"), expected);
}

@Test
public void testFalsePositive() throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(FinalLocalVariableCheck.class);
checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");

final String[] expected = {

};
verify(checkConfig, getPath("coding/InputFinalLocalVariableCheckFalsePositive.java"), expected);
}
}
@@ -0,0 +1,13 @@
package com.puppycrawl.tools.checkstyle.coding;

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

for ( int i = 0, s0 = list.size(); i < s0; i++ )
{
}
}
}

0 comments on commit 8f62c9f

Please sign in to comment.