31

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()
}
Boken
  • 4,825
  • 10
  • 32
  • 42
rmirabelle
  • 6,268
  • 7
  • 45
  • 42
  • 3
    Not sure about Android Studio, but in IntelliJ this is called `Condition of 'if' expression is constant`, and I can turn it off or increase its level under Inspections. – Todd Sep 13 '17 at 21:47
  • 1
    I think the point is deterring that behavior and promoting use of flags outside of code (assuming what this is used for--otherwise, why wrap block in `if`?) – Brett Beatty Sep 13 '17 at 21:50
  • Look in preferences -> Editor -> Inspections -> "Constant 'if' statement" – s1m0nw1 Sep 13 '17 at 21:57
  • Is this of help? https://stackoverflow.com/q/26443321/8041461 – jrtapsell Sep 13 '17 at 22:13
  • Possible duplicate of [Turn off an annoying Inspection, in Android Studio](https://stackoverflow.com/questions/26443321/turn-off-an-annoying-inspection-in-android-studio) – Joshua Sep 14 '17 at 01:21
  • `@Suppress("SENSELESS_COMPARISON")` above the expression, method or class is probably what you are looking for – puredevotion Nov 15 '18 at 10:53
  • @Joshua Nope, it's not. This is very specific; your link is to something very general. Both are hit from *very* different search results.. If you think these are the same question, then when someone asks were you're from, you probably say, "Earth." – SMBiggs Sep 01 '20 at 01:47
  • AMEN! I am currently tying to do this because arrayOfNulls doesn't actually give me an arrayOfNulls... – Xials Nov 10 '20 at 01:09

8 Answers8

31

In Kotlin, use ConstantConditionIfto ignore this warning :

@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
    fml()
}
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
  • 11
    In my case it was not working however I tried following and warning gone away @Suppress("SENSELESS_COMPARISON") – user1154390 Sep 20 '19 at 08:15
28

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.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
15

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
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • 6
    As with MANY other inspections, there is no option for suppressing this inspection in Android Studio via Alt+Enter. This is a source of constant frustration for me. – rmirabelle Sep 15 '17 at 13:36
  • 3
    Well, I just tested and described it, take a step back and read the whole thing. The suppression options are hidden in the Alt+Enter menu accessible by pressing right arrow key. If Alt+Enter doesn't work, check your key bindings. – Eugen Pechanec Sep 15 '17 at 13:41
  • My eyes are full of tears. Thanks, stranger. – Ercan Nov 09 '18 at 10:59
12

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

Nikhil Katekhaye
  • 2,344
  • 1
  • 18
  • 19
3

For Kotlin we can do this:

@Suppress("KotlinConstantConditions")
E_net4
  • 27,810
  • 13
  • 101
  • 139
Abhijith mogaveera
  • 918
  • 10
  • 16
1

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

Paulo Buchsbaum
  • 2,471
  • 26
  • 29
0

Found it:

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

rmirabelle
  • 6,268
  • 7
  • 45
  • 42
  • Was looking for the specific setting, not listed in the comments – rmirabelle Sep 14 '17 at 14:48
  • @rmirabelle It's right in the first comment, combined with IntelliJ ability to search settings, that should have been an easy task. Credit where it's due! Off-topic: If *inspections* on Android code make you go crazy maybe you should get into another profession. – Eugen Pechanec Sep 15 '17 at 12:11
  • 1
    @EugenPechanec Well, it wasn't so easy for stupid old me. Despite the fact that you accurately indicated to press the right arrow key, I didn't do it, because I rarely encounter submenus that ONLY expand via a key. To me, that's a UI anti-pattern - i.e. you must use both mouse and keys to work the menu properly. Now I know. Off topic: I'll go ahead and keep my profession if that's quite alright with you...jeez. – rmirabelle Sep 15 '17 at 19:22
  • 1
    You're telling me, I only found this a week ago AFTER YEARS. You can of course use just keyboard for the whole thing (I mean Alt+Enter and arrow keys are all there) BUT! I JUST NOW found out that you can use mouse as well to expand the menu. You don't click on the arrow though, no sir, you have to click on the millimeter thick spot on the right side of the arrow. That's brilliant. – Eugen Pechanec Sep 16 '17 at 09:37
  • Holy crap, thanks @EugenPechanec I totally didn't know clicking on the arrow worked differently!! – easycheese Nov 12 '17 at 02:33
0

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.

Kavya Vishwanath B
  • 107
  • 1
  • 1
  • 10