forked from google/error-prone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an evaluator for deciding if use of a method's result is expec…
…ted (aka CRV) or optional (aka CIRV). The idea here (for now) is to match the behavior of the `CheckReturnValue` checker (i.e. looking at the method, then enclosing classes, etc.) while improving on it in a number of ways: - Make it easy to plug in new rules determining a result-use policy for methods. - Make it easy to see what rules apply and how they're prioritized. - Add a "global" scope that can be used for implementing defaults. - Allow us to check declarations for conflicts between rules in general, not just to check for something being annotated with both CRV and CIRV. This should allow us to, for example, prevent an `@AutoValue` getter method from being annotated with CIRV. - Make it far easier for us to get good data. Every rule can create an `Evaluation` with data that allows us to see exactly why any given method had a certain policy selected for it: the ID of the rule that selected the policy, the scope at which the selection was made, and the symbol that was evaluated to make that selection (for example the class that an annotation was found on). See unknown commit for an example of how easy extending this with new rules should be. PiperOrigin-RevId: 447130426
- Loading branch information
Showing
7 changed files
with
586 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/ProtoRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2022 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns.checkreturnvalue; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.checkreturnvalue.ResultUseRule.MethodRule; | ||
import com.google.errorprone.suppliers.Supplier; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.tools.javac.code.Symbol.MethodSymbol; | ||
import com.sun.tools.javac.code.Type; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
/** Rules for methods on proto messages and builders. */ | ||
public final class ProtoRules { | ||
|
||
private ProtoRules() {} | ||
|
||
/** | ||
* Returns a rule that handles proto builders, making their fluent setter methods' results | ||
* ignorable. | ||
*/ | ||
public static ResultUseRule protoBuilders() { | ||
return new ProtoBuilder(); | ||
} | ||
|
||
private static final Supplier<Type> MESSAGE_LITE_BUILDER = | ||
supplier("com.google.protobuf.MessageLite.Builder"); | ||
|
||
// TODO(cgdecker): Move proto rules from IgnoredPureGetter and ReturnValueIgnored here | ||
|
||
/** Rules for methods on proto builders. */ | ||
private static final class ProtoBuilder extends MethodRule { | ||
private static final Pattern SETTERS = Pattern.compile("(add|clear|remove|set|put).+"); | ||
|
||
@Override | ||
public String id() { | ||
return "PROTO_BUILDER"; | ||
} | ||
|
||
@Override | ||
public Optional<ResultUsePolicy> evaluateMethod(MethodSymbol method, VisitorState state) { | ||
Type ownerType = method.owner.type; | ||
if (ASTHelpers.isSubtype(ownerType, MESSAGE_LITE_BUILDER.get(state), state)) { | ||
if (SETTERS.matcher(method.name).matches()) { | ||
return Optional.of(ResultUsePolicy.OPTIONAL); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static Supplier<Type> supplier(String name) { | ||
return VisitorState.memoize(s -> s.getTypeFromString(name)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/ResultUsePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns.checkreturnvalue; | ||
|
||
/** Policy for use of a method or constructor's result. */ | ||
public enum ResultUsePolicy { | ||
/** | ||
* Use of the result is expected except in certain contexts where the method is being used in a | ||
* way such that not using the result is likely correct. Examples include when the result type at | ||
* the callsite is {@code java.lang.Void} and when the surrounding context seems to be testing | ||
* that the method throws an exception. | ||
*/ | ||
EXPECTED, | ||
/** Use of the result is optional. */ | ||
OPTIONAL, | ||
} |
Oops, something went wrong.