java:S1172 false positive when parameters are used by function overridden in child class

  • Java
  • S1172
  • SonarQube for IDE 10.27.1.81796

It marks someParameter as unused in AbstractClass even though it is necessary in some of the classes which extend from it.

public abstract class AbstractClass {
  protected boolean someFunction(String someParameter) {
    return false;
  }
}

public class SomeActualClass extends AbstractClass {
  @Override
  protected boolean someFunction(String someParameter) {
    return someParameter.isEmpty();
  }
}

Hi Lars,

Your observation makes sense, and I appreciate you bringing it to our attention.

I’ve investigated this behavior and found that the rule is actually working as intended. Our aim is to ensure that a method is explicitly designed for overriding. To achieve this, we require the presence of a @param Javadoc description for the parameter. This is documented in the Exceptions section of the rule description (RSPEC).

I’ve verified that the following code snippet will suppress the warning:

  /**
   * @param someParameter designed for overriding
   */
  protected boolean someFunction(String someParameter)

I suppose that makes sense but documenting the parameter as being used does feel like just suppressing a warning. What if the parameter isn’t actually used anymore by any of the overriding classes? It’ll never get cleaned up

That’s a valid observation. It’s impossible to know definitively if a method is overridden. Especially with libraries, its usage might be in another JAR. What we have here is a heuristic, which checks if a method looks like it’s designed for overriding.