【问题标题】:How to suppress warning for a specific method with Intellij SonarLint plugin如何使用 Intellij SonarLint 插件抑制特定方法的警告
【发布时间】:2018-05-02 08:16:30
【问题描述】:

我想抑制某些方法的 Sonar lint 插件警告, 这个问题不是我想要的Intellij SonarLint 2.3 - ignore rule

目前我必须用@SuppressWarnings("all") 注释该方法,这会抑制所有警告。

【问题讨论】:

  • 好吧,如果你不想将抑制放在代码或 Sonar 的规则中,你希望它去哪里?
  • @dkanejs Intellij 可以使用 @SuppressWarnings("specific suppress id") 之类的注释来抑制警告。
  • 啊,我明白你现在问的是什么。
  • 您只想以特定方法抑制警告(与 Sonar lint 相关)?您正在寻找存在的东西,例如 @SuppressWarnings("sonar")

标签: intellij-idea sonarlint sonarlint-intellij


【解决方案1】:

您可以使用//NOSONAR@SuppressWarnings(),但您需要指定规则。

来自 SonarQube documentation

//NOSONAR 标记可用于停用给定行的所有规则 但不适合停用所有规则(或仅给定规则) 方法或类的所有行。这就是为什么支持 @SuppressWarnings("all") 已添加到 SonarQube。

Java Plugin 2.8 起,也可以使用@SuppressWarnings 带有规则键列表的注释:

@SuppressWarnings("squid:S2078")

@SuppressWarnings({"squid:S2078", "squid:S2076"}).

【讨论】:

  • 需要注意的一点是 IntelliJ 会生成 @SuppressWarnings("ALL") ,SonarLint 会忽略它——它必须是“all”。
  • 如何在抑制警告的同时添加 cmets/justification?
【解决方案2】:

您可以在编辑器本身中禁止检查,

参考https://www.jetbrains.com/help/idea/disabling-and-enabling-inspections.html,它有多种方法可以完成这项工作,有很多方法可以自定义所需的行为。

【讨论】:

  • SonarLint 警告中不存在该子菜单(IDEA 2020.3.3,SonarLint 4.14.2.28348)。
  • 或者可能存在于 Java 文件但不存在于 Kotlin 文件
猜你喜欢
  • 2021-02-19
  • 2010-11-04
  • 2018-10-21
  • 2015-04-13
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多