VariableDeclarationUsageDistanceCheck was update. Create new extended message in case ignoreFinal=true #239

This commit is contained in:
Max 2014-08-11 15:42:21 +04:00 committed by Roman Ivanov
parent fb1407c204
commit 9324fdcf34
3 changed files with 44 additions and 30 deletions

View File

@ -172,6 +172,11 @@ public class VariableDeclarationUsageDistanceCheck extends Check
*/
public static final String MSG_KEY = "variable.declaration.usage.distance";
/**
* Warning message key.
*/
public static final String MSG_KEY_EXT = "variable.declaration.usage.distance.extend";
/**
* Default value of distance between declaration of variable and its first
* usage.
@ -275,8 +280,14 @@ public class VariableDeclarationUsageDistanceCheck extends Check
if (dist > mAllowedDistance
&& !isInitializationSequence(variableUsageAst, variable.getText()))
{
log(variable.getLineNo(),
MSG_KEY, variable.getText(), dist, mAllowedDistance);
if (mIgnoreFinal) {
log(variable.getLineNo(),
MSG_KEY_EXT, variable.getText(), dist, mAllowedDistance);
}
else {
log(variable.getLineNo(),
MSG_KEY, variable.getText(), dist, mAllowedDistance);
}
}
}
}

View File

@ -64,4 +64,5 @@ unnecessary.paren.return=Unnecessary parentheses around return value.
unnecessary.paren.string=Unnecessary parentheses around string {0}.
package.dir.mismatch=Package declaration does not match directory ''{0}''.
variable.declaration.usage.distance=Distance between variable ''{0}'' declaration and its first usage is {1}, but allowed {2}.
variable.declaration.usage.distance.extend=Distance between variable ''{0}'' declaration and its first usage is {1}, but allowed {2}. Consider to make that variable as final if you still need to store its value in advance (before method calls that might do side effect on original value).
overload.methods.declaration=Overload methods should not be split. Previous overloaded method located at line ''{0}''.

View File

@ -19,6 +19,8 @@
package com.puppycrawl.tools.checkstyle.checks.coding;
import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY;
import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY_EXT;
import static java.text.MessageFormat.format;
import org.junit.Test;
@ -171,34 +173,34 @@ public class VariableDeclarationUsageDistanceCheckTest extends
checkConfig.addAttribute("validateBetweenScopes", "true");
checkConfig.addAttribute("ignoreFinal", "true");
final String[] expected = {
"30: " + getCheckMessage(MSG_KEY, "a", 2, 1),
"38: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
"44: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
"57: " + getCheckMessage(MSG_KEY, "count", 2, 1),
"71: " + getCheckMessage(MSG_KEY, "count", 4, 1),
"96: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
"144: " + getCheckMessage(MSG_KEY, "m", 3, 1),
"145: " + getCheckMessage(MSG_KEY, "n", 2, 1),
"184: " + getCheckMessage(MSG_KEY, "result", 2, 1),
"219: " + getCheckMessage(MSG_KEY, "t", 5, 1),
"222: " + getCheckMessage(MSG_KEY, "c", 3, 1),
"223: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
"260: " + getCheckMessage(MSG_KEY, "selected", 2, 1),
"261: " + getCheckMessage(MSG_KEY, "model", 2, 1),
"287: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
"300: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
"343: " + getCheckMessage(MSG_KEY, "green", 2, 1),
"344: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
"454: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
"455: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
"471: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
"471: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
"479: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
"491: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
"504: " + getCheckMessage(MSG_KEY, "count", 4, 1),
"505: " + getCheckMessage(MSG_KEY, "files", 2, 1),
"540: " + getCheckMessage(MSG_KEY, "id", 2, 1),
"542: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
"30: " + getCheckMessage(MSG_KEY_EXT, "a", 2, 1),
"38: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
"44: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
"57: " + getCheckMessage(MSG_KEY_EXT, "count", 2, 1),
"71: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
"96: " + getCheckMessage(MSG_KEY_EXT, "arg", 2, 1),
"144: " + getCheckMessage(MSG_KEY_EXT, "m", 3, 1),
"145: " + getCheckMessage(MSG_KEY_EXT, "n", 2, 1),
"184: " + getCheckMessage(MSG_KEY_EXT, "result", 2, 1),
"219: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 1),
"222: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
"223: " + getCheckMessage(MSG_KEY_EXT, "d2", 3, 1),
"260: " + getCheckMessage(MSG_KEY_EXT, "selected", 2, 1),
"261: " + getCheckMessage(MSG_KEY_EXT, "model", 2, 1),
"287: " + getCheckMessage(MSG_KEY_EXT, "sw", 2, 1),
"300: " + getCheckMessage(MSG_KEY_EXT, "wh", 2, 1),
"343: " + getCheckMessage(MSG_KEY_EXT, "green", 2, 1),
"344: " + getCheckMessage(MSG_KEY_EXT, "blue", 3, 1),
"454: " + getCheckMessage(MSG_KEY_EXT, "aOpt", 3, 1),
"455: " + getCheckMessage(MSG_KEY_EXT, "bOpt", 2, 1),
"471: " + getCheckMessage(MSG_KEY_EXT, "l1", 3, 1),
"471: " + getCheckMessage(MSG_KEY_EXT, "l2", 2, 1),
"479: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 1),
"491: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 1),
"504: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
"505: " + getCheckMessage(MSG_KEY_EXT, "files", 2, 1),
"540: " + getCheckMessage(MSG_KEY_EXT, "id", 2, 1),
"542: " + getCheckMessage(MSG_KEY_EXT, "parentId", 3, 1),
};
verify(checkConfig, getPath("coding/InputVariableDeclarationUsageDistanceCheck.java"), expected);
}