From e2ad52ad596f594968bb08098777551c2bccf8bb Mon Sep 17 00:00:00 2001 From: rnveach Date: Thu, 28 Apr 2016 08:37:12 -0400 Subject: [PATCH] Issue #3136: added indentation check of while in do..while (#3139) --- .../checks/indentation/DoWhileHandler.java | 21 +++++++--- .../indentation/IndentationCheckTest.java | 39 ++++++++++++++++++- .../InputInvalidDoWhileIndent.java | 25 ++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidDoWhileIndent.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/DoWhileHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/DoWhileHandler.java index b26088d36..b2523cf41 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/DoWhileHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/DoWhileHandler.java @@ -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(); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index fd91c0842..89fcf14e4 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -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 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidDoWhileIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidDoWhileIndent.java new file mode 100644 index 000000000..03a153bd0 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputInvalidDoWhileIndent.java @@ -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 \ No newline at end of file