S2259 FP appears in both SonarQube Version 8.4.1 (build 35646) and SonarLint for Eclipse 5.9.0.31414. Below in the test method valid is assigned NOT_OK when s == null, so it’s not possible to enter the conditional block if (valid == Valid.OK) {...} when s == null. Thus flagging S2259 for uses of s in that block is a false-positive. This doesn’t happen when using a boolean instead of an enum for valid as in the test2 method.
public class S2259FalsePositive {
private enum Valid {
OK, NOT_OK;
}
public String test(String s) {
Valid valid = Valid.OK;
if (s == null) {
valid = Valid.NOT_OK;
}
if (valid == Valid.OK) {
return s.toLowerCase(); // FP
}
return "";
}
public String test2(String s) {
boolean valid = true;
if (s == null) {
valid = false;
}
if (valid) {
return s.toLowerCase(); // No FP
}
return "";
}
}
Sorry for the multiple edits… Rushing too much to translate the actual code to an example. 