Fix NeedBraces check not requiring braces in multiline statements when using allowSingleLineStatement option. #895

This commit is contained in:
Michal Kordas 2015-05-23 22:47:56 +02:00 committed by Roman Ivanov
parent be08323f8a
commit cc355f7e10
3 changed files with 18 additions and 3 deletions

View File

@ -299,9 +299,13 @@ public class NeedBracesCheck extends Check {
boolean result = false;
final DetailAST ifCondition = literalIf.findFirstToken(TokenTypes.EXPR);
if (literalIf.getParent().getType() == TokenTypes.SLIST) {
DetailAST block = literalIf.getLastChild();
if (block.getType() != TokenTypes.LITERAL_RETURN) {
block = literalIf.getLastChild().getPreviousSibling();
final DetailAST literalIfLastChild = literalIf.getLastChild();
final DetailAST block;
if (literalIfLastChild.getType() == TokenTypes.LITERAL_ELSE) {
block = literalIfLastChild.getPreviousSibling();
}
else {
block = literalIfLastChild;
}
result = ifCondition.getLineNo() == block.getLineNo();
}

View File

@ -67,6 +67,8 @@ public class NeedBracesCheckTest extends BaseCheckTestSupport {
"46: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
"53: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"),
"59: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
"88: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
"92: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"),
};
verify(checkConfig, getPath("InputBracesSingleLineStatements.java"), expected);
}

View File

@ -83,4 +83,13 @@ public class InputBracesSingleLineStatements
else System.out.println("no");
for (;;);
}
private int testMissingWarnings() {
if (true)
throw new RuntimeException();
if (true) {
return 1;
} else
return 2;
}
}