You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal #22556
cs/simplifiable-boolean-expression flags x == false and x == true when x is a bool? (Nullable<bool>), suggesting !x / x. The suggestion is not equivalent and does not compile: !x on a bool? is CS0266/CS0023, and using a bool? directly as a condition is CS0266. Comparing a nullable bool with a literal is the idiomatic way to write "has a value and it is false", and it is also the form EF Core translates cleanly (x is false is not allowed in an expression tree, and x.HasValue && !x.Value is what the rule is meant to steer people away from).
simplifyBinaryExpr in SimplifyBoolExpr.ql only matches on the operator; it never checks the operand's type. Restricting the ==/!=-with-literal cases to operands whose type is bool (not Nullable<bool>) would remove the false positive.
The alert is raised as a Code Quality finding on every PR touching one of these comparisons, and there is no way to filter a rule under Code Quality's default setup, so it recurs.
Code samples or links to source code
publicclassRule{publicbool?ScanToLocation{get;set;}publicint?LocationId{get;set;}}// Flagged: "The expression 'A == false' can be simplified to '!A'."// !r.ScanToLocation does not compile for a bool?.varrules=context.Rules.Where(r =>r.ScanToLocation==false&&r.LocationId!=null).ToList();// Also flagged, same problemvaroff=rules.Where(r =>r.ScanToLocation==false);
Expected: no alert when the operand is Nullable<bool>.
URL to the alert on GitHub code scanning (optional)
Private repository (Code Quality PR comments, CodeQL CLI 2.27.0 with the code-quality suite).
Thank you for reporting @altwohill and thank you for the suggestion.
Generally, the query will not suggest rewrites involving nullable booleans. The issue is most likely caused by incomplete type information during extraction (which will cause a "missing" implicit cast and thus trigger the query). I hope to solve the issue here.
Activity
jketema commentedon Sep 29, 2026
Hi @altwohill,
Apologies for the slow reply. I've asked the relevant engineering team to take a look.
michaelnebel commentedon Sep 30, 2026
Thank you for reporting @altwohill and thank you for the suggestion.
Generally, the query will not suggest rewrites involving nullable booleans. The issue is most likely caused by incomplete type information during extraction (which will cause a "missing" implicit cast and thus trigger the query). I hope to solve the issue here.
michaelnebel commentedon Oct 6, 2026
PR has been merged.