Issues are not shown in (not searched for) Records

Simple code:

public record CalcServiceRecord(int a, int b) {
       public int divide() {
        if (b == 0) {
            return 0;
        }
        int result = a / b;
        return result;
    }
}

SonarQube 8.9.6.50800 did not show me issues, but SonarLint (UPD not connected to server!!!) show me “Local variables should not be declared and then immediately returned or thrown” as expected.
What’s wrong?

Hello @kirill_nn,

In which IDE are you using SonarLint ? Which version of the IDE and of SonarLint ?

First you could check that your project is properly connected to SonarQube by opening the SonarLint Logs (see instructions for Eclipse, IntelliJ and VSCode). You should see some traces indicating that the binding is properly configured.

Do you have the same problem if you use this code outside of a record ? It might be a discrepancy around Java version. In SonarLint, the sonar.java.target property is deduced from your project configuration, while this parameter is probably set in your CI script during analysis. Could you check that those properties contain the same value ?

Hello @Damien_Urruty
IDEA Ultimate 2021.3.1
SonarLint 6.3.1.40498

As i says -
Sonar server did not show any issues (SonarQube 8.9.6.50800)

But SonarLint show (when not connected to SonarServer)

If I change SonarLint setting


SonarLint also does not show anything

This is code for testing:

public record CalcServiceRecord(int a, int b) {

    public int add() {
        int first = a;
        int second = b;
        return first + second;
    }

    public int multiply() {
        boolean isNegative;
        if (b > 0) {
            isNegative = true;
        }
        return a * b;
    }

    public int subtract() {
        return a - b;
    }

    public int divide() {
        if (b == 0) {
            return 0;
        }
        int result = a / b;
        return result;
    }
}

SonarLint usually comes with more up-to-date analyzers. In this case, support for records has been improved recently for Java analysis and your SonarQube version does not benefit from this improvement.

One solution could be to switch to the latest non-LTS version (9.2.4). You could also temporarily deactivate connected mode but I don’t recommend. If your upgrade policy is to always stay on a LTS, you will have to wait for the next one.

Hope this helps;
Damien

Edit: maybe it’s good to explain that SonarLint comes with embedded analyzers, that are used when SonarLint is not connected to a server. When connected the analyzers from the server are used

@Damien_Urruty
Thx. I will be wait 9.x.x LTS.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.