Skip to content

Commit

Permalink
Add ResultUsePolicy.UNSPECIFIED.
Browse files Browse the repository at this point in the history
For now this doesn't change anything for checking since the check just cares whether the policy is `EXPECTED` or not.

PiperOrigin-RevId: 480151888
  • Loading branch information
cgdecker authored and Error Prone Team committed Oct 10, 2022
1 parent 47f34a9 commit 643fe87
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static com.google.errorprone.bugpatterns.checkreturnvalue.ProtoRules.protoBuilders;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUsePolicy.EXPECTED;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUsePolicy.OPTIONAL;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUsePolicy.UNSPECIFIED;
import static com.google.errorprone.bugpatterns.checkreturnvalue.Rules.globalDefault;
import static com.google.errorprone.bugpatterns.checkreturnvalue.Rules.mapAnnotationSimpleName;
import static com.google.errorprone.fixes.SuggestedFix.emptyFix;
Expand Down Expand Up @@ -149,11 +150,7 @@ private static Optional<ResultUsePolicy> defaultPolicy(ErrorProneFlags flags, St
*/
@Override
public Matcher<ExpressionTree> specializedMatcher() {
return (tree, state) ->
methodToInspect(tree)
.map(method -> evaluator.evaluate(method, state))
.orElse(OPTIONAL)
.equals(EXPECTED);
return (tree, state) -> getMethodPolicy(tree, state).equals(EXPECTED);
}

private static Optional<MethodSymbol> methodToInspect(ExpressionTree tree) {
Expand Down Expand Up @@ -190,6 +187,13 @@ private static Optional<MethodSymbol> methodSymbol(ExpressionTree tree) {
return sym instanceof MethodSymbol ? Optional.of((MethodSymbol) sym) : Optional.empty();
}

/** Returns the {@link ResultUsePolicy} for the method used in the given {@code expression}. */
public ResultUsePolicy getMethodPolicy(ExpressionTree expression, VisitorState state) {
return methodToInspect(expression)
.map(method -> evaluator.evaluate(method, state))
.orElse(UNSPECIFIED);
}

@Override
public boolean isCovered(ExpressionTree tree, VisitorState state) {
return methodToInspect(tree).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String id() {
@Override
public Optional<ResultUsePolicy> evaluateMethod(MethodSymbol method, VisitorState state) {
return EXTERNAL_RULE_EVALUATOR.get(state).methodMatches(method, state)
? Optional.of(ResultUsePolicy.OPTIONAL)
? Optional.of(ResultUsePolicy.UNSPECIFIED)
: Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public enum ResultUsePolicy {
EXPECTED,
/** Use of the result is optional. */
OPTIONAL,
/** It is unspecified whether the result should be used or not. */
UNSPECIFIED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.errorprone.bugpatterns.checkreturnvalue;

import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUsePolicy.OPTIONAL;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUsePolicy.UNSPECIFIED;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUseRule.RuleScope.ENCLOSING_ELEMENTS;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUseRule.RuleScope.GLOBAL;
import static com.google.errorprone.bugpatterns.checkreturnvalue.ResultUseRule.RuleScope.METHOD;
Expand Down Expand Up @@ -82,7 +82,7 @@ private ResultUsePolicyEvaluator(Builder builder) {
* apply to it.
*/
public ResultUsePolicy evaluate(MethodSymbol method, VisitorState state) {
return policies(method, state).findFirst().orElse(OPTIONAL);
return policies(method, state).findFirst().orElse(UNSPECIFIED);
}

private Stream<ResultUsePolicy> policies(MethodSymbol method, VisitorState state) {
Expand Down

0 comments on commit 643fe87

Please sign in to comment.