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?
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 ?
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