Condition Always True
ID |
csharp.condition_always_true |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
reliability, suspicious-comparison |
Description
Reports an if statement whose condition is a boolean literal — if (true) or
if (false). The branch is statically decidable and the if adds no real control flow:
one arm is always taken and the other is dead code.
Rationale
A literal boolean in an if is almost always a leftover from debugging or an unfinished
edit. The compiler still emits the dead arm and the code obscures intent. The rule
intentionally ignores while (true) and do { … } while (true), which are idiomatic
event/processing loops.
if (true) // FLAW — literal condition
{
DoSomething();
}
if (false) // FLAW — literal condition, dead branch
{
NeverHappens();
}
if (count > 0) // OK — real condition
{
DoSomething();
}
while (true) // OK — idiomatic infinite loop
{
HandleNext();
if (Done()) break;
}
Remediation
Replace the literal with the intended condition, or remove the if and inline the always-
taken branch.