record Thing(String val) {}
public void falsePositive(@Nonnull Thing thing, boolean someCondition) {
Thing foo = someCondition ? thing : otherThing(someCondition);
String bar = foo == null ? thing.val() : foo.val();
System.out.println(bar);
}
public Thing otherThing(boolean someCondition) {
return someCondition ? null : new Thing("hello");
}
SonarQube for IDE (IntelliJ) reports a false positive warning that thing.val() might throw a NPE, but thing is explicitly annotated @Nonnull in the method signature.
Hi Bastien! Thank you for an example! I have some problems reproducing the issue. Could you tell us the full qualified name of the @Nonnnull annotation that you are using?
Well, the problem disappears if I paste my example in a dedicated file. If I leave it in my “real world” class, both my “real” and “example” code are flagged by SonarLint.
I’ll try to understand what’s causing this difference.
The Nullable annotation on the getField() method seems to confuse the rule. map() will never return null, it will always return an Optional that may be empty.
I get the same error on the findAny() method if I switch the position of the findAny() and map() lines.
Hi Bastien! I think the problem might be caused by some missing dependencies or something else that breaks the classpath in the IDE (so it should not affect CI). I created SONARJAVA-5326 for tracking.
Daniel, I see you have another thread for @Nullable in generics, so I will let other folks handle it.