Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress the "unused" warning for a method, but not the variables within it

As the title asks, how do I suppress the warning for just the method? Is this possible?

A little background: I'm using a JavaScript bridge and it hooks into these methods, so I'd like to suppress this warning. What I would like to avoid is the annotation preventing warnings for unused variables within the method. I'm currently just using the @SuppressWarnings("unused") before the method declaration, but this suppresses everything.

like image 842
Daniel Arndt Avatar asked May 02 '13 19:05

Daniel Arndt


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

How do you avoid unused variable warnings in Python?

To suppress the warning, one can simply name the variable with an underscore ('_') alone. Python treats it as an unused variable and ignores it without giving the warning message.

What is suppress (' unused ')?

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

Which annotation should be used to remove warnings from a method?

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.


2 Answers

As far as I know, there is no way to scope SurpressWarning to the method declaration only.

This leaves you with these possibilities:

  • Increase the visibility of the method to package or protected. Slightly increasing visibility has the added benefit that it becomes available to unit tests.
  • Create a dummy method which has call to the unused methods. (This feels really ugly).
like image 166
Hendrik Brummermann Avatar answered Sep 20 '22 15:09

Hendrik Brummermann


In eclipse, If we set the following

Preferences -> Java -> Error/Warnings -> Unnecessary code -> Local variable is never read ==> Error

then, even when the @SuppressWarnings("unused") is applied on the method it only suppresses unused method warning. The unused variable is shown as an Error.

(I know, you wanted them to be 'warnings' but all I can come up with are 'errors' !)

like image 24
2020 Avatar answered Sep 20 '22 15:09

2020