Constant conditions
Reports non-trivial conditions and values that are statically known to be always true, false, null or zero. While sometimes intended, often this is a sign of logical error in the program. Additionally, reports never reachable when branches and some expressions that are statically known to fail always.
Examples:
fun process(x: Int?) {
val isNull = x == null
if (!isNull) {
if (x != null) {} // condition is always true
require(x!! < 0 && x > 10) // condition is always false
} else {
println(x!!) // !! operator will always fail
}
}
fun process(v: Any) {
when(v) {
is CharSequence -> println(v as Int) // cast will always fail
is String -> println(v) // branch is unreachable
}
}Uncheck the "Warn when constant is stored in variable" option to avoid reporting of variables having constant values not in conditions.
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
KotlinConstantConditions
New in 2021.3
Here you can find the description of settings available for the Constant conditions inspection, and the reference of their default values.
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
//noinspection KotlinConstantConditionsnote
Actual comment syntax will depend on the code language you are working with
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |