google / google/error-prone

False UnusedVariable warning for method parameters only used in overridden implementations

Open
#4,409 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

Consider the following example:

```java
public class App {
private static class Base {
protected void doStuff(String usedInDescendants) {}
}

private static class Descendant extends Base {
@Override
protected void doStuff(String actuallyUsed) {
System.out.println(actuallyUsed);
}
}

public static void main(String[] args) {
Base b = new Descendant();

b.doStuff("some string");
}
}
```
**Expected result**: no warnings

**Actual result**: ErrorProne complains about `usedInDescendants` parameter of `App.Base.doStuff` being unused. However, this parameter is used by the overridden method in `Descendant` class and cannot be easily removed.

There are several workarounds, but they aren't universally applicable:
* Increase the visibility of the base class - gives the type unnecessary exposure
* Make the method abstract - then the base class cannot provide a default implementation.

Even converting the `Base` to an interface with default method still triggers the warning.

A full reproducer can be found at
https://github.com/mlopatkin/errorprone-issue-reproducers/tree/trunk/unused-variable-overloaded-method

Tested with ErrorProne 2.27.1, 2.24.1 on Java 11 and Java 21.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.