diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsage.java index e2570a2fd51..0b94f27978e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsage.java @@ -3,16 +3,12 @@ import static com.google.errorprone.BugPattern.LinkType.CUSTOM; import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import static com.google.errorprone.BugPattern.StandardTags.STYLE; -import static com.sun.source.tree.Tree.Kind.IDENTIFIER; import static com.sun.source.tree.Tree.Kind.INSTANCE_OF; import static com.sun.source.tree.Tree.Kind.LAMBDA_EXPRESSION; -import static com.sun.source.tree.Tree.Kind.MEMBER_SELECT; -import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION; import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; @@ -20,11 +16,8 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.fixes.SuggestedFixes; import com.google.errorprone.matchers.Description; -import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.InstanceOfTree; import com.sun.source.tree.LambdaExpressionTree; -import com.sun.source.tree.MemberSelectTree; -import com.sun.source.tree.MethodInvocationTree; /** A {@link BugChecker} that aligns usages of T.class::isInstance. */ @AutoService(BugChecker.class) @@ -42,36 +35,13 @@ public IsInstanceUsage() {} @Override public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) { - if (ImmutableSet.of(LAMBDA_EXPRESSION, METHOD_INVOCATION).contains(tree.getKind())) { - if (INSTANCE_OF == tree.getBody().getKind()) { - return constructDescription( - tree, state, constructFix(tree, ((InstanceOfTree) tree.getBody()).getType())); - } else if (METHOD_INVOCATION == tree.getBody().getKind()) { - return treatMethodInvocation(tree, (MethodInvocationTree) tree.getBody(), state); - } + if (LAMBDA_EXPRESSION == tree.getKind() && INSTANCE_OF == tree.getBody().getKind()) { + return constructDescription( + tree, state, constructFix(tree, ((InstanceOfTree) tree.getBody()).getType())); } return Description.NO_MATCH; } - private Description treatMethodInvocation( - LambdaExpressionTree tree, MethodInvocationTree methodInvocationTree, VisitorState state) { - if (MEMBER_SELECT != methodInvocationTree.getMethodSelect().getKind() - || !((MemberSelectTree) methodInvocationTree.getMethodSelect()) - .getIdentifier() - .contentEquals("isInstance")) { - return Description.NO_MATCH; - } - MemberSelectTree methodSelect = (MemberSelectTree) methodInvocationTree.getMethodSelect(); - if (MEMBER_SELECT != methodSelect.getExpression().getKind() - || IDENTIFIER - != ((MemberSelectTree) methodSelect.getExpression()).getExpression().getKind()) { - return Description.NO_MATCH; - } - IdentifierTree identifierTree = - (IdentifierTree) ((MemberSelectTree) methodSelect.getExpression()).getExpression(); - return constructDescription(tree, state, constructFix(tree, identifierTree.getName())); - } - private Description constructDescription( LambdaExpressionTree tree, VisitorState state, SuggestedFix.Builder fixBuilder) { SuggestedFix fix = fixBuilder.build(); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MethodReferenceUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MethodReferenceUsage.java index bc3d2afc850..7bd04ad1d8e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MethodReferenceUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MethodReferenceUsage.java @@ -106,6 +106,7 @@ private static Optional constructMethodRef( } // XXX: Replace nested `Optional` usage. + // XXX: Cover for deeper method invocation like `T.class.isInstance(i)`. @SuppressWarnings("NestedOptionals") private static Optional constructMethodRef( LambdaExpressionTree lambdaExpr, MethodInvocationTree subTree) { diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsageTest.java index 1584a7a135c..74a56354c29 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IsInstanceUsageTest.java @@ -22,8 +22,6 @@ void identification() { " // BUG: Diagnostic contains:", " Flux.just(1).filter(i -> i instanceof Integer);", " // BUG: Diagnostic contains:", - " Flux.just(1).filter(i -> Integer.class.isInstance(i));", - " // BUG: Diagnostic contains:", " Flux.just(1).onErrorResume(t -> t instanceof Exception, t -> Flux.empty());", "", " Flux.just(1).filter(Integer.class::isInstance);", @@ -44,7 +42,6 @@ void replacement() { "class A {", " void m() {", " Flux.just(1).filter(i -> i instanceof Integer);", - " Flux.just(1).filter(i -> Integer.class.isInstance(i));", " }", "}") .addOutputLines( @@ -54,7 +51,6 @@ void replacement() { "class A {", " void m() {", " Flux.just(1).filter(Integer.class::isInstance);", - " Flux.just(1).filter(Integer.class::isInstance);", " }", "}") .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);