Skip to content

Commit

Permalink
Merge f393975 into 7feb71c
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed May 12, 2017
2 parents 7feb71c + f393975 commit b52224a
Show file tree
Hide file tree
Showing 19 changed files with 681 additions and 29 deletions.
Expand Up @@ -177,3 +177,6 @@ EitherLogOrThrowCheck.loggingMethodNames = Logging method names separated with c
WhitespaceBeforeArrayInitializerCheck.name = Whitespace Before Array Initializer
WhitespaceBeforeArrayInitializerCheck.desc = This checks enforces whitespace before array initializer.
MoveVariableInsideIfCheck.name = Move Variable Inside If Check
MoveVariableInsideIfCheck.desc = Checks if a variable is only used inside if statements and asks for it's declaration to be moved there too.
Expand Up @@ -529,5 +529,12 @@
<message-key key="whitespace.before.array.initializer"/>
</rule-metadata>

<rule-metadata name="%MoveVariableInsideIfCheck.name" internal-name="MoveVariableInsideIfCheck" parent="TreeWalker">
<alternative-name internal-name="com.github.sevntu.checkstyle.checks.coding.MoveVariableInsideIfCheck"/>
<description>%MoveVariableInsideIfCheck.desc</description>

<message-key key="move.variable.inside"/>
</rule-metadata>

</rule-group-metadata>
</checkstyle-metadata>
1 change: 1 addition & 0 deletions sevntu-checks/sevntu-checks.xml
Expand Up @@ -154,6 +154,7 @@
<module name="com.github.sevntu.checkstyle.checks.coding.UselessSuperCtorCall"/>
<module name="com.github.sevntu.checkstyle.checks.coding.UselessSingleCatch"/>
<module name="com.github.sevntu.checkstyle.checks.coding.WhitespaceBeforeArrayInitializer"/>
<module name="com.github.sevntu.checkstyle.checks.coding.MoveVariableInsideIf" />

