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(); + } }