Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False UnusedVariable warning for method parameters only used in overridden implementations #4409

Open
mlopatkin opened this issue May 24, 2024 · 3 comments

Comments

@mlopatkin
Copy link

Consider the following example:

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.

copybara-service bot pushed a commit that referenced this issue May 28, 2024
copybara-service bot pushed a commit that referenced this issue May 28, 2024
@cpovirk
Copy link
Member

cpovirk commented Jun 17, 2024

(Is this fixed by #4433, which started off being about a regression test but seems to have developed into an actual fix?)

@mlopatkin
Copy link
Author

Can confirm that the issue is fixed in 2.29.0. Thank you for solving it so quickly!

mlopatkin added a commit to gradle/gradle that referenced this issue Jul 16, 2024
A false positive UnusedVariable issue was fixed in 2.29.0

See google/error-prone#4409
@cushon cushon closed this as completed Jul 16, 2024
@mlopatkin
Copy link
Author

As of 2.29.1, the warning still happens when the descendant is an anonymous class:
https://github.com/mlopatkin/errorprone-issue-reproducers/blob/trunk/unused-variable-overloaded-method/src/main/java/com/example/AnonymousDescendant.java

@cushon cushon reopened this Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants