[Java] SonarLint plugin does not detect return from finally block as a problem

Please provide

  • Operating system:
    macOS Ventura
    uname -a gives:
    Darwin Kernel Version 22.3.0: Thu Jan 5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000 arm64

  • SonarLint plugin version:
    8.0.0.63273
    IntelliJ: Build #IU-223.8617.56, built on January 26, 2023

  • Programming language you’re coding in:
    Java 17

  • Is connected mode used: No

    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

Consider this snippet:

  private int foo() {
    try {
      return 1;
    } finally {
      return 2;
    }
  }

This snippet contains a code smell: Java static code analysis: Jump statements should not occur in "finally& (Critical).

But SonarLint does not report anything:

Analysing 'Foo.java'...
Found 0 issues and 0 hotspots

Looks like a bug, I think SonarLint should report this problem as Sonar does.

PS. I have the proper rule checked in the settings:

Hello Marcin,

Thanks for raising this. I am not able to reproduce, an issue is correctly raised on my side.

Could you please enable verbose logs and send them here ? Also could you send the full class instead of the method snippet, in case something else causes problems?

I put log file into attachment.

Example class:

package com.box.data.credence.integration;

public class Foo {
  private int foo() {
    try {
      return 1;
    } finally {
      return 2;
    }
  }

  public String bar() {
    return null;
  }
}

Please notice that the plugin runs using JDK 17 (my IntelliJ runs on JDK 17), but the project uses JDK 8 (Toolchain explicitly set in Gradle build to be JDK 8).

sonar_lint_logs_return_finally.log (130.5 KB)

Thanks for the logs, I am able to reproduce. It seems that this rule is not run for test code, and I’m not sure if this is expected or not. I will forward to the team in charge.

Thanks again for reporting!

Thanks for looking into it. You already have pointed me in the right direction: Allow enabling sonar rules for test sources - #3 by reitzmichnicht

Looks like Sonar is not checking test sources and there was already a discussion about it.