From a87b281fcabef73baa4bd40537d0c008b4fe1e1d Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 7 Sep 2023 08:47:24 -0700 Subject: [PATCH] Optionally batch up findings in SameNameButDifferent Emitting once per occurrence is useful when showing findings on changed lines in code review, but not when generating batch findings. PiperOrigin-RevId: 563444171 --- .../bugpatterns/SameNameButDifferent.java | 12 +++++++++ .../bugpatterns/SameNameButDifferentTest.java | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/SameNameButDifferent.java b/core/src/main/java/com/google/errorprone/bugpatterns/SameNameButDifferent.java index 1e828d29602..fccb79c97a5 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/SameNameButDifferent.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/SameNameButDifferent.java @@ -27,6 +27,7 @@ import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import com.google.errorprone.BugPattern; +import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher; import com.google.errorprone.fixes.SuggestedFix; @@ -48,6 +49,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import javax.inject.Inject; import javax.lang.model.element.ElementKind; import javax.lang.model.element.Name; import org.checkerframework.checker.nullness.qual.Nullable; @@ -57,6 +59,13 @@ summary = "This type name shadows another in a way that may be confusing.", severity = WARNING) public final class SameNameButDifferent extends BugChecker implements CompilationUnitTreeMatcher { + private final Boolean batchFindings; + + @Inject + SameNameButDifferent(ErrorProneFlags flags) { + batchFindings = flags.getBoolean("SameNameButDifferent:BatchFindings").orElse(false); + } + @Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { Table> table = HashBasedTable.create(); @@ -180,6 +189,9 @@ private void handle(Tree tree) { for (TreePath treePath : treePaths) { state.reportMatch( buildDescription(treePath.getLeaf()).setMessage(message).addFix(fix).build()); + if (batchFindings) { + break; + } } } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/SameNameButDifferentTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/SameNameButDifferentTest.java index 79caad61236..184c086e6f6 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/SameNameButDifferentTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/SameNameButDifferentTest.java @@ -16,6 +16,7 @@ package com.google.errorprone.bugpatterns; +import com.google.common.collect.ImmutableList; import com.google.errorprone.BugCheckerRefactoringTestHelper; import com.google.errorprone.CompilationTestHelper; import org.junit.Test; @@ -207,4 +208,28 @@ public void referencesSelf() { "}") .doTest(); } + + @Test + public void ungroupedOverloadsPositiveCasesCoveringOnlyFirstOverload() { + helper + .addSourceLines( + "Test.java", + "import java.util.function.Supplier;", + "class Test {", + " class One {", + " class Clash {}", + " // BUG: Diagnostic contains:", + " Clash a;", + " Clash b;", + " }", + " class Two {", + " class Clash {}", + " // BUG: Diagnostic contains:", + " Clash a;", + " Clash b;", + " }", + "}") + .setArgs(ImmutableList.of("-XepOpt:SameNameButDifferent:BatchFindings")) + .doTest(); + } }