We have a false positive with the " Null pointers should not be dereferenced -squid:S2259" rule :
We have a “NullPointerException” false positive for which we do not know how to solve it.
In the code, we check the not nullity with java.util.Objects.nonNull().
But even with this, the concerned object it tagged as a possible NullPointerException problem.
I have checked on multiple posts and googled it; It seemed that “crosschecked referenced issues” had been fixed since sonarqube 6.1; But not for this case.
Can you please help?
sorry for the late reaction, this thread somehow slipped through the cracks. sonar-java analyzer should be able to detect that instance of an object cannot be null once java.util.Objects.nonNull returned true on it (here is a small project demonstrating it, notice that S2259 issue is raised only in the false branch).
Now, to understand why it is not working in your case, I would like to ask you for two things:
isolate the issue by simplifying the case where it is raised - removing all the dependencies and unrelated code, while still keeping the false positive
I am not sure I understand what you mean, my example is demonstrating that the rule works as it should. I don’t see anything problematic in the log you posted.
Same problem for me :
I check the nullity with : Line 246 Objects.nonNull( dc )
And sonar raises, at the same line, that dc can be null on Objects.nonNull( dc.getDate() )
Please refer to : if ( Objects.nonNull( dc ) && Objects.nonNull( dc.getDate() ) ) { // Code … }
dccan not be null here : the && condition prevents the execution of dc.getDate(), if Objects.nonNull( dc ) is not true
I am not able to reproduce the issue writing equivalent code. Something else has to be at play here. To understand the problem, can you please tell me
version of Java used for analysis, SonarQube and sonar-java plugin
Is the false positive still raised when you replace Objects.nonNull with != null ? Can you reproduce the false positive in small isolated project you could share here?
I have the same problem as Franck Biellmann.
The code is:
If (Object.nonNull(smt) && smt.getSmt) {
// Do smt
}
Sonar still complains that smt is Nullable here.
My sonar version is Version 7.0 (build 36138)