Skip to content

Commit

Permalink
Issue checkstyle#14825: fix false positive for WhitespaceAroundCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
strkkk authored and nrmancuso committed May 10, 2024
1 parent 14c818e commit e92ef06
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 50 deletions.
Expand Up @@ -71,6 +71,10 @@
* of the catch block (left curly bracket) is not separated from the end
* of the catch block (right curly bracket).
* </p>
* <p>
* Note: <a href="https://openjdk.org/jeps/361">
* Switch expressions</a> are ignored by this check.
* </p>
* <ul>
* <li>
* Property {@code allowEmptyCatches} - Allow empty catch bodies.
Expand Down Expand Up @@ -497,22 +501,37 @@ public void visitToken(DetailAST ast) {
*/
private boolean isNotRelevantSituation(DetailAST ast, int currentType) {
final int parentType = ast.getParent().getType();
final boolean starImport = currentType == TokenTypes.STAR
&& parentType == TokenTypes.DOT;
final boolean insideCaseGroup = parentType == TokenTypes.CASE_GROUP;

final boolean starImportOrSlistInsideCaseGroup = starImport || insideCaseGroup;
final boolean colonOfCaseOrDefaultOrForEach =
isColonOfCaseOrDefault(parentType)
|| isColonOfForEach(parentType);
final boolean emptyBlockOrType =
isEmptyBlock(ast, parentType)
final boolean result;
switch (parentType) {
case TokenTypes.DOT: {
result = currentType == TokenTypes.STAR;
break;
}
case TokenTypes.LITERAL_DEFAULT:
case TokenTypes.LITERAL_CASE:
case TokenTypes.CASE_GROUP: {
result = true;
break;
}
case TokenTypes.FOR_EACH_CLAUSE: {
result = ignoreEnhancedForColon;
break;
}
case TokenTypes.EXPR: {
result = currentType == TokenTypes.LITERAL_SWITCH;
break;
}
case TokenTypes.ARRAY_INIT:
case TokenTypes.ANNOTATION_ARRAY_INIT: {
result = currentType == TokenTypes.RCURLY;
break;
}
default: {
result = isEmptyBlock(ast, parentType)
|| allowEmptyTypes && isEmptyType(ast);

return starImportOrSlistInsideCaseGroup
|| colonOfCaseOrDefaultOrForEach
|| emptyBlockOrType
|| isArrayInitialization(currentType, parentType);
}
}
return result;
}

/**
Expand Down Expand Up @@ -615,41 +634,6 @@ private static boolean isEmptyBlock(DetailAST ast, int parentType, int match) {
return result;
}

/**
* Whether colon belongs to cases or defaults.
*
* @param parentType parent
* @return true if current token in colon of case or default tokens
*/
private static boolean isColonOfCaseOrDefault(int parentType) {
return parentType == TokenTypes.LITERAL_DEFAULT
|| parentType == TokenTypes.LITERAL_CASE;
}

/**
* Whether colon belongs to for-each.
*
* @param parentType parent
* @return true if current token in colon of for-each token
*/
private boolean isColonOfForEach(int parentType) {
return parentType == TokenTypes.FOR_EACH_CLAUSE
&& ignoreEnhancedForColon;
}

/**
* Is array initialization.
*
* @param currentType current token
* @param parentType parent token
* @return true is current token inside array initialization
*/
private static boolean isArrayInitialization(int currentType, int parentType) {
return currentType == TokenTypes.RCURLY
&& (parentType == TokenTypes.ARRAY_INIT
|| parentType == TokenTypes.ANNOTATION_ARRAY_INIT);
}

/**
* Test if the given {@code DetailAST} is part of an allowed empty
* method block.
Expand Down
Expand Up @@ -48,6 +48,10 @@
With this property turned off, this raises violation because the beginning
of the catch block (left curly bracket) is not separated from the end
of the catch block (right curly bracket).
&lt;/p&gt;
&lt;p&gt;
Note: &lt;a href="https://openjdk.org/jeps/361"&gt;
Switch expressions&lt;/a&gt; are ignored by this check.
&lt;/p&gt;</description>
<properties>
<property default-value="false" name="allowEmptyCatches" type="boolean">
Expand Down
Expand Up @@ -260,6 +260,13 @@ public void testSwitchWhitespaceAround() throws Exception {
getPath("InputWhitespaceAroundSwitch.java"), expected);
}

@Test
public void testSwitchExpressionWhitespaceAround() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getNonCompilablePath("InputWhitespaceAroundSwitchExpressions.java"), expected);
}

@Test
public void testDoWhileWhitespaceAround() throws Exception {
final String[] expected = {
Expand Down
@@ -0,0 +1,38 @@
/*
WhitespaceAround
allowEmptyConstructors = (default)false
allowEmptyMethods = (default)false
allowEmptyTypes = (default)false
allowEmptyLoops = (default)false
allowEmptyLambdas = (default)false
allowEmptyCatches = (default)false
ignoreEnhancedForColon = (default)true
tokens = (default)ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, \
BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAMBDA, LAND, \
LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, \
LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED, \
LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, \
NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR, \
SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND
*/

package com.puppycrawl.tools.checkstyle.checks.whitespace.whitespacearound;

import java.util.Optional;

public class InputWhitespaceAroundSwitchExpressions {

Optional<String> someMethod(int arg) {
return Optional.ofNullable(switch (arg) { // ok - switch expression
default -> null;
});
}

Optional<String> someMethod2(int arg) {
return Optional.ofNullable(((switch (arg) { // ok - switch expression
default -> null;
})));
}
}
4 changes: 4 additions & 0 deletions src/xdocs/checks/whitespace/whitespacearound.xml
Expand Up @@ -57,6 +57,10 @@ try {
catch block (left curly bracket) is not separated from the end of the catch
block (right curly bracket).
</p>
<p>
Note: <a href="https://openjdk.org/jeps/361">
Switch expressions</a> are ignored by this check.
</p>
</subsection>

<subsection name="Properties" id="Properties">
Expand Down
4 changes: 4 additions & 0 deletions src/xdocs/checks/whitespace/whitespacearound.xml.template
Expand Up @@ -57,6 +57,10 @@ try {
catch block (left curly bracket) is not separated from the end of the catch
block (right curly bracket).
</p>
<p>
Note: <a href="https://openjdk.org/jeps/361">
Switch expressions</a> are ignored by this check.
</p>
</subsection>

<subsection name="Properties" id="Properties">
Expand Down

0 comments on commit e92ef06

Please sign in to comment.