Skip to content

Commit

Permalink
LocalVariableNameUpdate #23
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Jul 5, 2014
1 parent dcd4e91 commit 5b98ebf
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Expand Up @@ -18,9 +18,12 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.naming;

import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.Utils;

/**
* <p>
Expand Down Expand Up @@ -52,12 +55,24 @@
public class LocalVariableNameCheck
extends AbstractNameCheck
{
/**
* Allow one character loop variable.
*/
private boolean mAllowOneCharForLoopVar;

public static Pattern aSingleChar = Utils.getPattern("^[a-z]");

/** Creates a new <code>LocalVariableNameCheck</code> instance. */
public LocalVariableNameCheck()
{
super("^[a-z][a-zA-Z0-9]*$");
}

public final void setAllowOneCharForLoopVar(boolean aAllow)
{
mAllowOneCharForLoopVar = aAllow;
}

@Override
public int[] getDefaultTokens()
{
Expand All @@ -74,6 +89,14 @@ protected final boolean mustCheckName(DetailAST aAST)
aAST.findFirstToken(TokenTypes.MODIFIERS);
final boolean isFinal = (modifiersAST != null)
&& modifiersAST.branchContains(TokenTypes.FINAL);
if(isForLoopVariable(aAST) && mAllowOneCharForLoopVar) {
String variableName = aAST.findFirstToken(TokenTypes.IDENT).getText();
return !aSingleChar.matcher(variableName).find();
}
return (!isFinal && ScopeUtils.isLocalVariableDef(aAST));
}

private boolean isForLoopVariable(DetailAST aVariableDef) {
return aVariableDef.getParent().getType() == TokenTypes.FOR_INIT;
}
}
Expand Up @@ -63,5 +63,24 @@ public void testCatchParameter()
};
verify(checkConfig, getPath("InputEmptyStatement.java"), expected);
}

@Test
public void testLoopVariables()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(LocalVariableNameCheck.class);
checkConfig.addAttribute("format", "^[a-z]{2,}[a-zA-Z0-9]*$");
checkConfig.addAttribute("allowOneCharForLoopVar", "true");
final String[] expected = {
"119:13: Name 'ABC' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
"130:18: Name 'I' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
"132:20: Name 'InnerBlockVariable' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
"207:21: Name 'O' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
"235:21: Name 'i' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
"241:17: Name 'b' must match pattern '^[a-z]{2,}[a-zA-Z0-9]*$'.",
};
verify(checkConfig, getPath("InputSimple.java"), expected);
}
}

Expand Up @@ -223,3 +223,33 @@ enum MyEnum1
/** Should be mSomeMemeber */
private int someMember;
}

class LoopVariables
{
public void fooMethod()
{
for(int i = 1; i <10; i++) {
//come code
}

int i = 0;

for(boolean b = false; b != true; b = true) {
//some code
}

boolean b;

for(Integer k = 1; k <10; k++) {
//come code
}

for(double d = 1; d <10; d++) {
//come code
}

for(long l = 1; l <10; l++) {
//come code
}
}
}

0 comments on commit 5b98ebf

Please sign in to comment.