From a4cd7e22b031975b5336c3a99f5ca2cad730891b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 6 Oct 2021 19:54:29 -0700 Subject: [PATCH] Skip lambda checks in switch expressions Update `SpringLambdaCheck` to skip switch statement "arrow case" labels. Fixes gh-300 --- .../checkstyle/check/SpringLambdaCheck.java | 3 +- .../src/test/resources/check/LambdaSwitch.txt | 1 + .../test/resources/source/LambdaSwitch.java | 35 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/LambdaSwitch.txt create mode 100644 spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/LambdaSwitch.java diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLambdaCheck.java b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLambdaCheck.java index 4d452ea7..42822761 100644 --- a/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLambdaCheck.java +++ b/spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringLambdaCheck.java @@ -38,7 +38,8 @@ public int[] getAcceptableTokens() { @Override public void visitToken(DetailAST ast) { - if (ast.getType() == TokenTypes.LAMBDA) { + if (ast.getType() == TokenTypes.LAMBDA && ast.getParent() != null + && ast.getParent().getType() != TokenTypes.SWITCH_RULE) { visitLambda(ast); } } diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/LambdaSwitch.txt b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/LambdaSwitch.txt new file mode 100644 index 00000000..69174e4c --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/check/LambdaSwitch.txt @@ -0,0 +1 @@ ++0 errors \ No newline at end of file diff --git a/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/LambdaSwitch.java b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/LambdaSwitch.java new file mode 100644 index 00000000..59d213ac --- /dev/null +++ b/spring-javaformat/spring-javaformat-checkstyle/src/test/resources/source/LambdaSwitch.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.function.Function; + +/** + * Lambda in a switch statement. + * + * @author Josef Reichardt + */ +public class LambdaSwitch { + + private void sayHello(String[] args) { + Function getText = (cnt) -> "Number of args: " + cnt; + String message = switch (args.length) { + case 0 -> "No arg"; + case 1 -> "One arg"; + default -> getText.apply(args.length); + }; + System.out.println(message); + } +}