Skip to content

Commit c13d121

Browse files
committed
Skip lambda checks in switch expressions
Update `SpringLambdaCheck` to skip switch statement "arrow case" labels. Fixes gh-300
1 parent 3aeeba7 commit c13d121

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLambdaCheck.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
2121

2222
/**
23-
* Checks that lambda definitions follow Spring conventions. Single argument lambda
24-
* parameters should have parentheses. Single statement implementations should not use
25-
* curly braces.
23+
* Checks that lambda definitions follow Spring conventions. Single argument
24+
* lambda parameters should have parentheses. Single statement implementations
25+
* should not use curly braces.
2626
*
2727
* @author Phillip Webb
2828
*/
@@ -38,7 +38,8 @@ public int[] getAcceptableTokens() {
3838

3939
@Override
4040
public void visitToken(DetailAST ast) {
41-
if (ast.getType() == TokenTypes.LAMBDA) {
41+
if (ast.getType() == TokenTypes.LAMBDA && ast.getParent() != null
42+
&& ast.getParent().getType() != TokenTypes.SWITCH_RULE) {
4243
visitLambda(ast);
4344
}
4445
}
@@ -48,8 +49,7 @@ private void visitLambda(DetailAST lambda) {
4849
boolean hasParentheses = hasToken(lambda, TokenTypes.LPAREN);
4950
if (this.singleArgumentParentheses && !hasParentheses) {
5051
log(lambda.getLineNo(), lambda.getColumnNo(), "lambda.missingParen");
51-
}
52-
else if (!this.singleArgumentParentheses && hasParentheses) {
52+
} else if (!this.singleArgumentParentheses && hasParentheses) {
5353
if (!isUsingParametersToDefineType(lambda)) {
5454
log(lambda.getLineNo(), lambda.getColumnNo(), "lambda.unnecessaryParen");
5555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.util.function.Function;
18+
19+
/**
20+
* Lambda in a switch statement.
21+
*
22+
* @author Josef Reichardt
23+
*/
24+
public class LambdaSwitch {
25+
26+
private void sayHello(String[] args) {
27+
Function<Integer, String> getText = (cnt) -> "Number of args: " + cnt;
28+
String message = switch (args.length) {
29+
case 0 -> "No arg";
30+
case 1 -> "One arg";
31+
default -> getText.apply(args.length);
32+
};
33+
System.out.println(message);
34+
}
35+
}

0 commit comments

Comments
 (0)