Get warnings about unused method arguments from Eclipse to keep code clean

Unused variables and methods should alway be unwelcome. Removing them keeps the code cleaner and easier to read. Now, by default Eclipse warns you about unused private variables and methods, but it doesn’t warn you (by default) about unused method arguments.

But there is a compiler setting in Eclipse that can warn you when you don’t use an argument in a method. You can even handle arguments on inherited methods, especially useful when using 3rd party libraries.

Setup the compiler preferences

To get warnings of unused arguments:

  1. Go to Window > Preferences > Java > Compiler > Errors/Warnings.
  2. Open up the section Unnecessary Code.
  3. Change the setting for Parameter is never read from Ignore to Warning.
  4. (Recommended, but optional) Deselect Ignore overriding and implementing methods. I recommend deselecting this option. You can leave it selected if you want to, but the feature loses a bit of its usefulness. I discuss this option a bit more in the next section.

Here’s what the preference should look like:

And here’s an example of such a warning. In the example, the argument capacity isn’t used so is annotated as a warning.

What’s the easiest way to get rid of the warning? Well, just remove the argument using the Eclipse Change Method Signature refactoring. This will remove the argument from the method declaration and any method callers in one go. However, if you can’t remove the argument from the method then read the next section.

On an old codebase you may get lots of warnings initially. I’d normally handle these on a class-by-class basis only for the classes I’m currently working on, unless the team has a specific cleanup project/task. Just something to bear in mind, especially if you share workspace settings across the team via SVN or similar.

What about method signatures that can’t be changed?

When you deselected Ignore overriding and implementing methods, you told Eclipse to warn you about unused arguments in implemented/overridden methods. It’s a good indicator that your superclass/interface method is passing in more than it needs to (ie. remove the argument from the method signature) or that you’re ignoring something important about the interface contract (ie. use it somewhere in your method).

But sometimes you can’t remove the argument because you don’t have control over inherited methods, especially if you implement/override a 3rd party library’s method or if the method is part of a bigger framework that’s difficult to change. Other times you won’t have any need for the argument in very isolated cases. So you don’t want a warning to appear for these.

That’s why you can use the @SuppressWarning compiler annotation to stop Eclipse from reporting the warning. Here’s an example:

public String process(@SuppressWarnings("unused") int capacity, int max) {...}

You can apply SuppressWarning to an individual argument or the whole method. I’d recommend annotating only the argument that you want to ignore.

Eclipse makes it easy to add the compiler annotation. Just navigate to the warning, press Ctrl+1 and choose Add @SuppressWarning ‘unused’ to ‘variable’ (Quick Fix does its job again).

Related Tips

Support Eclipse On E

One thought on “Get warnings about unused method arguments from Eclipse to keep code clean

  1. Pingback: Tweets that mention Get warnings about unused method arguments from Eclipse to keep code clean « Eclipse on E -- Topsy.com