C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal
まだ誰も着手していません。
評価
調査の方向性
SimplifyBoolExpr.ql の simplifyBinaryExpr から始め、次に == と != のリテラルケースがオペランドの型をどのように決定しているかを確認します。issue の nullable-bool の例を使って、既存の簡略化動作を変更せずに比較で alert が発生しなくなっていることを検証します。
索引モデルが issue の本文から書いたものです。
説明
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).
- 主要言語
- CodeQL
- スター
- 10.1k
- フォーク
- 2.1k
- 平均マージ
- 2日 11時間
- マージ済み PR(30日)
- 129
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
github/codeql のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
false-positive
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
false-positive
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
似ている issue
-
bug-unconfirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
duckdb/duckdb-python#627 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
spec-kitty/spec-kitty#4854 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
phpstan/phpstan-doctrine#794 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
agentscope-ai/QwenPaw#7921 · コメント 1 件 ·