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 a8f6bce2d6..c99b496d32 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 @@ -45,6 +45,13 @@ public final class IdentityConversion extends BugChecker implements MethodInvoca private static final long serialVersionUID = 1L; private static final Matcher IS_CONVERSION_METHOD = anyOf( + staticMethod() + .onClassAny( + Primitives.allWrapperTypes().stream() + .map(Class::getName) + .collect(toImmutableSet())) + .named("valueOf"), + staticMethod().onClass(String.class.getName()).named("valueOf"), staticMethod() .onClassAny( "com.google.common.collect.ImmutableBiMap", @@ -62,13 +69,6 @@ public final class IdentityConversion extends BugChecker implements MethodInvoca staticMethod() .onClass("com.google.errorprone.matchers.Matchers") .namedAnyOf("allOf", "anyOf"), - staticMethod() - .onClassAny( - Primitives.allWrapperTypes().stream() - .map(Class::getName) - .collect(toImmutableSet())) - .named("valueOf"), - staticMethod().onClass(String.class.getName()).named("valueOf"), staticMethod().onClass("reactor.adapter.rxjava.RxJava2Adapter"), staticMethod() .onClass("reactor.core.publisher.Flux") 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 c1f5345a75..baa74babdc 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 @@ -16,10 +16,9 @@ final class IdentityConversionTest { 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;", + "A.java", + "import static com.google.errorprone.matchers.Matchers.instanceMethod;", + "import static com.google.errorprone.matchers.Matchers.staticMethod;", "", "import com.google.common.collect.ImmutableBiMap;", "import com.google.common.collect.ImmutableList;", @@ -38,8 +37,8 @@ void identification() { "import reactor.core.publisher.Flux;", "import reactor.core.publisher.Mono;", "", - "public final class Foo {", - " public void foo() {", + "public final class A {", + " public void m() {", " // BUG: Diagnostic contains:", " Boolean b1 = Boolean.valueOf(Boolean.FALSE);", " // BUG: Diagnostic contains:", @@ -149,7 +148,15 @@ void identification() { " ImmutableTable o11 = ImmutableTable.copyOf(ImmutableTable.of());", "", " // BUG: Diagnostic contains:", + " Matcher allOf1 = Matchers.allOf(instanceMethod());", + " Matcher allOf2 = Matchers.allOf(instanceMethod(), staticMethod());", + " // BUG: Diagnostic contains:", + " Matcher anyOf1 = Matchers.anyOf(staticMethod());", + " Matcher anyOf2 = Matchers.anyOf(instanceMethod(), staticMethod());", + "", + " // BUG: Diagnostic contains:", " Flux flux1 = Flux.just(1).flatMap(e -> RxJava2Adapter.fluxToFlowable(Flux.just(2)));", + "", " // BUG: Diagnostic contains:", " Flux flux2 = Flux.concat(Flux.just(1));", " // BUG: Diagnostic contains:", @@ -160,14 +167,9 @@ void identification() { " Flux flux5 = Flux.merge(Flux.just(1));", "", " // BUG: Diagnostic contains:", - " Mono m1 = Mono.from(Mono.just(1));", - " // BUG: Diagnostic contains:", - " Mono m2 = Mono.fromDirect(Mono.just(1));", - "", - " // BUG: Diagnostic contains:", - " Matcher matcher1 = Matchers.allOf(annotations(AT_LEAST_ONE, isType(\"foo\")));", + " Mono mono1 = Mono.from(Mono.just(1));", " // BUG: Diagnostic contains:", - " Matcher matcher2 = Matchers.anyOf(annotations(AT_LEAST_ONE, isType(\"bar\")));", + " Mono mono2 = Mono.fromDirect(Mono.just(1));", " }", "}") .doTest(); @@ -178,10 +180,8 @@ void replacementFirstSuggestedFix() { refactoringTestHelper .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;", + "A.java", + "import static com.google.errorprone.matchers.Matchers.staticMethod;", "import static org.mockito.Mockito.when;", "", "import com.google.common.collect.ImmutableCollection;", @@ -196,8 +196,8 @@ void replacementFirstSuggestedFix() { "import reactor.core.publisher.Flux;", "import reactor.core.publisher.Mono;", "", - "public final class Foo {", - " public void foo() {", + "public final class A {", + " public void m() {", " ImmutableSet set1 = ImmutableSet.copyOf(ImmutableSet.of());", " ImmutableSet set2 = ImmutableSet.copyOf(ImmutableList.of());", "", @@ -222,7 +222,7 @@ void replacementFirstSuggestedFix() { " Object o1 = ImmutableSet.copyOf(ImmutableList.of());", " Object o2 = ImmutableSet.copyOf(ImmutableSet.of());", "", - " Matcher matcher = Matchers.allOf(annotations(AT_LEAST_ONE, isType(\"foo\")));", + " Matcher matcher = Matchers.allOf(staticMethod());", "", " when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));", " }", @@ -230,10 +230,8 @@ void replacementFirstSuggestedFix() { " void bar(Publisher 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;", + "A.java", + "import static com.google.errorprone.matchers.Matchers.staticMethod;", "import static org.mockito.Mockito.when;", "", "import com.google.common.collect.ImmutableCollection;", @@ -248,8 +246,8 @@ void replacementFirstSuggestedFix() { "import reactor.core.publisher.Flux;", "import reactor.core.publisher.Mono;", "", - "public final class Foo {", - " public void foo() {", + "public final class A {", + " public void m() {", " ImmutableSet set1 = ImmutableSet.of();", " ImmutableSet set2 = ImmutableSet.copyOf(ImmutableList.of());", "", @@ -274,7 +272,7 @@ void replacementFirstSuggestedFix() { " Object o1 = ImmutableSet.copyOf(ImmutableList.of());", " Object o2 = ImmutableSet.of();", "", - " Matcher matcher = annotations(AT_LEAST_ONE, isType(\"foo\"));", + " Matcher matcher = staticMethod();", "", " when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));", " }", @@ -289,14 +287,14 @@ void replacementSecondSuggestedFix() { refactoringTestHelper .setFixChooser(FixChoosers.SECOND) .addInputLines( - "Foo.java", + "A.java", "import com.google.common.collect.ImmutableCollection;", "import com.google.common.collect.ImmutableList;", "import com.google.common.collect.ImmutableSet;", "import java.util.ArrayList;", "", - "public final class Foo {", - " public void foo() {", + "public final class A {", + " public void m() {", " ImmutableSet set1 = ImmutableSet.copyOf(ImmutableSet.of());", " ImmutableSet set2 = ImmutableSet.copyOf(ImmutableList.of());", "", @@ -305,14 +303,14 @@ void replacementSecondSuggestedFix() { " }", "}") .addOutputLines( - "Foo.java", + "A.java", "import com.google.common.collect.ImmutableCollection;", "import com.google.common.collect.ImmutableList;", "import com.google.common.collect.ImmutableSet;", "import java.util.ArrayList;", "", - "public final class Foo {", - " public void foo() {", + "public final class A {", + " public void m() {", " @SuppressWarnings(\"IdentityConversion\")", " ImmutableSet set1 = ImmutableSet.copyOf(ImmutableSet.of());", " ImmutableSet set2 = ImmutableSet.copyOf(ImmutableList.of());",