Recommend Precondition APIs instead of manually throwing runtime exceptions when testing against input parameters.#5872
Merged
Conversation
c2dc555 to
d9bcf5d
Compare
tbroyer
reviewed
Jun 12, 2026
| // This check relies purely on the method name. Since this checker specifically targets Guava's | ||
| // Preconditions, it's unlikely to cause issues, but a more robust check might also verify that | ||
| // the method belongs to a utility class or has a specific signature. Given this is a | ||
| // SUGGESTION, the current heuristic is acceptable. |
|
|
||
| // Don't refactor conditions with logical operators, as they can become quite complex and | ||
| // difficult to read when negated. | ||
| if (isHardToNegate(condition)) { |
Contributor
There was a problem hiding this comment.
This should probably come after the NEW_NPE / checkedExpr test, as that one doesn't negate the condition.
| } | ||
|
|
||
| @Override | ||
| public Description matchIf(IfTree tree, VisitorState state) { |
Contributor
There was a problem hiding this comment.
Should this first check whether Guava is in the classpath and bail out if not?
|
|
||
| ExpressionTree checkedExpr = getNullCheckedExpression(condition); | ||
| if (NEW_NPE.matches(newClass, state) && checkedExpr != null) { | ||
| return suggest(tree, "checkNotNull", state.getSourceForNode(checkedExpr), newClass, state); |
Contributor
There was a problem hiding this comment.
How about suggesting Objects.requireNonNull? (similar to #5861 but that one is meant to be a refactoring only, right?)
…ceptions when testing against input parameters. Flume results: unknown commit PiperOrigin-RevId: 931204960
d9bcf5d to
74546d8
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recommend
PreconditionAPIs instead of manually throwing runtime exceptions when testing against input parameters.Flume results: unknown commit