0

CheckStyle had this option in the early versions: http://api.dpml.net/checkstyle/3.5/com/puppycrawl/tools/checkstyle/checks/usage/UnusedParameterCheck.html

Looking for a similar checker. Can anyone recommend me something?

Testing for this use case:


... main method .. 

        test("hello");
        return Longs.tryParse(request.getCategoryId());
    }

    @Nullable
    Long test(String unused) {
        System.out.println("Hello World");
        return null;
    }

I want the build to fail

Current CheckStyle version in use is 3.7. Not looking to downgrade.

SagwaCat
  • 9
  • 2
  • 2
    Isn't the compilation going to fail when you try and run your automated tests (unit, integration, ...)? – ndc85430 Jan 06 '23 at 20:02
  • 1
    That doesn't even compile. Why do you need a linter for that? – Sören Jan 06 '23 at 20:05
  • Sorry, I added a more clear example. – SagwaCat Jan 06 '23 at 20:07
  • Does this answer your question? [How to check "The value of the local variable/field is not used" in Checkstyle?](https://stackoverflow.com/questions/20116032/how-to-check-the-value-of-the-local-variable-field-is-not-used-in-checkstyle) – Sören Jan 07 '23 at 07:58
  • Unfortunately that does not catch unused params. I just tried it out – SagwaCat Jan 10 '23 at 16:17

1 Answers1

0

Sonar Lint supports this as rule "java:S1172":

Unused method parameters should be removed - Unused parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.

Sonar lint rule

But there are some exceptions to this rule.

The rule will not raise issues for unused parameters:

  • that are annotated with @javax.enterprise.event.Observes
  • in overrides and implementation methods
  • in interface default methods
  • in non-private methods that only throw or that have empty bodies
  • in annotated methods, unless the annotation is @SuppressWarning("unchecked") or @SuppressWarning("rawtypes"), inwhich case the annotation will be ignored
  • in overridable methods (non-final, or not member of a final class, non-static, non-private), if the parameter is documented with a properjavadoc.

Also Eclipse IDE supports this out of the box. This can be enabled in Java>Compiler>Errors/Warnings with option "Value of method parameter is not used" Eclipse compiler settings for unused method parameters This produces info/warning/error (as you configure) "The value of the parameter XXX is not used"

Warning produced by Eclipse

Piro
  • 1,367
  • 2
  • 19
  • 40