Issue #3136: added indentation check of while in do..while (#3139)

This commit is contained in:
rnveach 2016-04-28 08:37:12 -04:00 committed by Roman Ivanov
parent 4c26dcafb2
commit e2ad52ad59
3 changed files with 79 additions and 6 deletions

View File

@ -42,17 +42,28 @@ public class DoWhileHandler extends BlockParentHandler {
}
/**
* Check the indentation level of the conditional expression.
* Check the indentation level of the while and conditional expression.
*/
private void checkCondExpr() {
final DetailAST condAst = getMainAst()
.findFirstToken(TokenTypes.LPAREN).getNextSibling();
private void checkWhileExpr() {
// check while statement alone
final DetailAST whileAst = getMainAst().findFirstToken(TokenTypes.DO_WHILE);
if (isOnStartOfLine(whileAst)
&& !getIndent().isAcceptable(expandedTabsColumnNo(whileAst))) {
logError(whileAst, "while", expandedTabsColumnNo(whileAst));
}
// check condition alone
final DetailAST condAst = getMainAst().findFirstToken(TokenTypes.LPAREN).getNextSibling();
checkExpressionSubtree(condAst, getIndent(), false, false);
}
@Override
public void checkIndentation() {
super.checkIndentation();
checkCondExpr();
checkWhileExpr();
}
}

View File

@ -1202,6 +1202,42 @@ public class IndentationCheckTest extends BaseCheckTestSupport {
verifyWarns(checkConfig, fileName, expected);
}
@Test
public void testInvalidDoWhileWithChecker()
throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class);
checkConfig.addAttribute("arrayInitIndent", "4");
checkConfig.addAttribute("basicOffset", "4");
checkConfig.addAttribute("braceAdjustment", "0");
checkConfig.addAttribute("caseIndent", "4");
checkConfig.addAttribute("forceStrictCondition", "false");
checkConfig.addAttribute("lineWrappingIndentation", "4");
checkConfig.addAttribute("tabWidth", "4");
checkConfig.addAttribute("throwsIndent", "4");
final String fileName = getPath("InputInvalidDoWhileIndent.java");
final String[] expected = {
"7: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"8: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"9: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"10: " + getCheckMessage(MSG_ERROR, "do..while rcurly", 0, 8),
"11: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"12: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
"13: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"14: " + getCheckMessage(MSG_ERROR, "do..while lcurly", 0, 8),
"15: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"16: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
"17: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
"18: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"19: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
"20: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
"21: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
"22: " + getCheckMessage(MSG_CHILD_ERROR, "do..while", 0, 8),
"23: " + getCheckMessage(MSG_ERROR, "do..while rparen", 0, 8),
};
verifyWarns(checkConfig, fileName, expected);
}
@Test
public void testValidBlockWithChecker()
throws Exception {
@ -1655,7 +1691,8 @@ public class IndentationCheckTest extends BaseCheckTestSupport {
"input expected warning #" + position + " at line " + comment.getLineNumber()
+ " to report '" + comment.getExpectedMessage() + "' but got instead: "
+ line + ": " + message,
message.endsWith(comment.getExpectedMessage()));
line == comment.getLineNumber()
&& message.endsWith(comment.getExpectedMessage()));
}
@Override

View File

@ -0,0 +1,25 @@
package com.puppycrawl.tools.checkstyle.checks.indentation; //indent:0 exp:0
public class InputInvalidDoWhileIndent { //indent:0 exp:0
public void method1() { //indent:4 exp:4
boolean test = true; //indent:8 exp:8
do System.getProperty("foo"); while (test); //indent:0 exp:8 warn
do {} while (test); //indent:0 exp:8 warn
do { //indent:0 exp:8 warn
} while (test); //indent:0 exp:8 warn
do {} //indent:0 exp:8 warn
while (test); //indent:0 exp:8 warn
do //indent:0 exp:8 warn
{} while (test); //indent:0 exp:8 warn
do {} //indent:0 exp:8 warn
while //indent:0 exp:8 warn
(test); //indent:0 exp:8 warn
do {} while //indent:0 exp:8 warn
(test); //indent:0 exp:8 warn
do {} while //indent:0 exp:8 warn
( //indent:0 exp:8 warn
test //indent:0 exp:8 warn
); //indent:0 exp:8 warn
} //indent:4 exp:4
} //indent:0 exp:0