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 committed May 5, 2024
1 parent e6051f2 commit e6cdb69
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
Expand Up @@ -497,22 +497,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 +630,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 @@ -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;
})));
}
}

0 comments on commit e6cdb69

Please sign in to comment.