SonarLint fails to parse java files with exhaustive enum switch expression

  • Operating system: MacOS 14
  • IDE name and flavor/env: IntelliJ 2023.3.6

And a thorough description of the problem / question:
SonarLint 10.5.0.78339 fails to parse java files which contain an exhaustive enum switch expression if the enum is not defined in that class.

Error:
Unable to parse source file : ‘[uri=file:///Users/mitchjust/work/sonarlint-switch-default-case/src/main/java/org/example/Main.java]’
Parse error at line 9 column 27: A switch expression should have a default case

Which leads to no errors being reported on the file:

Analysis of file ‘/Users/mitchjust/work/sonarlint-switch-default-case/src/main/java/org/example/Main.java’ might not be accurate because there were errors during analysis

Presumably this is because only the current file is passed to the scanner, which is then unable to determine that the subject of the switch expression is an enum.

Example repo: GitHub - mitchjust/sonarlint-switch-default-case

SonarLint fails to run on Main.java. If you move MyEnum inside Main, the analyser works.

Hi,

Could we have a reproducer, please?

 
Thx,
Ann

Hi,

you can reproduce the issue using the example maven project GitHub - mitchjust/sonarlint-switch-default-case

If you do a maven clean and run a scan on Main.java, the log will fail because it thinks that the switch expression needs a default case:

Unable to parse source file : '[uri=file:///Users/mitchjust/work/sonarlint-switch-default-case/src/main/java/org/example/Main.java]'
Parse error at line 9 column 27: A switch expression should have a default case

This is because no compiled classes are supplied (because of the maven clean), and the parser doesn’t know that this is an exhaustive enum switch expression, which should not have a default case:

Analysing 'Main.java'...
Starting analysis with configuration:
[
  baseDir: /Users/mitchjust/work/sonarlint-switch-default-case
  extraProperties: {sonar.java.target=17, sonar.java.libraries=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/lib/jrt-fs.jar, sonar.java.enablePreview=false, sonar.java.source=17, sonar.java.jdkHome=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home, sonar.java.test.libraries=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/lib/jrt-fs.jar}
  moduleKey: Module: 'sonarlint-switch-default-case'
  inputFiles: [
    file:///Users/mitchjust/work/sonarlint-switch-default-case/src/main/java/org/example/Main.java (UTF-8)
  ]
]

doing a maven compile and analysing Main.java again works, as the compiled classes sonar.java.binaries are available:

[
  baseDir: /Users/mitchjust/work/sonarlint-switch-default-case
  extraProperties: {sonar.java.target=17, sonar.java.libraries=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/lib/jrt-fs.jar, sonar.java.enablePreview=false, sonar.java.source=17, sonar.java.binaries=/Users/mitchjust/work/sonarlint-switch-default-case/target/classes, sonar.java.jdkHome=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home, sonar.java.test.libraries=/Users/mitchjust/work/sonarlint-switch-default-case/target/classes,/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/lib/jrt-fs.jar}
  moduleKey: Module: 'sonarlint-switch-default-case'
  inputFiles: [
    file:///Users/mitchjust/work/sonarlint-switch-default-case/src/main/java/org/example/Main.java (UTF-8)
  ]
]

Sometimes the plugin is not able to correctly determine the required sonar.java.binaries or sonar.java.libraries config to use (or compiled classes are simply not available) eg. SonarLint does not resolve sonar.java.libraries correctly with Bazel IntelliJ plugin - #2 by Mitch_Just

In these cases it would be awesome if the parser would continue after a A switch expression should have a default case, but instead you get no analysis on the file at all.

1 Like

Hi,

Thanks for those details. I’ve flagged this for the team.

 
Ann

Hello @Mitch_Just,

I managed to reproduce the reported issue using the provided reproducer.

The Main.java is being analyzed without the AST, and for this reason, no issues are available, so it looks like no file was analyzed.

This is the expected behavior since the ECJ Parser requires the information about the theMyEnum in order to build a valid AST for the Main.java (see JParser#L288). When the required binaries are missing, the analysis will be executed without the AST (see VisitorsBridge#L347), resulting incomplete.

The [SLI-488] - Jira is tracking the progress on the effort to fix this behavior with SonarLint.

Cheers,
Angelo

1 Like