Issue #1293: Refactoring of UnnecessaryParanthesesCheck. UT coverage was increased up to 100%.

This commit is contained in:
Baratali Izmailov 2015-07-05 05:33:16 -07:00 committed by Roman Ivanov
parent ce59d6abb3
commit b433c987fa
3 changed files with 28 additions and 29 deletions

View File

@ -805,7 +805,6 @@
<regex><pattern>.*.checks.coding.SimplifyBooleanExpressionCheck</pattern><branchRate>100</branchRate><lineRate>77</lineRate></regex>
<regex><pattern>.*.checks.coding.SimplifyBooleanReturnCheck</pattern><branchRate>83</branchRate><lineRate>96</lineRate></regex>
<regex><pattern>.*.checks.coding.StringLiteralEqualityCheck</pattern><branchRate>100</branchRate><lineRate>87</lineRate></regex>
<regex><pattern>.*.checks.coding.UnnecessaryParenthesesCheck</pattern><branchRate>91</branchRate><lineRate>96</lineRate></regex>
<regex><pattern>.*.checks.coding.VariableDeclarationUsageDistanceCheck</pattern><branchRate>90</branchRate><lineRate>97</lineRate></regex>

View File

@ -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 {
* <code>TokenTypes.EXPR</code>.
* @return <code>true</code> if the expression is surrounded by
* parentheses.
* @throws IllegalArgumentException if <code>ast.getType()</code> is not
* equal to <code>TokenTypes.EXPR</code>.
*/
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;
}
/**

View File

@ -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());
}
}