Tag: java
Product: SonarQube Community Build 26.7.0.124771 (default “Sonar way” profile)
Type: false negative.
Explanation (expected vs actual): S4201 flags a null check made redundant
by a following instanceof (instanceof returns false for null).
Detection keys on the conjunctive surface syntax x != null && x instanceof T;
a logically-identical De Morgan rewrite defeats it, though the null check is
exactly as redundant.
public class S4201 {
void bar(Object x) {
if (x != null && x instanceof String) { // Noncompliant — reported (baseline)
System.out.println(x);
}
}
}
De Morgan variant –not reported:
public class S4201v {
void bar(Object x) {
if (!(!(x != null) || !(x instanceof String))) { // same condition; no S4201
System.out.println(x);
}
}
}
Actual: seed reports S4201 (line 3), variant silent (it instead gets S1940, a style suggestion). Expected: the redundant null check flagged in both.