Skip to content

Commit

Permalink
Optionally batch up findings in SameNameButDifferent
Browse files Browse the repository at this point in the history
Emitting once per occurrence is useful when showing findings on changed lines
in code review, but not when generating batch findings.

PiperOrigin-RevId: 563444171
  • Loading branch information
cushon authored and Error Prone Team committed Sep 7, 2023
1 parent fe652df commit a87b281
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String, TypeSymbol, List<TreePath>> table = HashBasedTable.create();
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit a87b281

Please sign in to comment.