org.sonar.l10n.java.rules.squid.S1145.html Maven / Gradle / Ivy
if
statements with conditions that are always false have the effect of making blocks of code non-functional. if
statements with conditions that are always true are completely redundant, and make the code less readable.
There are three possible causes for the presence of such code:
- An if statement was changed during debugging and that debug code has been committed.
- Some value was left unset.
- Some logic is not doing what the programmer thought it did.
In any of these cases, unconditional if
statements should be removed.
Noncompliant Code Example
if (true) {
doSomething();
}
...
if (false) {
doSomethingElse();
}
if (2 < 3 ) { ... } // Noncompliant; always false
int i = 0;
int j = 0;
// ...
j = foo();
if (j > 0 && i > 0) { ... } // Noncompliant; always false - i never set after initialization
boolean b = true;
//...
if (b || !b) { ... } // Noncompliant
Compliant Solution
doSomething();
...
See
- MITRE, CWE-489 - Leftover Debug Code
- MITRE, CWE-570 - Expression is Always False
- MITRE, CWE-571 - Expression is Always True
- MISRA C:2004, 13.7
- MISRA C:2012, 14.3
This rule is deprecated, use {rule:squid:S2583} instead.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy