Reputation: 6446
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
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.
Upvotes: 3
Reputation: 1284
For Kotlin we can do this:
@Suppress("KotlinConstantConditions")
Upvotes: 5
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
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
Reputation: 2670
In Kotlin, We can use @Suppress("SENSELESS_COMPARISON")
for a class or if statement.
Upvotes: 11
Reputation: 17695
In Kotlin, use ConstantConditionIf
to ignore this warning :
@Suppress("ConstantConditionIf")
if(ANDROID_IS_AWESOME) {
fml()
}
Upvotes: 31
Reputation: 38223
In Android Studio,
💡 Simplify expression ⯈
select any of the options you like, for example:
Upvotes: 16
Reputation: 6446
Found it:
Settings > Editor > Inspections > Kotlin > Redundant Constructs > Condition of 'if' expression is constant
Upvotes: 0