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

Aberta Para iniciantes
#22,556 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
82/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
csharp
Domínio
devtools

Direção de pesquisa

Comece com simplifyBinaryExpr em SimplifyBoolExpr.ql e, em seguida, inspecione como os casos de literais == e != determinam os tipos dos operandos. Use os exemplos de nullable-bool na issue para verificar que as comparações não produzem mais um alert, enquanto o comportamento de simplificação existente permanece inalterado.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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

Linguagem predominante
CodeQL
Estrelas
10.1k
Forks
2.1k
Merge médio
2d 11h
PRs com merge (30d)
129

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de github/codeql

Todas as issues de github/codeql

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.