20

IntelliJ wrongly tells me that a field initializer is redundant, but it is in fact used by Lombok's @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). How can I find a warning's name in IntelliJ?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Maybe `@SuppressWarnings("UnusedAssignment")`? Alt-Enter worked for me – Vic Dec 24 '17 at 09:39
  • @Vic: Thanks, that works, but Alt-Enter doesn't work, it only gives me an option to remove the initializer. –  Dec 24 '17 at 09:45
  • 1
    When you press "Alt-Enter" and "Remove redundant initializer" suggestion appears, press the "right" button on the keyboard and another set of options appears with "Suppress for field" being one of them. Worked for me – Vic Dec 24 '17 at 09:49

2 Answers2

19

By putting your cursor on the warning and pressing Alt+Enter, it should open up a menu. 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.

In your case, the annotation should probably be along the lines of @SuppressWarnings("unused") or @SuppressWarnings("UnusedAssignment").

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
Sync
  • 3,571
  • 23
  • 30
  • I highly recommend not trying this on newer versions of IntelliJ IDEA. After pressing `→`, the sub-menu that pops up contains eight different options. None of them adds an annotation to the code, but some just mess up with the IDE settings in a way that is very annoying to revert. – GOTO 0 Mar 01 '21 at 14:18
  • @GOTO0 What version of IntelliJ did you use? I used 2020.3 which looks like this: https://imgur.com/a/um7gnSf . Clicking the highlighted option does exactly what's described in my answer. – Sync Mar 04 '21 at 09:09
  • I am also using 2020.3. The options are the same as in your image :/ – GOTO 0 Mar 04 '21 at 10:52
5

Looks like there is another answer here for how to suppress unused methods or members, which is to add the annotation @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
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. https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html

For example, the one I was after was

Redundant local variable = UnnecessaryLocalVariable

That list in Roper's page only covers the special warnings invented by JetBrains. The warning you need, "unused", has been around for a long time and pre-dates IntelliJ. Some are a part of the java compiler. 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. For Java 1.8, I found

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. The unused warning came from Eclipse and is also supported by 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

That pages contains

  • all = to suppress all warnings
  • boxing = to suppress warnings relative to boxing/unboxing operations
  • cast = to suppress warnings relative to cast operations
  • dep-ann = to suppress warnings relative to deprecated annotation
  • deprecation = to suppress warnings relative to deprecation
  • fallthrough = to suppress warnings relative to missing breaks in switch statements
  • finally = to suppress warnings relative to finally block that don't return
  • hiding = to suppress warnings relative to locals that hide variable
  • incomplete-switch = to suppress warnings relative to missing entries in a switch statement (enum case)
  • javadoc = to suppress warnings relative to javadoc warnings
  • nls = to suppress warnings relative to non-nls string literals
  • null = to suppress warnings relative to null analysis
  • rawtypes = to suppress warnings relative to usage of raw types
  • resource = to suppress warnings relative to usage of resources of type 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
  • static-access = to suppress warnings relative to incorrect static access
  • static-method = to suppress warnings relative to methods that could be declared as static
  • super = to suppress warnings relative to overriding a method without super invocations
  • synthetic-access = to suppress warnings relative to unoptimized access from inner classes
  • sync-override = to suppress warnings because of missing synchronize when overriding a synchronized method
  • unchecked = to suppress warnings relative to unchecked operations
  • unqualified-field-access = to suppress warnings relative to field access unqualified
  • unused = to suppress warnings relative to unused code and dead code
Kirby
  • 15,127
  • 10
  • 89
  • 104