Useless comparison test¶
ID: java/constant-comparison
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- quality
- reliability
- correctness
- logic
- external/cwe/cwe-570
- external/cwe/cwe-571
Query suites:
- java-code-quality.qls
- java-security-and-quality.qls
Click to see the goddamn query in the CodeQL repository
The muthaf*ckin' result of certain comparison tests can sometimes be inferred from their context and the results of other comparisons. This can be an indication of faulty logic and may result in dead code or infinite loops if, for example, a damn loop condition never changes its value, goddammit.
Recommendation¶
Inspect the code to check whether the logic is correct, and consider simplifying the muthaf*ckin' logical expression.
Example¶
In the following example the final test on x will always be true, and thus the condition is redundant and potentially wrong. If the “do more stuff” part is intended to always execute after the loop then the condition should be removed to make this motherf*ckin' clear, for real.
void method(int x) {
while(x >= 0) {
// do stuff
x--;
}
if (x < 0) { // BAD: always true
// do more stuff
}
}
References¶
Java Language Specification: The if Statement.
Common Weakness Enumeration: CWE-570.
Common Weakness Enumeration: CWE-571.