-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #14747: fix ParenPad to not flag unsupported Tokens #14792
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,7 @@ | |
|
||
package com.puppycrawl.tools.checkstyle.checks.whitespace; | ||
|
||
import java.util.BitSet; | ||
|
||
import com.puppycrawl.tools.checkstyle.StatelessCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
import com.puppycrawl.tools.checkstyle.utils.CommonUtil; | ||
|
@@ -97,7 +96,26 @@ | |
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA"> | ||
* LAMBDA</a>, | ||
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> | ||
* RECORD_DEF</a>. | ||
* RECORD_DEF</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPECAST"> | ||
* TYPECAST</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STAR"> | ||
* STAR</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PLUS"> | ||
* PLUS</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MINUS"> | ||
* MINUS</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DIV"> | ||
* DIV</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MOD"> | ||
* MOD</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND"> | ||
* LAND</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR"> | ||
* LOR</a>, | ||
* <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LNOT"> | ||
* LNOT</a> | ||
* . | ||
* </li> | ||
* </ul> | ||
* <p> | ||
|
@@ -123,20 +141,9 @@ | |
* | ||
* @since 3.0 | ||
*/ | ||
@StatelessCheck | ||
public class ParenPadCheck extends AbstractParenPadCheck { | ||
|
||
/** | ||
* Tokens that this check handles. | ||
*/ | ||
private final BitSet acceptableTokens; | ||
|
||
/** | ||
* Initializes acceptableTokens. | ||
*/ | ||
public ParenPadCheck() { | ||
acceptableTokens = TokenUtil.asBitSet(makeAcceptableTokens()); | ||
} | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return makeAcceptableTokens(); | ||
|
@@ -162,6 +169,15 @@ public void visitToken(DetailAST ast) { | |
case TokenTypes.DOT: | ||
case TokenTypes.EXPR: | ||
case TokenTypes.QUESTION: | ||
case TokenTypes.TYPECAST: | ||
case TokenTypes.STAR: | ||
case TokenTypes.PLUS: | ||
case TokenTypes.MINUS: | ||
case TokenTypes.DIV: | ||
case TokenTypes.MOD: | ||
case TokenTypes.LAND: | ||
case TokenTypes.LOR: | ||
case TokenTypes.LNOT: | ||
processExpression(ast); | ||
break; | ||
case TokenTypes.LITERAL_FOR: | ||
|
@@ -239,8 +255,7 @@ private void visitLiteralFor(DetailAST ast) { | |
} | ||
|
||
/** | ||
* Checks parens inside {@link TokenTypes#EXPR}, {@link TokenTypes#QUESTION} | ||
* and {@link TokenTypes#METHOD_CALL}. | ||
* Checks all parens that are children of a given Token. | ||
* | ||
* @param ast the token to check. | ||
*/ | ||
|
@@ -253,31 +268,10 @@ private void processExpression(DetailAST ast) { | |
else if (currentNode.getType() == TokenTypes.RPAREN && !isInTypecast(currentNode)) { | ||
processRight(currentNode); | ||
} | ||
else if (currentNode.hasChildren() && !isAcceptableToken(currentNode)) { | ||
// Traverse all subtree tokens which will never be configured | ||
// to be launched in visitToken() | ||
currentNode = currentNode.getFirstChild(); | ||
continue; | ||
} | ||
|
||
// Go up after processing the last child | ||
while (currentNode.getNextSibling() == null && currentNode.getParent() != ast) { | ||
currentNode = currentNode.getParent(); | ||
} | ||
currentNode = currentNode.getNextSibling(); | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether AcceptableTokens contains the given ast. | ||
* | ||
* @param ast the token to check. | ||
* @return true if the ast is in AcceptableTokens. | ||
*/ | ||
private boolean isAcceptableToken(DetailAST ast) { | ||
return acceptableTokens.get(ast.getType()); | ||
} | ||
|
||
Comment on lines
-276
to
-280
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need this anymore we needed it to avoid checking the same node twice. while doing a deep scan when we found a subtree that is acceptableToken we don't visit it because it will be picked up with now there is no deep scan for tokens so we don't need this check. I removed this method, the related unnecessary class fields and the test corresponding to it |
||
/** | ||
* Returns array of acceptable tokens. | ||
* | ||
|
@@ -306,6 +300,15 @@ private static int[] makeAcceptableTokens() { | |
TokenTypes.SUPER_CTOR_CALL, | ||
TokenTypes.LAMBDA, | ||
TokenTypes.RECORD_DEF, | ||
TokenTypes.TYPECAST, | ||
TokenTypes.STAR, | ||
TokenTypes.PLUS, | ||
TokenTypes.MINUS, | ||
TokenTypes.DIV, | ||
TokenTypes.MOD, | ||
TokenTypes.LAND, | ||
TokenTypes.LOR, | ||
TokenTypes.LNOT, | ||
Comment on lines
+303
to
+311
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some tokens that got missed due to having no deep scan (we may need to add some more tokens but those were the obvious one from the test and input files) |
||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the deep scan and just check the first child and all of its siblings