Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LambdaParameterNameCheck throws NPE when parsing switch expressions #8683

Closed
nrmancuso opened this issue Aug 12, 2020 · 1 comment
Closed
Milestone

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Aug 12, 2020

Child of #8658
Check documentation: https://checkstyle.sourceforge.io/config_naming.html#LambdaParameterName

Checks lambda parameter names.

➜  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="LambdaParameterName"/>
    </module>
</module>
➜  src cat TestClass.java                                                                
import java.util.stream.Stream;

public class TestClass {

    boolean method1(Nums k, String string) { 
        switch (k) {
            case ONE:
                Stream.of(string.split(" "))
                        .map(word -> word.trim()) 
                        .anyMatch(Word -> "in".equals(Word)); // violation
                break;
            default: ;
        }
        return false;
    }

    boolean method2(Nums k, String string) { 
        switch (k) {
            case ONE -> {
                Stream.of(string.split(" "))
                        .map(word -> word.trim())
                        .anyMatch(Word -> "in".equals(Word)); // violation
                System.out.println("case one");
            }
            default -> Stream.of(string.split(" "))
                    .map(word -> word.trim())
                    .anyMatch(Word -> "in".equals(Word)); // violation
        }
        return true;
    }

    boolean method3(Nums k, String string) {
        return switch (k) {
            case ONE:
                yield Stream.of(string.split(" "))
                        .map(word -> word.trim()) 
                        .anyMatch(Word -> "in".equals(Word)); // violation
            default: yield false;
        };
    }

    boolean method4(Nums k, String string) {
        return switch (k) {
            case ONE -> {
                yield Stream.of(string.split(" "))
                        .map(word -> word.trim())
                        .anyMatch(Word -> "in".equals(Word)); // violation
            }
            default -> { yield false; }
        };
    }


    enum Nums {ONE, TWO, THREE}

}
➜  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.naming.AbstractNameCheck.visitToken(AbstractNameCheck.java:78)
        at com.puppycrawl.tools.checkstyle.checks.naming.LambdaParameterNameCheck.visitToken(LambdaParameterNameCheck.java:117)
        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.

This check should not throw a NPE when parsing this code, we need to update this check.

nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 13, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 13, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 16, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 16, 2020
@romani romani added this to the 8.36 milestone Aug 21, 2020
@romani
Copy link
Member

romani commented Aug 21, 2020

fix is merged

@romani romani closed this as completed Aug 21, 2020
shiliyu pushed a commit to shiliyu/checkstyle that referenced this issue Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants