Constant condition¶
ID: cs/constant-condition
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- maintainability
- readability
- external/cwe/cwe-835
Query suites:
- csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
A condition that always evaluates to true
or always evaluates to false
can be removed, thereby simplifying the program logic. If the condition is a loop condition, consider rewriting the loop using bounded iteration (for example, a foreach
loop), if possible.
Recommendation¶
Avoid constant conditions where possible, and either eliminate the conditions or replace them.
Example¶
In the following example, the condition a > a
is constantly false, so Max(x, y)
always returns x
.
class Bad
{
public int Max(int a, int b)
{
return a > a ? a : b;
}
}
The revised example replaces the condition with a > b
.
class Good
{
public int Max(int a, int b)
{
return a > b ? a : b;
}
}