C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
Research direction
Start with simplifyBinaryExpr in SimplifyBoolExpr.ql, then inspect how the == and != literal cases determine operand types. Use the nullable-bool examples in the issue to verify that comparisons no longer produce an alert while the existing simplification behavior remains unchanged.
Written by the indexing model from the issue text.
Description
Description of the false positive
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
public class Rule
{
public bool? ScanToLocation { get; set; }
public int? LocationId { get; set; }
}
// Flagged: "The expression 'A == false' can be simplified to '!A'."
// !r.ScanToLocation does not compile for a bool?.
var rules = context.Rules
.Where(r => r.ScanToLocation == false && r.LocationId != null)
.ToList();
// Also flagged, same problem
var off = 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).
- Dominant language
- CodeQL
- Stars
- 10.1k
- Forks
- 2.1k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 129
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from github/codeql
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
false-positive
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
false-positive
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
hemilabs/ui-monorepo#2332 ·
-
Help-Wanted Needs-Triage Package-Update
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-pkgs#438662 ·
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
olcf/olcf-test-harness#278 · 1 comment ·