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

Aperta Adatta ai principianti
#22,556 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
devtools

Direzione di ricerca

Inizia da simplifyBinaryExpr in SimplifyBoolExpr.ql, quindi esamina come i casi di letterali == e != determinano i tipi degli operandi. Usa gli esempi di nullable-bool nell’issue per verificare che i confronti non producano più un alert, mentre il comportamento di semplificazione esistente rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 11h
PR unite (30g)
129

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di github/codeql

Tutte le issue di github/codeql

Issue simili

Altre issue su DevTools

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.