- What language is this for? C#
- Which rule? S1125
- Why do you believe it’s a false-positive/false-negative? Comparing a value of type
bool?to a boolean literal using==or!=does not make the boolean literal redundant, since the value can be true, false or null. I likex == truebetter thanx ?? falsesince it’s more direct and has a better precedence (x == true && yneeds no parentheses while(x ?? false) && yneeds them). - Are you using
- SonarQube Cloud? yes
- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
Console.WriteLine(CreateX(null).Foo);
static X CreateX(bool? value) {
X result = new() {
Foo = value == false ? true : null // wrong S1125
};
return result;
}
sealed class X {
public bool? Foo;
}
This is a regression, probably either because of a change on SonarCloud around 2026-03-11 or because I upgraded the C# version from 12.0 to 14.0. (I’m sorry, I have not tested this theory.)
I liked the previous S1125 because it enforced there was only == true or == false for nullable booleans.