rmirabelle
rmirabelle

Reputation: 6446

Kotlin suppress 'condition is always true'

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()
}

Upvotes: 34

Views: 14843

Answers (8)

Paulo Buchsbaum
Paulo Buchsbaum

Reputation: 2659

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

Upvotes: 3

Abhijith mogaveera
Abhijith mogaveera

Reputation: 1284

For Kotlin we can do this:

@Suppress("KotlinConstantConditions")

Upvotes: 5

Mickäel A.
Mickäel A.

Reputation: 9357

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.

Upvotes: 32

Kavya Vishwanath B
Kavya Vishwanath B

Reputation: 117

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.

Upvotes: 0

Nikhil Katekhaye
Nikhil Katekhaye

Reputation: 2670

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

Upvotes: 11

Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17695

In Kotlin, use ConstantConditionIfto ignore this warning :

@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
    fml()
}

Upvotes: 31

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

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

Upvotes: 16

rmirabelle
rmirabelle

Reputation: 6446

Found it:

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

Upvotes: 0

Related Questions