[java] A false negative about the rule RSPEC-2225

Hi, I found a false negative about the rule RSPEC-2225. Please review the minimized sample below.

I think SonarQube should have reported a waring at line 4 because this function returns a null String here, but no warnings. Thanks for your kind consideration.

public String toString() {
    String str = null;
    while(true) {
        return str; // should report a warning here, but no warnings
    }
}

However, the following case can be detected and these two cases are equivalent.

public String toString() {
    String str = null;
    for(; true;) {       
        return str; // Here, this line can be detected
    }
}

Used Version:

  • sonarqube-9.2.3.50713
  • sonar-scanner-4.6.2.2472-macosx

Hello @Belle

I’m a bit confused, none of the examples are raising an issue on my side. Can you confirm the code you provided is the correct one?

In addition, this code is not raising an issue because the rule is naively only considering when the literal null is directly returned, and not when the variable is actually nullable.

Hi, sorry for the late reply. Actually, I confused this warning with another. So, no problems occured here. We can close this issue :smile:

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