Skip to content

Commit

Permalink
Start using ASTHelpers#streamReceivers.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 385516160
  • Loading branch information
graememorgan authored and Error Prone Team committed Jul 19, 2021
1 parent cf22d2d commit c6f8e19
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,11 +178,9 @@ private static Tree stripParameters(Tree tree) {

private static Optional<ExpressionTree> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,7 +91,7 @@ public class ModifiedButNotUsed extends BugChecker
"com.google.common.collect.ImmutableMultimap");

private static final ImmutableSet<String> COLLECTIONS =
Streams.concat(
concat(
GUAVA_IMMUTABLES.stream().map(i -> i + ".Builder"),
Stream.of(
"java.util.Collection", "java.util.Map", "com.google.common.collect.Multimap"))
Expand Down Expand Up @@ -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<Void, Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,13 +118,6 @@ private static ImmutableList<SuggestedFix> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,13 +190,7 @@ private static Matcher<ExpressionTree> hasReceiverMatching(Matcher<ExpressionTre
@Nullable
private static ExpressionTree findReceiverMatching(
ExpressionTree tree, VisitorState state, Matcher<ExpressionTree> 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);
}

/**
Expand Down

0 comments on commit c6f8e19

Please sign in to comment.