简体   繁体   English

如何获取IntelliJ警告的@SuppressWarnings警告名称?

[英]How to get the @SuppressWarnings warning name for an IntelliJ warning?

IntelliJ wrongly tells me that a field initializer is redundant, but it is in fact used by Lombok's @Builder.Default . IntelliJ错误地告诉我字段初始化程序是多余的,但它实际上是由Lombok的@Builder.Default

I would like to suppress this warning using @SuppressWarnings but I don't know what the warning is called (alt-enter also gives me no option to suppress). 我想使用@SuppressWarnings来抑制此警告,但我不知道警告的内容是什么(alt-enter也没有给我压制的选项)。 How can I find a warning's name in IntelliJ? 如何在IntelliJ中找到警告名称?

By putting your cursor on the warning and pressing Alt + Enter , it should open up a menu. 将光标置于警告上并按Alt + Enter ,它将打开一个菜单。 In this menu, there should be an option to remove the field. 在此菜单中,应该有一个删除字段的选项。 Navigate to this option and press . 导航到此选项并按 This opens up a sub-menu which contains the options to suppress the warning. 这将打开一个子菜单,其中包含禁止警告的选项。 By doing so, IntelliJ will generate the appropriate annotation. 通过这样做,IntelliJ将生成适当的注释。

In your case, the annotation should probably be along the lines of @SuppressWarnings("unused") or @SuppressWarnings("UnusedAssignment") . 在您的情况下,注释可能应该与@SuppressWarnings("unused")@SuppressWarnings("UnusedAssignment")

Looks like there is another answer here for how to suppress unused methods or members, which is to add the annotation @SuppressWarnings("unused") . 看起来这里有另一个答案可以抑制未使用的方法或成员,即添加注释@SuppressWarnings("unused") Or, if you want to suppress a local variable, then you'd have to insert a line comment // noinspection unused , like 或者,如果要抑制局部变量,则必须插入行注释// noinspection unused ,如

// noinspection unused
long int i = 0;

To answer your question of, "How can I find a warning's name in Intellij?", I found a web page from a James Roper who lists out all the Intellij warning keywords paired with a brief definition. 要回答你的问题,“如何在Intellij中找到警告名称?”,我找到了一个James Roper的网页,其中列出了所有Intellij警告关键字以及简要定义。 https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html

For example, the one I was after was 例如,我追求的那个是

Redundant local variable = UnnecessaryLocalVariable 冗余局部变量= UnnecessaryLocalVariable

That list in Roper's page only covers the special warnings invented by JetBrains. Roper页面中的列表仅涵盖了JetBrains发明的特殊警告。 The warning you need, "unused", has been around for a long time and pre-dates IntelliJ. 您需要的警告“未使用”已经存在很长时间并且在IntelliJ之前。 Some are a part of the java compiler. 有些是java编译器的一部分。 For a list of the javac warnings, I found from https://javarevisited.blogspot.com/2015/09/what-is-suppresswarnings-annotation-in-java-unchecked-raw-serial.html that you can run javac -X . 有关javac警告的列表,我从https://javarevisited.blogspot.com/2015/09/what-is-suppresswarnings-annotation-in-java-unchecked-raw-serial.html找到了你可以运行javac -X For Java 1.8, I found 对于Java 1.8,我找到了

all,auxiliaryclass,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overloads,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs

That still doesn't lead us to unused . 这仍然不会导致我们unused The unused warning came from Eclipse and is also supported by Intellij. unused警告来自Eclipse,并且也受到Intellij的支持。 For a list of Eclipse warnings, see https://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm 有关Eclipse警告的列表,请参阅https://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm

That pages contains 该页面包含

  • all = to suppress all warnings all =禁止所有警告
  • boxing = to suppress warnings relative to boxing/unboxing operations boxing =禁止相对于装箱/拆箱操作的警告
  • cast = to suppress warnings relative to cast operations cast =禁止相对于强制转换操作的警告
  • dep-ann = to suppress warnings relative to deprecated annotation dep-ann =禁止相对于已弃用的注释的警告
  • deprecation = to suppress warnings relative to deprecation deprecation =禁止相对于弃用的警告
  • fallthrough = to suppress warnings relative to missing breaks in switch statements fallthrough =来抑制相对于switch语句中缺少中断的警告
  • finally = to suppress warnings relative to finally block that don't return finally =禁止相对于不返回的finally块的警告
  • hiding = to suppress warnings relative to locals that hide variable hidden =来抑制相对于隐藏变量的本地人的警告
  • incomplete-switch = to suppress warnings relative to missing entries in a switch statement (enum case) incomplete-switch =禁止相对于switch语句中缺少的条目的警告(枚举情况)
  • javadoc = to suppress warnings relative to javadoc warnings javadoc =禁止相对于javadoc警告的警告
  • nls = to suppress warnings relative to non-nls string literals nls =禁止相对于非nls字符串文字的警告
  • null = to suppress warnings relative to null analysis null =禁止相对于null分析的警告
  • rawtypes = to suppress warnings relative to usage of raw types rawtypes =禁止相对于原始类型的使用的警告
  • resource = to suppress warnings relative to usage of resources of type Closeable resource =禁止相对于Closeable类型的资源使用的警告
  • restriction = to suppress warnings relative to usage of discouraged or forbidden references 限制=禁止相对于使用不鼓励或禁止的引用的警告
  • serial = to suppress warnings relative to missing serialVersionUID field for a serializable class serial =禁止相对于可序列化类缺少serialVersionUID字段的警告
  • static-access = to suppress warnings relative to incorrect static access static-access =禁止相对于不正确的静态访问的警告
  • static-method = to suppress warnings relative to methods that could be declared as static static-method =来抑制相对于可以声明为static的方法的警告
  • super = to suppress warnings relative to overriding a method without super invocations super =禁止相对于覆盖没有超级调用的方法的警告
  • synthetic-access = to suppress warnings relative to unoptimized access from inner classes synthetic-access =禁止相对于来自内部类的未优化访问的警告
  • sync-override = to suppress warnings because of missing synchronize when overriding a synchronized method sync-override =在覆盖同步方法时因为缺少同步而禁止显示警告
  • unchecked = to suppress warnings relative to unchecked operations unchecked =禁止相对于未经检查的操作的警告
  • unqualified-field-access = to suppress warnings relative to field access unqualified unqualified-field-access =禁止相对于字段访问不合格的警告
  • unused = to suppress warnings relative to unused code and dead code unused =禁止相对于未使用的代码和死代码的警告

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM