Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.suppliers.Supplier;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ExpressionTree;
Expand All @@ -55,6 +56,7 @@
import java.util.List;
import javax.inject.Inject;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import org.jspecify.annotations.Nullable;

/**
Expand All @@ -66,6 +68,10 @@
severity = WARNING)
public final class PreferPreconditions extends BugChecker implements IfTreeMatcher {

private static final Supplier<@Nullable TypeElement> PRECONDITIONS =
VisitorState.memoize(
state -> state.getElements().getTypeElement("com.google.common.base.Preconditions"));

private final ConstantExpressions constantExpressions;

@Inject
Expand All @@ -75,6 +81,10 @@ public final class PreferPreconditions extends BugChecker implements IfTreeMatch

@Override
public Description matchIf(IfTree tree, VisitorState state) {
if (PRECONDITIONS.get(state) == null) {
// Only emit findings if Preconditions is already on the classpath
return NO_MATCH;
}
if (tree.getElseStatement() != null) {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,6 @@ public static ScannerSupplier warningChecks() {
PreconditionsCheckNotNullRepeated.class,
PreferCharsetOverload.class,
PreferInstanceofOverGetKind.class,
PreferPreconditions.class,
PreferTestParameter.class,
PreferThrowsTag.class,
PrimitiveAtomicReference.class,
Expand Down Expand Up @@ -1314,6 +1313,7 @@ public static ScannerSupplier warningChecks() {
ParameterComment.class,
ParameterMissingNullable.class,
PreferJavaTimeOverload.class,
PreferPreconditions.class,
PreferredInterfaceType.class,
PrimitiveArrayPassedToVarargsMethod.class,
PrivateConstructorForNoninstantiableModule.class,
Expand Down
Loading