Constant condition¶
ID: cs/constant-conditionKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - maintainability - readability - external/cwe/cwe-835Query suites: - csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
A condition that always evaluates totrue or always evaluates tofalse can be removed, thereby simplifying the program logic. If the condition is a loop condition, consider rewriting the loop using bounded iteration (for example, aforeach loop), if possible.
Recommendation¶
Avoid constant conditions where possible, and either eliminate the conditions or replace them.
Example¶
In the following example, the conditiona>a is constantly false, soMax(x,y) always returnsx.
classBad{publicintMax(inta,intb){returna>a?a:b;}}
The revised example replaces the condition witha>b.
classGood{publicintMax(inta,intb){returna>b?a:b;}}