-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Closed
Description
Child of #8658
Check documentation: https://checkstyle.sourceforge.io/config_blocks.html#NeedBraces
Checks for braces around code blocks.
➜ src /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java
➜ src 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"/>
<property name="allowSingleLineStatement" value="true"/>
</module>
</module>
</module>
➜ src cat TestClass.java
interface MathOperation2 {
int operation(int a, int b);
}
public class TestClass {
MathOperation2 tooManyParens(int k) {
return switch (k) {
case 1 -> {
MathOperation2 case5 = (a, b) ->
(a + b); // should be violation
yield case5;
}
case (2) -> { // should be violation
MathOperation2 case6 = (int a, int b) ->
(a + b); // should be violation
yield case6;
}
case 3 -> {
MathOperation2 case7 = (int a, int b) -> { // ok
return (a + b);
};
yield (case7);
}
case 4 -> {
Runnable r3 = () -> // should be violation
String.CASE_INSENSITIVE_ORDER.equals("Hello world two!");
MathOperation2 case10 = (int x, int y) -> { // ok
return (x + y);
};
yield case10; }
default -> {
MathOperation2 case8 = (int x, int y) -> { // ok
return (x + y);
};
yield case8;
}
};
}
}
➜ src java -jar /home/nick/IdeaProjects/checkstyle/target/checkstyle-8.36-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing TestClass.java
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:311)
at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:221)
at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:408)
at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:331)
at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:190)
at com.puppycrawl.tools.checkstyle.Main.main(Main.java:125)
Caused by: java.lang.NullPointerException
at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.getLastLambdaToken(NeedBracesCheck.java:474)
at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSingleLineLambda(NeedBracesCheck.java:460)
at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSingleLineStatement(NeedBracesCheck.java:344)
at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.isSkipStatement(NeedBracesCheck.java:308)
at com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.visitToken(NeedBracesCheck.java:236)
at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:340)
at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:451)
at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:278)
at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:151)
at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:87)
at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:337)
at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:298)
... 5 more
Checkstyle ends with 1 errors.
I would expect the above violations, and would not expect this check to produce a NPE. We need to update this check.