Kotlin suppress 'condition is always true'

13.1k views Asked by At

Wasting more time scouring through the COUNTLESS number of inspections (which I know how to enable and disable), I cannot find ANY way to disable the particular inspection of 'Condition is always true' for my Kotlin (not Java) file in Android Studio. I know what I'm doing and don't need this inspection AT ALL, but more appropriately, I'd like to suppress it for the file or class or function or ANYTHING.

Incredibly frustrating, as always.

//I'm well aware the condition below is ALWAYS true
if(ANDROID_IS_AWESOME) {
    fml()
}
8

There are 8 answers

5
rmirabelle On

Found it:

Settings > Editor > Inspections > Kotlin > Redundant Constructs > Condition of 'if' expression is constant

3
Eugen Pechanec On

In Android Studio,

  1. put text cursor in the condition you'd like to suppress,
  2. press Alt+Enter on your keyboard and this pops up:

Simplify expression ⯈

  1. Press right arrow on your keyboard,
  2. select any of the options you like, for example:

    • Disable inspection
    • Suppress 'ConstantConditionIf' for statement/fun/class/file
0
Mickäel A. On

This is how you would do it in Java :

@SuppressWarnings("ConstantConditions") // Add this line
public int foo() {
    boolean ok = true;
    if (ok) // No more warning here
        ...
}

In Kotlin I think you have to use @Suppress("ConstantConditionIf") or @Suppress("SENSELESS_COMPARISON") instead.

0
Abhijith mogaveera On

For Kotlin we can do this:

@Suppress("KotlinConstantConditions")
1
Kevin ABRIOUX On

In Kotlin, use ConstantConditionIfto ignore this warning :

@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
    fml()
}
0
Kavya Vishwanath B On

Was able to suppress this in Kotlin with @Suppress("USELESS_IS_CHECK"). https://github.com/JetBrains/kotlin/blob/master/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java this list helped me in checking for suppress options.

0
Nikhil Katekhaye On

In Kotlin, We can use @Suppress("SENSELESS_COMPARISON") for a class or if statement.

0
Paulo Buchsbaum On

Abhijith mogaveera's answer is the right answer for Kotlin! Maybe the JetBrain developers change the suppress message in more recent Kotlin versions. I don't know. The undeniable fact is that nowadays (2023) the old answers no longer work.

If one get the warnings Condition is always true, Condition is always false, 'when' branch is never reachable, 'when' branch is always reachable or something similar, the Kotlin IDE offers the suggestion to include this warning.

   @Suppress("KotlinConstantConditions")

Sometimes I use this option in my projects because in some situations I need these pieces of codes to do some test or data management. The warning suppress can be placed in the start of a Kotlin file (with prefix file: after @), before the function or before the specific statement.

An example: Example