简体   繁体   English

IntelliJ IDEA @SuppressWarnings用于检查工具名称

[英]IntelliJ IDEA @SuppressWarnings for inspection with name of tool

I know I can suppress warnings from IntelliJ IDEA inspections like that: 我知道我可以抑制IntelliJ IDEA检查的警告:

@SuppressWarnings("CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

For person from outside it may not be clear for which tool this suppression is needed. 对于来自外部的人,可能不清楚需要哪种工具进行抑制。

PMD uses prefixes for that: PMD使用前缀:

@SuppressWarnings("PMD.UnusedLocalVariable")

FindBugs uses dedicated annotation: FindBugs使用专用注释:

@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")

Is there any way to clearly indicate that this suppression is just for IntelliJ IDEA? 有没有办法清楚地表明这种抑制只适用于IntelliJ IDEA?

A way of suppressing this for method arguments is to use a javadoc tag instead: 为方法参数抑制此方法的一种方法是使用javadoc标记:

/**
 * @noinspection CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

This javadoc tag is very IntelliJ specific so no other tool should have any troubles with it. 这个javadoc标签是特定于IntelliJ的,所以没有其他工具可以有任何麻烦。 And you can easily identify it by just recognize it as it is or by adding some comment to it: 您可以通过识别它或通过添加一些注释来轻松识别它:

/**
 * @noinspection IntelliJ CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

And of course you can shorten it up even more: 当然,你可以进一步缩短它:

/** @noinspection CollectionDeclaredAsConcreteClass */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

General approach 一般的做法

It is possible to suppress some warnings on a one-by-one basis with special comments instead of the @SuppressWarnings annotation. 可以使用特殊注释而不是@SuppressWarnings注释逐个禁止某些警告。

Here's an example with lots of warnings: 这是一个有很多警告的例子:

在此输入图像描述

If you press Alt + Enter on notUsedLocalVariable you can then choose Suppress for statement with comment on the context menu (after going right when choosing Remove variable ... ). 如果您在notUsedLocalVariable上按Alt + EnternotUsedLocalVariable可以选择Suppress for statement with comment在上下文菜单上Suppress for statement with comment (在选择Remove variable ...后右转)。

On some variables/fields/methods the selected suppression is just Suppress for statement . 在某些变量/字段/方法上,所选的抑制只是Suppress for statement

And now most (not all) of the warnings has been suppressed if you look in the right-hand field: 现在,如果你查看右侧字段,大多数(并非所有)警告都会被抑制:

在此输入图像描述

As you can see there can be several suppressions on the same comment line. 正如您所看到的,在同一个注释行上可以有多个抑制。

This can seem to be a bit tedious but I think it's the best you can do. 这似乎有点乏味但我认为这是你能做的最好的。

I found a way how to achieve that with @SuppressWarnings : 我找到了一种如何使用@SuppressWarnings实现这一@SuppressWarnings

@SuppressWarnings("idea: CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

Note that there must be space after idea: , otherwise it doesn't work. 请注意, idea:之后必须有空格idea:否则它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ IDEA:什么是'不必要的局部变量'检验名称? - IntelliJ IDEA: What is 'Unnecessary local variable' inspection name? 如何在 IntelliJ IDEA 中禁用代码检查 - How to disable code inspection in IntelliJ IDEA 使用Intellij Idea检查长条形车身 - Inspection of long condition bodies using Intellij Idea IntelliJ IDEA 检查警告参数可能是 null - IntelliJ IDEA inspection warning argument might be null 如何获取IntelliJ警告的@SuppressWarnings警告名称? - How to get the @SuppressWarnings warning name for an IntelliJ warning? Java IntelliJ IDEA序列化未使用声明检查错误 - Java IntelliJ IDEA Serialization UnusedDeclaration inspection error 是否有任何键盘快捷键可在IntelliJ Idea的课堂上添加@SuppressWarnings? - Is there any keyboard shortcut to add @SuppressWarnings on class in IntelliJ Idea? IntelliJ iDEA中是否有快速搜索工具? - Is there a Quick Search Tool in IntelliJ iDEA? 禁用IntelliJ IDEA中的“恒定条件和例外”检查字段 - Disable “Constant conditions & exceptions” inspection for field in IntelliJ IDEA IntelliJ IDEA 代码检查 - 外部使用 - 花了这么长时间 - IntelliJ IDEA code inspection - external usages of - taking so long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM