Skip to content

Recommend Precondition APIs instead of manually throwing runtime exceptions when testing against input parameters.#5872

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_887005452
Jun 12, 2026
Merged

Recommend Precondition APIs instead of manually throwing runtime exceptions when testing against input parameters.#5872
copybara-service[bot] merged 1 commit into
masterfrom
test_887005452

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Recommend Precondition APIs instead of manually throwing runtime exceptions when testing against input parameters.

Flume results: unknown commit

// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a WARNING actually


// Don't refactor conditions with logical operators, as they can become quite complex and
// difficult to read when negated.
if (isHardToNegate(condition)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@copybara-service copybara-service Bot merged commit 74546d8 into master Jun 12, 2026
@copybara-service copybara-service Bot deleted the test_887005452 branch June 12, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants