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?
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").
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
Related
For ease of development, my team has the following code in every java class:
/**
* The logger instance.
*/
private static Logger LOGGER = new Logger(Foo.class);
Not every class actually logs something, though, so sometimes this LOGGER goes unused, complete with Eclipse warning. We have this for when we need it to log something for debugging purposes or info purposes. The result is that we have a number of classes where there are no other Eclipse warnings apart from this one. We could add annotations to suppress these warnings, but that would involve touching a lot of files for no real benefit other than fixing eclipse warnings.
Ideally, what I would want to do is set a rule that states "IF the unused field is named LOGGER, THEN suppress the "field is not used" warning." Is this at all possible?
Did you try #SuppressWarning("unused") ? You can refer to Eclipse Documentation
Is it possible to suppress this kind of warning?
Note: I am not looking for suppressing all warnings(like this #SuppressWarnings("all") ), but only the mentioned type.
For Intellij, put this annotation on the class that has the warnings
#SuppressWarnings("SynchronizeOnNonFinalField")
This lead me to the tag to use for suppression and trial and error lead me to put it on the class instead of on the field or synchronized statement. :-P
If you're using IntelliJ, it looks like it's #SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter"). Otherwise it depends. You might find the answer on the list here: What is the list of valid #SuppressWarnings warning names in Java?
Note that the warning is usually correct. In most cases you should just use a final field.
This would help you,
All values are permitted (unrecognized ones are ignored). The list of
recognized ones is compiler specific.
'unchecked' and 'deprecation' are required by the Java Language
Specification, and so should be valid with all compilers. 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
Please refer this, same has discussed here What is the list of valid #SuppressWarnings warning names in Java?
I recently discovered FindBugs' #edu.umd.cs.findbugs.annotations.SuppressWarnings annotation which is pretty cool and allows you to basically tell FindBugs to ignore certain warnings.
I've successfully implemented my own SLF4J binding by following their recommendations to take slf4j-simple and modify it with your own logger and logger factory bindings, and I'm pleased to say it works like a charm.
I just ran find bugs on the package that contains this SLF4J binding and it is complaining about a certain line of code written by the original StaticLoggerBinder author (Ceki Gulku):
// to avoid constant folding by the compiler, this field must *not* be final.
publicstatic String REQUESTED_API_VERSION = "1.6"; // !final
FindBugs complains that this field "isn't final but it should be". However, the (very) smart people over at SLF4J already thought of that, and placed the surrounding comments provided above.
So, just to get FindBugs to shut up, I've modified the code per my usual way of suppressing FB warnings:
#edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
public static String REQUESTED_API_VERSION = "1.6";
When I clean my project and re-run FindBugs, I get a second warning on the same line of code, this time complaining:
This field is never read. The field is public or protected, so perhaps it is intended to be used with classes not seen as part of the analysis. If not, consider removing it from the class.
When I add this second warning suppression:
#edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
#edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public static String REQUESTED_API_VERSION = "1.6";
I get a compiler/syntax error from Eclipse:
Duplicate annotation #SuppressWarnings.
How can I suppress multiple FindBugs warnings on the same line of code?
Just list all the warning identifiers in an array, within a single annotation:
#edu.umd.cs.findbugs.annotations.SuppressWarnings({
"MS_SHOULD_BE_FINAL",
"URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";
Just as for the standard java.lang.SuppressWarnings, the FindBugs version also has a parameter of type String[]. For a single value, the curly braces can be omitted though to make life easier.
FindBugs 3+ style:
#SuppressFBWarnings(
value={"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE","STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"},
justification="let me just make the build pass")
Try this:
#edu.umd.cs.findbugs.annotations.SuppressWarnings({"MS_SHOULD_BE_FINAL" , "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";
I have a Java application that I work on using Eclipse. I have hundreds of warnings that are all due to things like this:
ArrayList< SomeType > list = new ArrayList();
generating a warning that ArrayList is a raw type since I haven't included the type specifier when allocating the ArrayList itself. Is there a way to suppress this warning globally in Eclipse, rather than adding a #SuppressWarning annotation?
I'm concerned that I'm missing a warning that may be more valuable because of all these raw type ones.
Can change the settings by navigating -
project properties -> Java Compiler -> Errors/Warnings -> Generics types
Enable the - Enable project specific settings
Change warning to ignore.
Just do this -
#SuppressWarnings("rawtypes")
ArrayList< SomeType > list = new ArrayList();
warning will be removed.
You could use the -Xlint:unchecked command line option but that would be the same as #SuppressWarnings("unchecked") which btw can be put on individual assignments like
#SuppressWarnings("unchecked")
ArrayList< SomeType > list = new ArrayList();
However, I strongly recommend to fix those warnings where possible and only suppress them in specific cases you are sure about.
I had this problem with a directory containing a large number of generated files. For that case, warnings can be switched off per directory. In the package exploreer, right click on the directory, then choose
Properties -> Java Compiler -> Ignore optional compile problems
Note that this switches off other warnings as well - but for generated code, these have to be fixed by changing the generator anyway.
I tried annotating the enclosing method with
#SuppressWarnings("compareIdentical")
but this does not work (worse yet, the annotation results in its own Unsupported #SuppressWarnings("compareIdentical") warning!)
I know that I can always use
#SuppressWarnings("all")
but that'd be more warning-suppression than I want.
FWIW, I got the "compareIdentical" string from the "Warning Options" table in http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm (a hail-mary pass, to be sure).
Thanks!
Officially, there are only 3 supported arguments to #SuppressWarnings(), as specified by the standard $9.6.4.5:
Unchecked warnings (§4.8, §5.1.6, §5.1.9, §8.4.1, §8.4.8.3, §15.12.4.2, §15.13.2, §15.27.3) are specified by the string "unchecked".
Deprecation warnings (§9.6.4.6) are specified by the string "deprecation".
Removal warnings (§9.6.4.6) are specified by the string "removal".
But, in small text, the standard mentions support for extra types:
For other kinds of warnings, compiler vendors should document the strings they support for #SuppressWarnings. Vendors are encouraged to cooperate to ensure that the same names work across multiple compilers.
These are supported by some compilers:
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
raw to suppress warnings relative to usage of raw types
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
super to suppress warnings relative to overriding a method without super invocations
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 and dead code
So, there is nothing which might help you.