diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IdentityConversion.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IdentityConversion.java index 00d6d9bdcbc..35785f50f9f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IdentityConversion.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IdentityConversion.java @@ -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( @@ -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() {} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java index a8f867502a5..524af3e579a 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IdentityConversionTest.java @@ -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;", @@ -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;", @@ -122,8 +128,8 @@ void identification() { " // BUG: Diagnostic contains:", " ImmutableList o2 = ImmutableList.copyOf(ImmutableList.of());", " ImmutableListMultimap o3 =", - " // BUG: Diagnostic contains:", - " ImmutableListMultimap.copyOf(ImmutableListMultimap.of());", + " // BUG: Diagnostic contains:", + " ImmutableListMultimap.copyOf(ImmutableListMultimap.of());", " // BUG: Diagnostic contains:", " ImmutableMap o4 = ImmutableMap.copyOf(ImmutableMap.of());", " // BUG: Diagnostic contains:", @@ -137,8 +143,8 @@ void identification() { " // BUG: Diagnostic contains:", " ImmutableSet o9 = ImmutableSet.copyOf(ImmutableSet.of());", " ImmutableSetMultimap o10 =", - " // BUG: Diagnostic contains:", - " ImmutableSetMultimap.copyOf(ImmutableSetMultimap.of());", + " // BUG: Diagnostic contains:", + " ImmutableSetMultimap.copyOf(ImmutableSetMultimap.of());", " // BUG: Diagnostic contains:", " ImmutableTable o11 = ImmutableTable.copyOf(ImmutableTable.of());", "", @@ -157,6 +163,11 @@ void identification() { " Mono m1 = Mono.from(Mono.just(1));", " // BUG: Diagnostic contains:", " Mono 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(); @@ -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;", @@ -206,6 +222,8 @@ 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)));", " }", "", @@ -213,11 +231,16 @@ void replacementFirstSuggestedFix() { "}") .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;", @@ -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)));", " }", "",