How to specifically suppress "Comparing identical expressions" in Eclipse-Helios JDT - java

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.

Related

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.
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

How do I suppress warning "Synchronization on a non-final field"

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?

Advantage of #SuppressWarnings annotation

#SuppressWarnings annotation is for cleaner code or by adding it is there any performance gain or any advantage ?
or can we reduce compile time by doing so.
The #SuppressWarnings annotation type allows Java programmers to disable compilation warnings for a certain part of a program (type, field, method, parameter, constructor, and local variable). Normally warnings are good. However in some cases they would be inappropriate and annoying. So programmers can choose to tell the compiler ignoring such warnings if needed.
There is no relation with performance.
The developer who wrote the code knew that it was always going to be safe, decided to use #SuppressWarnings("unchecked") to suppress the warning at compilation.
As mentioned by others, it is just a trust between the developer and code written.
more info here and here
There is no performance gain, only when compiling, compiler will or will not write a waring. However after compilation, there is no difference whatsoever.
It does not make your code cleaner or improve the performance. It just helps you to concentrate your attention on potentially dangerous code. If you have a list of 130 warnings you will soon stop to read them.
Most warnings represents bad programming practices or potencial problems that the compiler is not able to solve. A finished program should, ideally, compile with no warnings. This way, when you modify it and a new warning appears you can decide what to do.
For example:
Unreachable code. What was I thinking here
Lib ZZZ is deprecated. Should I upgrade to the new one? Can I continue with this for now?
Add type arguments to list... ups, I should be using generics
SuppressWarnings annotation is just to suppress warnings that you know are sure to occur and you don't care about it. Its just helps you to have a cleaner inspection, suppressing warnings you expect to see. No performance gain.

Can I set compiler preferences to ignore for “A generic array is created for a varargs parameter”?

I'm having the same problem as the person who asked this question.
Specifically, the warning is
Type safety: A generic array of Object&Serializable&Comparable is
created for a varargs parameter
on this line of code
Collections.addAll(row, name, units, Boolean.FALSE);
// types: List<Object>, String, Integer, Boolean
The solutions posted were lovely, but I was wondering if Eclipse provides another way. Can I change the compiler preferences to ignore that sort of type safety warning? If so, which one would I change?
EDIT: So far, I've tried all the ones in generic types and "Inexact type match for varargs" in Potential Programming Problems." Are there warnings Eclipse won't let me ignore or is there a different one I can turn off?
In Eclipse, you can disable any warnings, and also you can enable some warnings that are disabled by default, under Project->Properties->Java Compiler->Errors / Warnings:

How to suppress multiple FindBugs warnings for the same line of code

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";

Categories