diff --git a/pom.xml b/pom.xml index f87e77aa9..1a7783e8e 100644 --- a/pom.xml +++ b/pom.xml @@ -805,7 +805,6 @@ .*.checks.coding.SimplifyBooleanExpressionCheck10077 .*.checks.coding.SimplifyBooleanReturnCheck8396 .*.checks.coding.StringLiteralEqualityCheck10087 - .*.checks.coding.UnnecessaryParenthesesCheck9196 .*.checks.coding.VariableDeclarationUsageDistanceCheck9097 diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java index c45ad434e..b98563285 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java @@ -19,7 +19,6 @@ package com.puppycrawl.tools.checkstyle.checks.coding; -import antlr.collections.AST; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; @@ -89,8 +88,6 @@ public class UnnecessaryParenthesesCheck extends Check { */ public static final String MSG_RETURN = "unnecessary.paren.return"; - /** The minimum number of child nodes to consider for a match. */ - private static final int MIN_CHILDREN_FOR_MATCH = 3; /** The maximum string length before we chop the string. */ private static final int MAX_QUOTED_LENGTH = 25; @@ -186,6 +183,12 @@ public class UnnecessaryParenthesesCheck extends Check { }; } + @Override + public int[] getRequiredTokens() { + // Check can work with any of acceptable tokens + return new int[] {}; + } + @Override public void visitToken(DetailAST ast) { final int type = ast.getType(); @@ -246,7 +249,7 @@ public class UnnecessaryParenthesesCheck extends Check { // warning about an immediate child node in visitToken, so we don't // need to log another one here. - if (parentToSkip != ast && exprSurrounded(ast)) { + if (parentToSkip != ast && isExprSurrounded(ast)) { if (assignDepth >= 1) { log(ast, MSG_ASSIGN); } @@ -279,11 +282,10 @@ public class UnnecessaryParenthesesCheck extends Check { * parentheses. */ private boolean isSurrounded(DetailAST ast) { + // if previous sibling is left parenthesis, + // next sibling can't be other than right parenthesis final DetailAST prev = ast.getPreviousSibling(); - final DetailAST next = ast.getNextSibling(); - - return prev != null && prev.getType() == TokenTypes.LPAREN - && next != null && next.getType() == TokenTypes.RPAREN; + return prev != null && prev.getType() == TokenTypes.LPAREN; } /** @@ -292,22 +294,9 @@ public class UnnecessaryParenthesesCheck extends Check { * TokenTypes.EXPR. * @return true if the expression is surrounded by * parentheses. - * @throws IllegalArgumentException if ast.getType() is not - * equal to TokenTypes.EXPR. */ - private boolean exprSurrounded(DetailAST ast) { - if (ast.getType() != TokenTypes.EXPR) { - throw new IllegalArgumentException("Not an expression node."); - } - boolean surrounded = false; - if (ast.getChildCount() >= MIN_CHILDREN_FOR_MATCH) { - final AST n1 = ast.getFirstChild(); - final AST nn = ast.getLastChild(); - - surrounded = n1.getType() == TokenTypes.LPAREN - && nn.getType() == TokenTypes.RPAREN; - } - return surrounded; + private boolean isExprSurrounded(DetailAST ast) { + return ast.getFirstChild().getType() == TokenTypes.LPAREN; } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java index bd5dffe7e..14f78341b 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java @@ -19,11 +19,6 @@ package com.puppycrawl.tools.checkstyle.checks.coding; -import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; -import com.puppycrawl.tools.checkstyle.DefaultConfiguration; -import java.io.File; -import org.junit.Test; - import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_ASSIGN; import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_EXPR; import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_IDENT; @@ -31,6 +26,14 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthes import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_RETURN; import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_STRING; +import java.io.File; + +import org.junit.Assert; +import org.junit.Test; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + /** * Test fixture for the UnnecessaryParenthesesCheck. * @@ -101,4 +104,12 @@ public class UnnecessaryParenthesesCheckTest extends BaseCheckTestSupport { final String[] expected = {}; verify(checkConfig, getPath("Input15Extensions.java"), expected); } + + @Test + public void testTokensNotNull() { + UnnecessaryParenthesesCheck check = new UnnecessaryParenthesesCheck(); + Assert.assertNotNull(check.getDefaultTokens()); + Assert.assertNotNull(check.getAcceptableTokens()); + Assert.assertNotNull(check.getRequiredTokens()); + } }