From c7fcb6edaa4b91b501de4cddffe3e657ccb0fc48 Mon Sep 17 00:00:00 2001 From: ghm Date: Fri, 11 Nov 2022 02:12:53 -0800 Subject: [PATCH] Don't fire MissingSummary on _effectively_ private methods. PiperOrigin-RevId: 487766405 --- .../com/google/errorprone/util/ASTHelpers.java | 2 +- .../bugpatterns/javadoc/MissingSummary.java | 5 ++--- .../bugpatterns/javadoc/MissingSummaryTest.java | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 7c36f7d71b8..8fcd7a4deda 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -356,7 +356,7 @@ public static boolean canBeRemoved(ClassSymbol symbol) { } /** Returns whether this symbol or any of its owners are private. */ - private static boolean isEffectivelyPrivate(Symbol symbol) { + public static boolean isEffectivelyPrivate(Symbol symbol) { return enclosingElements(symbol).anyMatch(Symbol::isPrivate); } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java index 1d05765ed57..841fb703e9c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/javadoc/MissingSummary.java @@ -24,6 +24,7 @@ import static com.google.errorprone.util.ASTHelpers.findSuperMethods; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.hasAnnotation; +import static com.google.errorprone.util.ASTHelpers.isEffectivelyPrivate; import static java.util.stream.Collectors.joining; import com.google.errorprone.BugPattern; @@ -198,8 +199,6 @@ private static boolean requiresJavadoc(Tree tree, VisitorState state) { && !findSuperMethods((MethodSymbol) symbol, state.getTypes()).isEmpty()) { return false; } - return symbol != null - && (symbol.getModifiers().contains(Modifier.PUBLIC) - || symbol.getModifiers().contains(Modifier.PROTECTED)); + return symbol != null && !isEffectivelyPrivate(symbol); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/MissingSummaryTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/MissingSummaryTest.java index b8bc0da106a..24bd71e829a 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/MissingSummaryTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/javadoc/MissingSummaryTest.java @@ -121,6 +121,20 @@ public void privateCase() { .doTest(); } + @Test + public void effectivelyPrivateCase() { + helper + .addSourceLines( + "Test.java", // + "class Test {", + " private class Inner {", + " /** @throws IllegalStateException */", + " public void test() {}", + " }", + "}") + .doTest(); + } + @Test public void negative() { helper