<!-- Design -->
<module name="com.github.sevntu.checkstyle.checks.design.ChildBlockLength">
Expand Down
Expand Up @@ -231,9 +231,9 @@ private boolean isRatioBetweenIfAndElseBlockSuitable(DetailAST literalIf) {
boolean result = true;

final DetailAST lastChildAfterIf = literalIf.getLastChild();
final int linesOfCodeInIfBlock = getAmounOfCodeRowsInBlock(literalIf);
final int linesOfCodeInElseBlock = getAmounOfCodeRowsInBlock(lastChildAfterIf);
if (linesOfCodeInElseBlock > 0) {
final int linesOfCodeInIfBlock = getAmounOfCodeRowsInBlock(literalIf);
result = linesOfCodeInIfBlock / linesOfCodeInElseBlock < multiplyFactorForElseBlocks;
}
return result;
Expand Down
Expand Up @@ -390,6 +390,8 @@ public void visitToken(DetailAST ast) {
public void leaveToken(DetailAST ast) {
if (ast.getType() == TokenTypes.CLASS_DEF
&& !isClassDefInMethodDef(ast)) {
// -@cs[MoveVariableInsideIf] assignment value is a modification
// call so it can't be moved
final ClassDetail classDetail = classDetails.pop();

if (checkGettersSetters) {
Expand Down Expand Up @@ -687,9 +689,6 @@ private static boolean isSetterName(String methodName) {
private boolean isGetterCorrect(DetailAST methodDef, String methodPrefix) {
boolean result = false;

final String methodName = getIdentifier(methodDef);
final String methodNameWithoutPrefix = getNameWithoutPrefix(methodName, methodPrefix);

final DetailAST parameters = methodDef.findFirstToken(TokenTypes.PARAMETERS);

// no parameters
Expand All @@ -703,6 +702,9 @@ private boolean isGetterCorrect(DetailAST methodDef, String methodPrefix) {
if (returnStatementAst != null) {
final DetailAST exprAst = returnStatementAst.getFirstChild();
final String returnedFieldName = getNameOfGetterField(exprAst);
final String methodName = getIdentifier(methodDef);
final String methodNameWithoutPrefix = getNameWithoutPrefix(methodName,
methodPrefix);
if (returnedFieldName != null
&& !localVariableHidesField(statementsAst, returnedFieldName)
&& verifyFieldAndMethodName(returnedFieldName,
Expand Down Expand Up @@ -748,15 +750,14 @@ private static boolean localVariableHidesField(DetailAST slist,
private boolean isSetterCorrect(DetailAST methodDefAst, String methodPrefix) {
boolean result = false;

final String methodName = getIdentifier(methodDefAst);
final String setterFieldName = fieldPrefix
+ getNameWithoutPrefix(methodName, methodPrefix);

final DetailAST methodTypeAst = methodDefAst.findFirstToken(TokenTypes.TYPE);

if (methodTypeAst.branchContains(TokenTypes.LITERAL_VOID)) {

final DetailAST statementsAst = methodDefAst.findFirstToken(TokenTypes.SLIST);
final String methodName = getIdentifier(methodDefAst);
final String setterFieldName = fieldPrefix
+ getNameWithoutPrefix(methodName, methodPrefix);

result = statementsAst != null
&& !localVariableHidesField(statementsAst, setterFieldName)
Expand Down Expand Up @@ -1066,9 +1067,8 @@ private static boolean isMainMethodModifiers(final DetailAST methodAST) {
*/
private static boolean isVoidType(final DetailAST methodAST) {
boolean result = true;
DetailAST methodTypeAST = null;
if (hasChildToken(methodAST, TokenTypes.TYPE)) {
methodTypeAST = methodAST.findFirstToken(TokenTypes.TYPE);
final DetailAST methodTypeAST = methodAST.findFirstToken(TokenTypes.TYPE);
result = hasChildToken(methodTypeAST, TokenTypes.LITERAL_VOID);
}
return result;
Expand Down Expand Up @@ -1343,9 +1343,8 @@ else if (isBooleanGetterName(getterName)) {
// method is NOT getter
final DetailAST setterAst = allGettersSetters.get(j);
final String setterName = getIdentifier(setterAst);
String setterField = null;
if (isSetterName(setterName)) {
setterField = getNameWithoutPrefix(
final String setterField = getNameWithoutPrefix(
getIdentifier(setterAst), SETTER_PREFIX);

// if fields are same and setter is sibling with getter
Expand Down
Expand Up @@ -330,14 +330,10 @@ private String validate(DetailAST forLiteralNode) {
final DetailAST forEachNode = forLiteralNode.findFirstToken(TokenTypes.FOR_EACH_CLAUSE);
final DetailAST keySetOrEntrySetNode =
getKeySetOrEntrySetNode(forEachNode);
boolean isMapClassField = false;
// Search for keySet or entrySet
if (keySetOrEntrySetNode != null) {
if (keySetOrEntrySetNode.getPreviousSibling().getChildCount() != 0) {
isMapClassField = true;
}
final DetailAST variableDefNode = forEachNode.getFirstChild();
final String keyOrEntryVariableName = variableDefNode.getLastChild().getText();
final boolean isMapClassField = keySetOrEntrySetNode.getPreviousSibling()
.getChildCount() != 0;

final String currentMapVariableName;

Expand All @@ -351,6 +347,8 @@ private String validate(DetailAST forLiteralNode) {
final DetailAST forEachOpeningBrace = forLiteralNode.getLastChild();

if (!isMapPassedIntoAnyMethod(forEachOpeningBrace)) {
final DetailAST variableDefNode = forEachNode.getFirstChild();
final String keyOrEntryVariableName = variableDefNode.getLastChild().getText();

if (proposeKeySetUsage
&& KEY_SET_METHOD_NAME.equals(
Expand Down

0 comments on commit b52224a

Please sign in to comment.