Skip to content

Commit

Permalink
Check for redundant allOf, anyOf error-prone matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgheniiShipilov committed Dec 19, 2022
1 parent fd2946a commit bac4154
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/** A {@link BugChecker} that flags redundant identity conversions. */
// XXX: Consider detecting cases where a flagged expression is passed to a method, and where removal
// of the identify conversion would cause a different method overload to be selected. Depending on
// of the identity conversion would cause a different method overload to be selected. Depending on
// the target method such a modification may change the code's semantics or performance.
@AutoService(BugChecker.class)
@BugPattern(
Expand Down Expand Up @@ -70,7 +70,10 @@ public final class IdentityConversion extends BugChecker implements MethodInvoca
staticMethod()
.onClass("reactor.core.publisher.Flux")
.namedAnyOf("concat", "firstWithSignal", "from", "merge"),
staticMethod().onClass("reactor.core.publisher.Mono").namedAnyOf("from", "fromDirect"));
staticMethod().onClass("reactor.core.publisher.Mono").namedAnyOf("from", "fromDirect"),
staticMethod()
.onClass("com.google.errorprone.matchers.Matchers")
.namedAnyOf("allOf", "anyOf"));

/** Instantiates a new {@link IdentityConversion} instance. */
public IdentityConversion() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ void identification() {
compilationTestHelper
.addSourceLines(
"Foo.java",
"import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;",
"import static com.google.errorprone.matchers.Matchers.annotations;",
"import static com.google.errorprone.matchers.Matchers.isType;",
"",
"import com.google.common.collect.ImmutableBiMap;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableListMultimap;",
Expand All @@ -28,6 +32,8 @@ void identification() {
"import com.google.common.collect.ImmutableSet;",
"import com.google.common.collect.ImmutableSetMultimap;",
"import com.google.common.collect.ImmutableTable;",
"import com.google.errorprone.matchers.Matcher;",
"import com.google.errorprone.matchers.Matchers;",
"import reactor.adapter.rxjava.RxJava2Adapter;",
"import reactor.core.publisher.Flux;",
"import reactor.core.publisher.Mono;",
Expand Down Expand Up @@ -122,8 +128,8 @@ void identification() {
" // BUG: Diagnostic contains:",
" ImmutableList<Object> o2 = ImmutableList.copyOf(ImmutableList.of());",
" ImmutableListMultimap<Object, Object> o3 =",
" // BUG: Diagnostic contains:",
" ImmutableListMultimap.copyOf(ImmutableListMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableListMultimap.copyOf(ImmutableListMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableMap<Object, Object> o4 = ImmutableMap.copyOf(ImmutableMap.of());",
" // BUG: Diagnostic contains:",
Expand All @@ -137,8 +143,8 @@ void identification() {
" // BUG: Diagnostic contains:",
" ImmutableSet<Object> o9 = ImmutableSet.copyOf(ImmutableSet.of());",
" ImmutableSetMultimap<Object, Object> o10 =",
" // BUG: Diagnostic contains:",
" ImmutableSetMultimap.copyOf(ImmutableSetMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableSetMultimap.copyOf(ImmutableSetMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableTable<Object, Object, Object> o11 = ImmutableTable.copyOf(ImmutableTable.of());",
"",
Expand All @@ -157,6 +163,11 @@ void identification() {
" Mono<Integer> m1 = Mono.from(Mono.just(1));",
" // BUG: Diagnostic contains:",
" Mono<Integer> m2 = Mono.fromDirect(Mono.just(1));",
"",
" // BUG: Diagnostic contains:",
" Matcher allOfMatcher = Matchers.allOf(annotations(AT_LEAST_ONE, isType(\"foo\")));",
" // BUG: Diagnostic contains:",
" Matcher anyOfMatcher = Matchers.anyOf(annotations(AT_LEAST_ONE, isType(\"foo\")));",
" }",
"}")
.doTest();
Expand All @@ -168,11 +179,16 @@ void replacementFirstSuggestedFix() {
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"Foo.java",
"import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;",
"import static com.google.errorprone.matchers.Matchers.annotations;",
"import static com.google.errorprone.matchers.Matchers.isType;",
"import static org.mockito.Mockito.when;",
"",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import com.google.errorprone.matchers.Matcher;",
"import com.google.errorprone.matchers.Matchers;",
"import java.util.ArrayList;",
"import java.util.Collection;",
"import org.reactivestreams.Publisher;",
Expand Down Expand Up @@ -206,18 +222,25 @@ void replacementFirstSuggestedFix() {
" Object o1 = ImmutableSet.copyOf(ImmutableList.of());",
" Object o2 = ImmutableSet.copyOf(ImmutableSet.of());",
"",
" Matcher matcher = Matchers.allOf(annotations(AT_LEAST_ONE, isType(\"foo\")));",
"",
" when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));",
" }",
"",
" void bar(Publisher<Integer> publisher) {}",
"}")
.addOutputLines(
"Foo.java",
"import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;",
"import static com.google.errorprone.matchers.Matchers.annotations;",
"import static com.google.errorprone.matchers.Matchers.isType;",
"import static org.mockito.Mockito.when;",
"",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import com.google.errorprone.matchers.Matcher;",
"import com.google.errorprone.matchers.Matchers;",
"import java.util.ArrayList;",
"import java.util.Collection;",
"import org.reactivestreams.Publisher;",
Expand Down Expand Up @@ -251,6 +274,8 @@ void replacementFirstSuggestedFix() {
" Object o1 = ImmutableSet.copyOf(ImmutableList.of());",
" Object o2 = ImmutableSet.of();",
"",
" Matcher matcher = annotations(AT_LEAST_ONE, isType(\"foo\"));",
"",
" when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));",
" }",
"",
Expand Down

0 comments on commit bac4154

Please sign in to comment.