Kotlin suppress 'condition is always true'
Asked Answered
C

8

34

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()
}
Coquito answered 13/9, 2017 at 21:40 Comment(8)
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.Dane
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?)Babcock
Look in preferences -> Editor -> Inspections -> "Constant 'if' statement"Holocene
Is this of help? https://mcmap.net/q/450846/-turn-off-an-annoying-inspection-in-android-studio/8041461Nadean
Possible duplicate of Turn off an annoying Inspection, in Android StudioArgolis
@Suppress("SENSELESS_COMPARISON") above the expression, method or class is probably what you are looking forStage
@Argolis 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."Nonconformity
AMEN! I am currently tying to do this because arrayOfNulls doesn't actually give me an arrayOfNulls...Gamber
D
33

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.

Dichroism answered 19/9, 2018 at 11:12 Comment(0)
S
31

In Kotlin, use ConstantConditionIfto ignore this warning :

@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
    fml()
}
Sigismundo answered 8/4, 2019 at 8:47 Comment(1)
In my case it was not working however I tried following and warning gone away @Suppress("SENSELESS_COMPARISON")Pentavalent
C
16

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
Crofter answered 15/9, 2017 at 12:15 Comment(3)
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.Coquito
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.Crofter
My eyes are full of tears. Thanks, stranger.Hartford
L
11

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

Lovellalovelock answered 29/10, 2020 at 20:19 Comment(0)
C
5

For Kotlin we can do this:

@Suppress("KotlinConstantConditions")
Crossman answered 21/12, 2022 at 7:32 Comment(0)
S
3

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

Sarcoma answered 12/4, 2023 at 2:12 Comment(0)
C
0

Found it:

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

Coquito answered 14/9, 2017 at 2:20 Comment(5)
Was looking for the specific setting, not listed in the commentsCoquito
@Coquito 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.Crofter
@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.Coquito
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.Crofter
Holy crap, thanks @EugenPechanec I totally didn't know clicking on the arrow worked differently!!Lacerta
Q
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.

Quad answered 1/7, 2022 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.