public class MoveVariableInsideIfCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks if a variable is only used inside if statements and asks for its declaration to be moved there too.
Rationale: Code inside if/else statements are only executed when those specific block conditions evaluate to true. Moving variables inside these blocks prevents the code from being executed when the value of the variable is not even being used. It also helps limit the scope of the variables from being too broad to confuse new readers. Suppressing variables with false violations because of the check's limitations (stated below) also help clearly state the purpose of the variable as a temporary storage for a current/future changing value.
An example of how to configure the check is:
<module name="MoveVariableInsideIfCheck"/>
which will produce the following violation:
String variable = input.substring(1); // violation - variable is only used inside if block
if (condition) {
return method(variable);
}
return "";
The code can be written as the following to avoid a violation:
if (condition) {
String variable = input.substring(1);
return method(variable);
}
return "";
No violations will be produced if a variable is used in same scope as declaration, condition of block, or if used in multiple blocks.
String variable = input.substring(1);
if (condition && variable.charAt(0) == 'T') {
return method(variable);
}
else {
return method2(variable);
}
return "";
Limitations: The check can not determine if the value of variable being stored is changed after the declaration. Variables like this can't be moved, or may be too complex to move, and thus should be suppressed.
Case #1:
final String variable = list.remove(0); // false positive - list is modified with storing value
final String next = list.get(0); // expecting above list modification
if (next.equals(input)) {
list.add(variable);
}
Case #2:
final String variable = field.get(0); // false positive - field is modified later, before block
modifyField(); // field is modified inside this method
if (condition) {
field.add(variable);
}
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
MoveVariableInsideIfCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.