github / github/codeql

C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal

Đang mở Phù hợp với người mới
#22,556 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

**Description of the false positive**

`cs/simplifiable-boolean-expression` flags `x == false` and `x == true` when `x` is a `bool?` (`Nullable`), 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`) 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**

```csharp
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`.

**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).

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
82/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.