From c6f8e195bb6b399823a69ec1b38f587466d12a85 Mon Sep 17 00:00:00 2001 From: ghm Date: Mon, 19 Jul 2021 03:11:41 -0700 Subject: [PATCH] Start using ASTHelpers#streamReceivers. PiperOrigin-RevId: 385516160 --- .../bugpatterns/ImmutableSetForContains.java | 9 ++++----- .../errorprone/bugpatterns/MissingTestCall.java | 10 +++------- .../bugpatterns/ModifiedButNotUsed.java | 14 ++++++++------ .../ProtoBuilderReturnValueIgnored.java | 10 ++-------- .../bugpatterns/ProtoTruthMixedDescriptors.java | 15 ++++++--------- .../bugpatterns/TruthAssertExpected.java | 9 ++------- 6 files changed, 25 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ImmutableSetForContains.java b/core/src/main/java/com/google/errorprone/bugpatterns/ImmutableSetForContains.java index 32530e674d7..cfd387309a4 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ImmutableSetForContains.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ImmutableSetForContains.java @@ -29,6 +29,7 @@ import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.util.ASTHelpers.getReceiver; import static com.google.errorprone.util.ASTHelpers.getSymbol; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import static java.util.stream.Collectors.toMap; import com.google.common.collect.ImmutableList; @@ -177,11 +178,9 @@ private static Tree stripParameters(Tree tree) { private static Optional getRootMethod( MethodInvocationTree methodInvocationTree, VisitorState state) { - ExpressionTree receiver = getReceiver(methodInvocationTree); - while (receiver != null && IMMUTABLE_BUILDER_METHODS.matches(receiver, state)) { - receiver = getReceiver(receiver); - } - return Optional.ofNullable(receiver); + return streamReceivers(methodInvocationTree) + .filter(t -> !IMMUTABLE_BUILDER_METHODS.matches(t, state)) + .findFirst(); } // Scans the tree for all usages of immutable list vars and determines if any usage will prevent diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MissingTestCall.java b/core/src/main/java/com/google/errorprone/bugpatterns/MissingTestCall.java index b6a139cea2a..d4554c5ee2e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MissingTestCall.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MissingTestCall.java @@ -16,11 +16,12 @@ package com.google.errorprone.bugpatterns; +import static com.google.common.collect.Streams.findLast; import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; -import static com.google.errorprone.util.ASTHelpers.getReceiver; import static com.google.errorprone.util.ASTHelpers.getSymbol; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableSet; @@ -32,7 +33,6 @@ import com.google.errorprone.matchers.Matcher; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; -import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.ReturnTree; @@ -126,11 +126,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { @Nullable private static ExpressionTree getUltimateReceiver(ExpressionTree tree) { - ExpressionTree receiver = getReceiver(tree); - while (receiver instanceof MemberSelectTree || receiver instanceof MethodInvocationTree) { - receiver = getReceiver(receiver); - } - return receiver; + return findLast(streamReceivers(tree)).orElse(null); } private static boolean isField(@Nullable ExpressionTree tree) { diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ModifiedButNotUsed.java b/core/src/main/java/com/google/errorprone/bugpatterns/ModifiedButNotUsed.java index 25f5ffec1db..e099e63a78f 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ModifiedButNotUsed.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ModifiedButNotUsed.java @@ -17,6 +17,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.Iterables.getOnlyElement; +import static com.google.common.collect.Streams.concat; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.matchers.Matchers.allOf; @@ -30,12 +31,12 @@ import static com.google.errorprone.util.ASTHelpers.getReceiver; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.isConsideredFinal; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import static java.util.stream.Collectors.joining; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; -import com.google.common.collect.Streams; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.ExpressionStatementTreeMatcher; @@ -90,7 +91,7 @@ public class ModifiedButNotUsed extends BugChecker "com.google.common.collect.ImmutableMultimap"); private static final ImmutableSet COLLECTIONS = - Streams.concat( + concat( GUAVA_IMMUTABLES.stream().map(i -> i + ".Builder"), Stream.of( "java.util.Collection", "java.util.Map", "com.google.common.collect.Multimap")) @@ -286,10 +287,11 @@ public Description matchExpressionStatement(ExpressionStatementTree tree, Visito * Whether this is a chain of method invocations terminating in a new proto or collection builder. */ private static boolean newFluentChain(ExpressionTree tree, VisitorState state) { - while (tree instanceof MethodInvocationTree && FLUENT_CHAIN.matches(tree, state)) { - tree = getReceiver(tree); - } - return tree != null && FLUENT_CONSTRUCTOR.matches(tree, state); + return concat(Stream.of(tree), streamReceivers(tree)) + .filter(t -> !FLUENT_CHAIN.matches(t, state)) + .findFirst() + .map(t -> FLUENT_CONSTRUCTOR.matches(t, state)) + .orElse(false); } private static class UnusedScanner extends TreePathScanner { diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoBuilderReturnValueIgnored.java b/core/src/main/java/com/google/errorprone/bugpatterns/ProtoBuilderReturnValueIgnored.java index 0d9b9e38cc2..2280426ba05 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoBuilderReturnValueIgnored.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ProtoBuilderReturnValueIgnored.java @@ -22,6 +22,7 @@ import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; import static com.google.errorprone.util.ASTHelpers.getReceiver; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import static com.google.errorprone.util.SideEffectAnalysis.hasSideEffect; import com.google.common.collect.ImmutableList; @@ -117,13 +118,6 @@ private static ImmutableList generateFixes( } private static boolean doesNotTerminateInNewBuilder(ExpressionTree tree, VisitorState state) { - for (ExpressionTree receiver = getReceiver(tree); - receiver instanceof MethodInvocationTree; - receiver = getReceiver(receiver)) { - if (ROOT_INVOCATIONS_TO_IGNORE.matches(receiver, state)) { - return false; - } - } - return true; + return streamReceivers(tree).noneMatch(t -> ROOT_INVOCATIONS_TO_IGNORE.matches(t, state)); } } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoTruthMixedDescriptors.java b/core/src/main/java/com/google/errorprone/bugpatterns/ProtoTruthMixedDescriptors.java index f8f2008ea8a..479bd84833e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoTruthMixedDescriptors.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ProtoTruthMixedDescriptors.java @@ -21,10 +21,10 @@ import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; -import static com.google.errorprone.util.ASTHelpers.getReceiver; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.getType; import static com.google.errorprone.util.ASTHelpers.isSubtype; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; @@ -93,14 +93,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState return Description.NO_MATCH; } TypeSymbol type = getOnlyElement(types); - for (ExpressionTree receiver = getReceiver(tree); - receiver instanceof MethodInvocationTree; - receiver = getReceiver(receiver)) { - if (ASSERT_THAT.matches(receiver, state)) { - return validateReceiver(tree, (MethodInvocationTree) receiver, type, state); - } - } - return Description.NO_MATCH; + return streamReceivers(tree) + .filter(t -> ASSERT_THAT.matches(t, state)) + .map(t -> validateReceiver(tree, (MethodInvocationTree) t, type, state)) + .findFirst() + .orElse(Description.NO_MATCH); } // Tries to resolve the proto which owns the symbol at `tree`, or absent if there isn't one. diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TruthAssertExpected.java b/core/src/main/java/com/google/errorprone/bugpatterns/TruthAssertExpected.java index f33d14935f6..fb99e77d7ca 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TruthAssertExpected.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TruthAssertExpected.java @@ -22,6 +22,7 @@ import static com.google.errorprone.matchers.Matchers.anyOf; import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; +import static com.google.errorprone.util.ASTHelpers.streamReceivers; import com.google.common.base.Ascii; import com.google.errorprone.BugPattern; @@ -189,13 +190,7 @@ private static Matcher hasReceiverMatching(Matcher matcher) { - while (tree instanceof MethodInvocationTree) { - tree = ASTHelpers.getReceiver(tree); - if (tree == null || matcher.matches(tree, state)) { - return tree; - } - } - return null; + return streamReceivers(tree).filter(t -> matcher.matches(t, state)).findFirst().orElse(null); } /**