#21579·checkstyle

False Negative: NeedBraces silently passes lambdas in switch rule

Author: lithops-ztyCreated Sep 14, 2026Updated Sep 15, 2026
Labelsapproved

I have read check documentation: https://checkstyle.org/checks/blocks/needbraces.html I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

bash
/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="tokens" value="LAMBDA"/>
    </module>
  </module>
</module>

/var/tmp $ cat NeedBracesLambdaProbe.java
import java.util.function.Function;

public class NeedBracesLambdaProbe {
    Function<String, String> notInSwitchRule = Word -> Word.trim();   // line 4 -> reported

    static Function<String, String> ruleValue(int x) {
        return switch (x) {
            case 1 -> Word -> Word.trim();        // line 8 -> NOT reported
            default -> (Word) -> Word.trim();     // line 9 -> NOT reported
        };
    }
}

/var/tmp $ javac NeedBracesLambdaProbe.java

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-14.1.0-all.jar -c config.xml NeedBracesLambdaProbe.java
Starting audit...
[ERROR] /var/tmp/NeedBracesLambdaProbe.java:4:53: '->' construct must use '{}'s. [NeedBraces]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

Both lambdas in the switch rules should be flagged. The root cause is the same as #21578