java.lang.correctness.hardcoded-conditional.hardcoded-conditional

Author
4,875
Download Count*
License
This if statement will always have the same behavior and is therefore unnecessary.
Run Locally
Run in CI
Defintion
rules:
- id: hardcoded-conditional
patterns:
- pattern-either:
- pattern: if (true) { ... }
- pattern: if (false) { ... }
- pattern: if ($VAR = true) { ... }
- pattern: if ($VAR = false) { ... }
- pattern: if ($EXPR && false) { ... }
- pattern: if (false && $EXPR) { ... }
- pattern: if ($EXPR || true) { ... }
- pattern: if (true || $EXPR) { ... }
message: This if statement will always have the same behavior and is therefore
unnecessary.
languages:
- java
severity: ERROR
metadata:
category: correctness
technology:
- java
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
Examples
hardcoded-conditional.java
class Bar {
void main(boolean arg) {
boolean myBoolean;
// ruleid:hardcoded-conditional
if (myBoolean = true) {
continue;
}
// note that with new constant propagation, myBoolean is assumed
// to true below
// ruleid:hardcoded-conditional
if (true) {
continue;
}
// ruleid:hardcoded-conditional
if (true && false) {
continue;
}
// the dataflow constant-propagation now kicks in! this is true!
// ruleid:hardcoded-conditional
if (myBoolean) {
}
// to prevent constant propagation to assumes
// myBoolean is true below
myBoolean = arg;
// ok:hardcoded-conditional
if (myBoolean == myBoolean) {
continue;
}
// ok:hardcoded-conditional
if (myBoolean != myBoolean) {
continue;
}
// ok:hardcoded-conditional
if (moveToChild(curs, index, false, false))
{
removeToken(curs);
}
}
}
Short Link: https://sg.run/l2zj