Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnecessary @SuppressWarnings("unused")

I'm getting a compiler warning for the @SuppressWarnings annotation in eclipse for the code:

@Override public boolean doSomething(@SuppressWarnings("unused") String whatever) throws AnException {     throw new AnException("I'm still in bed and can't do anything until I've had a shower!"); } 

It looks like a yellow squiggle under the word "unused" and on mouse hover I get the tooltip Unnecessary @SuppressWarnings("unused").

I think another developer is being prompted to put in these annotations by eclipse and I'm basically being prompted to take them out. How can I configure eclipse to prompt me to put the @SuppressWarnings annotation in instead of it complaining about it?

If anyone would like to comment on best practice here then that would also be most welcome.

like image 562
Edd Avatar asked Mar 02 '12 10:03

Edd


People also ask

What does @SuppressWarnings unused mean?

The @SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ( "deprecation" ) and unused local variables or unused private methods ( "unused" ).

Why we use suppress warnings?

If we don't want to fix the warning, then we can suppress it with the @SuppressWarnings annotation. This annotation allows us to say which kinds of warnings to ignore. While warning types can vary by compiler vendor, the two most common are deprecation and unchecked.

What are unchecked warnings?

An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

What does @SuppressWarnings mean in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.


2 Answers

In the code in your question, the @SuppressWarnings("unused") annotation is unnecessary because the method is either overriding another method from a superclass or implementing an interface. Even if you don't actually use the whatever parameter it's mandatory to declare it, otherwise the @Override annotation will produce an error (you'd be changing the signature of the overridden method if you removed the parameter.)

In some older versions of Eclipse the code as shown would not cause a warning, but in more recent releases it does. I believe it's a valid warning, and I'd rather remove the @SuppressWarnings("unused") in this case.

like image 146
Óscar López Avatar answered Oct 17 '22 10:10

Óscar López


Go to

WindowPreferencesJavaCompilerErrors/WarningsAnnotations.

And select Ignore for Unused '@SuppressWarnings` token.

like image 38
aioobe Avatar answered Oct 17 '22 09:10

aioobe