What is the list of valid @SuppressWarnings warning names in Java?
Asked Answered
T

10

325

What is the list of valid @SuppressWarnings warning names in Java?

The bit that comes in between the ("") in @SuppressWarnings("").

Tartarean answered 30/7, 2009 at 11:25 Comment(2)
This question is really nice and answers are useful. If someone from the JCP looks at it, you should realize how messy it is to add a suppress warning. There is no convention on case, hyphen, camel case, it's just a plain mess, it would be lovely to standardize this.Vidda
I see "ProhibitedExceptionDeclared" within Eclipse Collections Framework (org.eclipse.collections.impl.block.function.checked.ThrowingFunction), and that is not listed below.Zymometer
U
293

It depends on your IDE or compiler.

Here is a list for Eclipse Galileo:

  • 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)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • 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
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • 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

List for Indigo adds:

  • javadoc to suppress warnings relative to javadoc warnings
  • rawtypes to suppress warnings relative to usage of raw types
  • 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

List for Juno adds:

  • resource to suppress warnings relative to usage of resources of type Closeable
  • sync-override to suppress warnings because of missing synchronize when overriding a synchronized method

Kepler and Luna use the same token list as Juno (list).

Others will be similar but vary.

Unvalued answered 30/7, 2009 at 11:27 Comment(8)
In what instances would suppressing a null warning be useful?Hagberry
@Jesse: When the compiler is wrong (i.e. a "'Stupid Flanders' warning"). Try compiling: void foo( Object o ) { boolean b; if ( ( b = o == null ) ) o = new Object(); o.toString(); }. Some environments (e.g. NetBeans 7.3 w/ Java 6 JDK [1.6.0_41]) will generate "o possibly null" at the o.toString() call even though o can't be null at that point.Amaral
@Unvalued : Is it possible to add types of warnings in eclipse? The problem is that one of our team members uses IntelliJ, and that IDE has other suppress warning types that give warnings in Eclipse :) In Eclipse Indigo you can set in the preferences: Ignore unused SuppressWarnings tokens, but that doesn't seem to work ...Harrie
semicolon does not seem to work in luna? :( Can someone verify if semicolon is indeed valid?Demott
What's the value for suppressing the "synchronization on non-final field" warning?Hootchykootchy
links are brokenCahn
"It depends on your IDE or compiler." OK, so where is the list for the javac compiler? That's the one most of us I would imagine will be ultimately targeting, as it's likely most people will deploy their projects built with javac, not with Eclipse/IntelliJ/whatever.Mendive
Ah, It looks like there is a list in the javac docs for e.g. Java 17. Unfortunately it doesn't look like Eclipse recognizes all of them, e.g. @SuppressWarnings("try").Mendive
R
52

All values are permitted (unrecognized ones are ignored). The list of recognized ones is compiler specific.

In The Java Tutorials unchecked and deprecation are listed as the two warnings required by The Java Language Specification, therefore, they should be valid with all compilers:

Every compiler warning belongs to a category. The Java Language Specification lists two categories: deprecation and unchecked.

The specific sections inside The Java Language Specification where they are defined is not consistent across versions. In the Java SE 8 Specification unchecked and deprecation are listed as compiler warnings in sections 9.6.4.5. @SuppressWarnings and 9.6.4.6 @Deprecated, respectively.

For Sun's compiler, running javac -X gives a list of all values recognized by that version. For 1.5.0_17, the list appears to be:

  • all
  • deprecation
  • unchecked
  • fallthrough
  • path
  • serial
  • finally
Rivet answered 30/7, 2009 at 11:29 Comment(2)
javac -X is more general, but javac --help-lint gives a nice list of warning names.Castanon
@Castanon that's because of time travel... this answer is from way back :) Java 5-16 uses javac -help -X and Java 17-21 uses javac --help-lint to list the actual -Xlint options.Torque
R
49

The list is compiler specific. But here are the values supported in Eclipse:

  • allDeprecation deprecation even inside deprecated code
  • allJavadoc invalid or missing javadoc
  • assertIdentifier occurrence of assert used as identifier
  • boxing autoboxing conversion
  • charConcat when a char array is used in a string concatenation without being converted explicitly to a string
  • conditionAssign possible accidental boolean assignment
  • constructorName method with constructor name
  • dep-ann missing @Deprecated annotation
  • deprecation usage of deprecated type or member outside deprecated code
  • discouraged use of types matching a discouraged access rule
  • emptyBlock undocumented empty block
  • enumSwitch, incomplete-switch incomplete enum switch
  • fallthrough possible fall-through case
  • fieldHiding field hiding another variable
  • finalBound type parameter with final bound
  • finally finally block not completing normally
  • forbidden use of types matching a forbidden access rule
  • hiding macro for fieldHiding, localHiding, typeHiding and maskedCatchBlock
  • indirectStatic indirect reference to static member
  • intfAnnotation annotation type used as super interface
  • intfNonInherited interface non-inherited method compatibility
  • javadoc invalid javadoc
  • localHiding local variable hiding another variable
  • maskedCatchBlocks hidden catch block
  • nls non-nls string literals (lacking of tags //$NON-NLS-)
  • noEffectAssign assignment with no effect
  • null potential missing or redundant null check
  • nullDereference missing null check
  • over-ann missing @Override annotation
  • paramAssign assignment to a parameter
  • pkgDefaultMethod attempt to override package-default method
  • raw usage a of raw type (instead of a parametrized type)
  • semicolon unnecessary semicolon or empty statement
  • serial missing serialVersionUID
  • specialParamHiding constructor or setter parameter hiding another field
  • static-access macro for indirectStatic and staticReceiver
  • staticReceiver if a non static receiver is used to get a static field or call a static method
  • super overriding a method without making a super invocation
  • suppress enable @SuppressWarnings
  • syntheticAccess, synthetic-access when performing synthetic access for innerclass
  • tasks enable support for tasks tags in source code
  • typeHiding type parameter hiding another type
  • unchecked unchecked type operation
  • unnecessaryElse unnecessary else clause
  • unqualified-field-access, unqualifiedField unqualified reference to field
  • unused macro for unusedArgument, unusedImport, unusedLabel, unusedLocal, unusedPrivate and unusedThrown
  • unusedArgument unused method argument
  • unusedImport unused import reference
  • unusedLabel unused label
  • unusedLocal unused local variable
  • unusedPrivate unused private member declaration
  • unusedThrown unused declared thrown exception
  • uselessTypeCheck unnecessary cast/instanceof operation
  • varargsCast varargs argument need explicit cast
  • warningToken unhandled warning token in @SuppressWarnings

Sun JDK (1.6) has a shorter list of supported warnings:

  • deprecation Check for use of depreciated items.
  • unchecked Give more detail for unchecked conversion warnings that are mandated by the Java Language Specification.
  • serial Warn about missing serialVersionUID definitions on serializable classes.
  • finally Warn about finally clauses that cannot complete normally.
  • fallthrough Check switch blocks for fall-through cases and provide a warning message for any that are found.
  • path Check for a nonexistent path in environment paths (such as classpath).

The latest available javac (1.6.0_13) for mac have the following supported warnings

  • all
  • cast
  • deprecation
  • divzero
  • empty
  • unchecked
  • fallthrough
  • path
  • serial
  • finally
  • overrides
Roily answered 30/7, 2009 at 11:38 Comment(5)
The Eclipse list here looks to compiler flags and not SuppressWarning annotations (check the last part of the doc you linked).Tartarean
They are both. By setting the compiler flags you tell the compiler what kind of warnings you want. With the annotations you can suppress these warnings in specific places in your code.Roily
I think the list is shorter for Eclipse. See latest galileo docs, list of available tokens for SupressWarnings is explicit there: help.eclipse.org/galileo/index.jsp?topic=/…Aguila
I tried @SuppressWarnings("raw") in Eclipse 3.5 and it does not work - I get a warning that it "raw" is not a valid value for this annotation.Emergent
me too. Unfortunatel that the jsp support isn't as flushed out as the java support is.Mandrill
G
9

I noticed that //noinspection can be auto-generated in IntelliJ

  • make sure you have not already a plan @SuppressWarninigs before the statement
  • Now you can auto-generate the specific //noinspection by hitting Alt+Enter when you have the warning selected and then use the right arrow key to see the Suppress for ... option

Ended up here when I wanted to suppress a "switch has too few case labels" warning from IntelliJ. I did not find a complete List for IntelliJ's @SuppressWarning support but //noinspection did the trick for me.

Gynaecomastia answered 17/1, 2019 at 15:2 Comment(3)
You can add @SuppressWarnings("SwitchStatementWithTooFewBranches") for that as well.Seduction
Unfortunately the Supress for ... option when clicking ALT+ENTER is not always availableKlein
//noinspection SwitchStatementWithTooFewBranches before the switch works as well for me.Superheterodyne
R
8

A new favorite for me is @SuppressWarnings("WeakerAccess") in IntelliJ, which keeps it from complaining when it thinks you should have a weaker access modifier than you are using. We have to have public access for some methods to support testing, and the @VisibleForTesting annotation doesn't prevent the warnings.

ETA: "Anonymous" commented, on the page @MattCampbell linked to, the following incredibly useful note:

You shouldn't need to use this list for the purpose you are describing. IntelliJ will add those SuppressWarnings for you automatically if you ask it to. It has been capable of doing this for as many releases back as I remember.

Just go to the location where you have the warning and type Alt-Enter (or select it in the Inspections list if you are seeing it there). When the menu comes up, showing the warning and offering to fix it for you (e.g. if the warning is "Method may be static" then "make static" is IntellJ's offer to fix it for you), instead of selecting "enter", just use the right arrow button to access the submenu, which will have options like "Edit inspection profile setting" and so forth. At the bottom of this list will be options like "Suppress all inspections for class", "Suppress for class", "Suppress for method", and occasionally "Suppress for statement". You probably want whichever one of these appears last on the list. Selecting one of these will add a @SuppressWarnings annotation (or comment in some cases) to your code suppressing the warning in question. You won't need to guess at which annotation to add, because IntelliJ will choose based on the warning you selected.

Roy answered 22/3, 2018 at 21:33 Comment(1)
I had the trouble of getting the mouse to hover and open the "Edit inspection profile setting" menu. It is the tiny arrow next to the Show Context options entry.Talyah
P
3

I just want to add that there is a master list of IntelliJ suppress parameters at: https://gist.github.com/vegaasen/157fbc6dce8545b7f12c

It looks fairly comprehensive. Partial:

Warning Description - Warning Name

"Magic character" MagicCharacter 
"Magic number" MagicNumber 
'Comparator.compare()' method does not use parameter ComparatorMethodParameterNotUsed 
'Connection.prepare*()' call with non-constant string JDBCPrepareStatementWithNonConstantString 
'Iterator.hasNext()' which calls 'next()' IteratorHasNextCallsIteratorNext 
'Iterator.next()' which can't throw 'NoSuchElementException' IteratorNextCanNotThrowNoSuchElementException 
'Statement.execute()' call with non-constant string JDBCExecuteWithNonConstantString 
'String.equals("")' StringEqualsEmptyString 
'StringBuffer' may be 'StringBuilder' (JDK 5.0 only) StringBufferMayBeStringBuilder 
'StringBuffer.toString()' in concatenation StringBufferToStringInConcatenation 
'assert' statement AssertStatement 
'assertEquals()' between objects of inconvertible types AssertEqualsBetweenInconvertibleTypes 
'await()' not in loop AwaitNotInLoop 
'await()' without corresponding 'signal()' AwaitWithoutCorrespondingSignal 
'break' statement BreakStatement 
'break' statement with label BreakStatementWithLabel 
'catch' generic class CatchGenericClass 
'clone()' does not call 'super.clone()' CloneDoesntCallSuperClone
Prenatal answered 20/12, 2017 at 16:44 Comment(0)
T
3

Official sources for the list of supported values

  • JSL 9.6.4.5 @SuppressWarnings
  • javac docs
  • CLI of your actual java
    • Java 5-16 javac -help -X
    • Java 17-21 javac --help-lint
  • In case you need older documentation, https://javaalmanac.io seems to be a pretty good reference.
  • Anything else that your IDE, or static code analyzer can understand. For example
    • Eclipse has a few additional ones that look like Java offical ones
    • IntelliJ IDEA has inspections that can be suppressed the same way.
    • Checkstyle, PMD, etc. can also use this same annotation.

javac -Xlint options across JDK versions

Based on the above here's an official-ish list from Oracle/OpenJDK JDKs.

Name Since* Description
all 5 Enable all warnings
none 6 Disable all warnings
preview 11
17std
Warn about use of preview language features.
auxiliaryclass 8 Warn about an auxiliary class that is hidden in a source file, and is used from other files.
cast 6 Warn about use of unnecessary casts.
classfile 7 Warn about issues related to classfile contents.
deprecation 5
11std
Warn about use of deprecated items.
dep-ann 7 Warn about items marked as deprecated in JavaDoc but not using the @Deprecated annotation.
divzero 6 Warn about division by constant integer 0.
empty 6 Warn about empty statement after if.
exports 17 Warn about issues regarding module exports.
fallthrough 5 Warn about falling through from one case of a switch statement to the next.
finally 5 Warn about finally clauses that do not terminate normally.
lossy-conversions 21 Warn about possible lossy conversions in compound assignment.
missing-explicit-ctor 17 Warn about missing explicit constructors in public and protected classes in exported packages.
module 9 Warn about module system related issues.
opens 9 Warn about issues regarding module opens.
options 7 Warn about issues relating to use of command line options.
output-file-clash 21 Warn when an output file is overwritten during compilation. This can occur, for example, on case-insensitive filesystems. Covers class files, native header files, and source files.
overloads 8 Warn about issues regarding method overloads.
overrides 6 Warn about issues regarding method overrides.
path 5 Warn about invalid path elements on the command line.
processing 7 Warn about issues regarding annotation processing.
rawtypes 7 Warn about use of raw types.
removal 9
11std
Warn about use of API that has been marked for removal.
requires-automatic 9 Warn about use of automatic modules in the requires clauses.
requires-transitive-automatic 9 Warn about automatic modules in requires transitive.
serial 5 Warn about Serializable classes that do not have a serialVersionUID field. Also warn about other suspect declarations in Serializable and Externalizable classes and interfaces.
static 7 Warn about accessing a static member using an instance.
strictfp 17 Warn about unnecessary use of the strictfp modifier.
synchronization 17 Warn about synchronization attempts on instances of value-based classes.
text-blocks 17 Warn about inconsistent white space characters in text block indentation.
this-escape 21 Warn when a constructor invokes a method that could be overriden in an external subclass. Such a method would execute before the subclass constructor completes its initialization.
try 7 Warn about issues relating to use of try blocks (i.e. try-with-resources).
unchecked 5
6std
Warn about unchecked operations.
varargs 7 Warn about potentially unsafe vararg methods.

* I only checked the commonly occurring 5, 6, 7, 8, 9, 11, 17, 21 versions.
std ones are standard according to JLS version evolution.

Torque answered 20/10, 2023 at 13:3 Comment(0)
H
2

This seems to be a much more complete list, where I found some warnings specific to Android Studio that I couldn't find elsewhere (e.g. SynchronizeOnNonFinalField):

https://jazzy.id.au/2008/10/30/list_of_suppresswarnings_arguments.html

Oh, now SO's guidelines contradict SO's restrictions. On one hand, I am supposed to copy the list rather than providing only the link. But on the other hand, this would exceed the maximum allowed number of characters. So let's just hope the link won't break.

Hootchykootchy answered 11/10, 2014 at 17:26 Comment(0)
G
1

JSL 1.7

The Oracle documentation mentions:

  • unchecked: Unchecked warnings are identified by the string "unchecked".
  • deprecation: A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with the annotation @Deprecated is used (i.e. overridden, invoked, or referenced by name), unless: [...] The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation"); or

It then explains that implementations can add and document their own:

Compiler vendors should document the warning names they support in conjunction with this annotation type. Vendors are encouraged to cooperate to ensure that the same names work across multiple compilers.

Gallicize answered 26/3, 2015 at 14:16 Comment(1)
It is not true that unchecked is the only one endorsed by the standard; the very next section from the one you quoted says deprecation warnings should not be produced when "The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation")"Clause
G
1

If you're using SonarLint, try above the method or class the whole squid string: @SuppressWarnings("squid:S1172")

Gerard answered 17/4, 2019 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.