Issue #1293: Improved coverage for FinalLocalVariableCheck
This commit is contained in:
parent
e794bd04c7
commit
b097b3f002
1
pom.xml
1
pom.xml
|
|
@ -1097,7 +1097,6 @@
|
|||
<regex><pattern>.*.checks.UniquePropertiesCheck\$.*</pattern><branchRate>75</branchRate><lineRate>90</lineRate></regex>
|
||||
|
||||
<regex><pattern>.*.checks.coding.DeclarationOrderCheck</pattern><branchRate>82</branchRate><lineRate>93</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.FinalLocalVariableCheck</pattern><branchRate>96</branchRate><lineRate>100</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.IllegalInstantiationCheck</pattern><branchRate>81</branchRate><lineRate>97</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.ReturnCountCheck</pattern><branchRate>55</branchRate><lineRate>74</lineRate></regex>
|
||||
<regex><pattern>.*.checks.coding.VariableDeclarationUsageDistanceCheck</pattern><branchRate>90</branchRate><lineRate>98</lineRate></regex>
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ public class FinalLocalVariableCheck extends Check {
|
|||
break;
|
||||
case TokenTypes.VARIABLE_DEF:
|
||||
if (ast.getParent().getType() != TokenTypes.OBJBLOCK
|
||||
&& isVariableInForInit(ast)
|
||||
&& !isVariableInForInit(ast)
|
||||
&& shouldCheckEnhancedForLoopVariable(ast)
|
||||
&& !ast.branchContains(TokenTypes.FINAL)) {
|
||||
insertVariable(ast);
|
||||
|
|
@ -205,6 +205,7 @@ public class FinalLocalVariableCheck extends Check {
|
|||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalStateException("Incorrect token type");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -240,7 +241,7 @@ public class FinalLocalVariableCheck extends Check {
|
|||
* @return true if variable is defined in {@link TokenTypes#FOR_INIT for-loop init}
|
||||
*/
|
||||
private static boolean isVariableInForInit(DetailAST variableDef) {
|
||||
return variableDef.getParent().getType() != TokenTypes.FOR_INIT;
|
||||
return variableDef.getParent().getType() == TokenTypes.FOR_INIT;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -23,10 +23,13 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCh
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
public class FinalLocalVariableCheckTest
|
||||
extends BaseCheckTestSupport {
|
||||
|
|
@ -145,4 +148,20 @@ public class FinalLocalVariableCheckTest
|
|||
};
|
||||
verify(checkConfig, getPath("coding/InputFinalLocalVariableNameShadowing.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImproperToken() throws Exception {
|
||||
FinalLocalVariableCheck check = new FinalLocalVariableCheck();
|
||||
|
||||
DetailAST lambdaAst = new DetailAST();
|
||||
lambdaAst.setType(TokenTypes.LAMBDA);
|
||||
|
||||
try {
|
||||
check.visitToken(lambdaAst);
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
// it is OK
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,3 +163,11 @@ class class1
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
class A {
|
||||
{
|
||||
int y = 0;
|
||||
y = 9;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue