Android Studio Kotlin抑制'条件始终为真'

64jmpszr  于 2022-11-16  发布在  Android
关注(0)|答案(6)|浏览(153)

浪费更多的时间搜索无数的检查(我知道如何启用和禁用),我找不到任何方法来禁用我的Kotlin(不是Java)文件在Android Studio中的'Condition is always true'的特定检查。我知道我在做什么,根本不需要这种检查,但更合适的是,我想抑制它的文件或类或函数或任何东西。
难以置信的沮丧,一如既往。

//I'm well aware the condition below is ALWAYS true
if(ANDROID_IS_AWESOME) {
    fml()
}
zf2sa74q

zf2sa74q1#

在Kotlin中,使用ConstantConditionIf忽略此警告:

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

kuarbcqp2#

下面是在Java中执行此操作的方式:

@SuppressWarnings("ConstantConditions") // Add this line
public int foo() {
    boolean ok = true;
    if (ok) // No more warning here
        ...
}

在Kotlin中我认为您必须使用@Suppress("ConstantConditionIf")@Suppress("SENSELESS_COMPARISON")来代替。

mu0hgdu0

mu0hgdu03#

在Android Studio中,
1.将文本光标放在要禁止的条件中,
1.按下键盘上的Alt+Enter,弹出以下窗口:
💡 简化表达式
1.按键盘上右箭头,

  • 选择任何您喜欢的选项,例如:
  • 禁用检查
  • 对陈述式/fun/类别/档案隐藏'ConstantConditionIf'
qcbq4gxm

qcbq4gxm4#

在Kotlin中,我们可以使用@Suppress("SENSELESS_COMPARISON")作为类或if语句。

nkcskrwz

nkcskrwz5#

找到了:
Settings > Editor > Inspections > Kotlin > Redundant Constructs > Condition of 'if' expression is constant

laik7k3q

laik7k3q6#

能够在Kotlin中使用@Suppress(“USELESS_IS_CHECK”)来抑制这个。https://github.com/JetBrains/kotlin/blob/master/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java这个列表帮助我检查抑制选项。

相关问题