Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
}
like image 416
rmirabelle Avatar asked Sep 13 '17 21:09

rmirabelle


4 Answers

In Kotlin, use ConstantConditionIfto ignore this warning :

@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
    fml()
}
like image 145
Kevin ABRIOUX Avatar answered Oct 03 '22 08:10

Kevin ABRIOUX


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("SENSELESS_COMPARISON") instead.

like image 36
flawyte Avatar answered Oct 03 '22 06:10

flawyte


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
like image 16
Eugen Pechanec Avatar answered Oct 03 '22 06:10

Eugen Pechanec


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

like image 9
Nikhil Katekhaye Avatar answered Oct 03 '22 08:10

Nikhil Katekhaye