From 0f7f677b298298f88fa5a6c37ecd3d399da067ec Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 21 Jan 2026 08:44:38 -0800 Subject: [PATCH] Add tests for UnusedMethod with overridden methods in private interfaces. I am now less convinced that the _finding_ was wrong, though the _fix_ is certainly unhelpful. The example in a bug does seem to be genuinely unused in that the method on the interface is not visible outside the class, and isn't called within it. The fix won't compile though. PiperOrigin-RevId: 859119202 --- .../bugpatterns/UnusedMethodTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java index 813e0e9e358..f90464a32b8 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java @@ -738,4 +738,92 @@ public int frobnicator() { """) .doTest(); } + + @Test + public void overriddenMethod_visibleFromSubclass_noFinding() { + helper + .addSourceLines( + "Test.java", + """ + class Test { + private interface Frobnicator { + int frobnicate(); + } + + public class Impl implements Frobnicator { + @Override + public int frobnicate() { + return 0; + } + } + } + """) + .doTest(); + } + + @Test + public void overriddenMethod_notVisibleOutside() { + // Note that while the finding is correct here, the fix is not by itself: the method and all its + // overrides could be removed. + refactoringHelper + .addInputLines( + "Test.java", + """ + class Test { + private interface Frobnicator { + // BUG: Diagnostic contains: + int frobnicate(); + } + + private class Impl implements Frobnicator { + @Override + public int frobnicate() { + return 0; + } + } + } + """) + .addOutputLines( + "Test.java", + """ + class Test { + private interface Frobnicator {} + + private class Impl implements Frobnicator { + @Override + public int frobnicate() { + return 0; + } + } + } + """) + .allowBreakingChanges() + .doTest(); + } + + @Test + public void overriddenMethod_notVisibleOutside_butUsed() { + helper + .addSourceLines( + "Test.java", + """ + class Test { + private interface Frobnicator { + int frobnicate(); + } + + private class Impl implements Frobnicator { + @Override + public int frobnicate() { + return 0; + } + } + + void f(Frobnicator f) { + var unused = f.frobnicate(); + } + } + """) + .doTest(); + } }