File tree Expand file tree Collapse file tree
core/src/main/java/com/google/errorprone Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3535import com .google .errorprone .fixes .SuggestedFix ;
3636import com .google .errorprone .matchers .Description ;
3737import com .google .errorprone .matchers .Matcher ;
38+ import com .google .errorprone .suppliers .Supplier ;
3839import com .sun .source .tree .BinaryTree ;
3940import com .sun .source .tree .BlockTree ;
4041import com .sun .source .tree .ExpressionTree ;
5556import java .util .List ;
5657import javax .inject .Inject ;
5758import javax .lang .model .element .ElementKind ;
59+ import javax .lang .model .element .TypeElement ;
5860import org .jspecify .annotations .Nullable ;
5961
6062/**
6668 severity = WARNING )
6769public final class PreferPreconditions extends BugChecker implements IfTreeMatcher {
6870
71+ private static final Supplier <@ Nullable TypeElement > PRECONDITIONS =
72+ VisitorState .memoize (
73+ state -> state .getElements ().getTypeElement ("com.google.common.base.Preconditions" ));
74+
6975 private final ConstantExpressions constantExpressions ;
7076
7177 @ Inject
@@ -75,6 +81,10 @@ public final class PreferPreconditions extends BugChecker implements IfTreeMatch
7581
7682 @ Override
7783 public Description matchIf (IfTree tree , VisitorState state ) {
84+ if (PRECONDITIONS .get (state ) == null ) {
85+ // Only emit findings if Preconditions is already on the classpath
86+ return NO_MATCH ;
87+ }
7888 if (tree .getElseStatement () != null ) {
7989 return NO_MATCH ;
8090 }
Original file line number Diff line number Diff line change @@ -1137,7 +1137,6 @@ public static ScannerSupplier warningChecks() {
11371137 PreconditionsCheckNotNullRepeated .class ,
11381138 PreferCharsetOverload .class ,
11391139 PreferInstanceofOverGetKind .class ,
1140- PreferPreconditions .class ,
11411140 PreferTestParameter .class ,
11421141 PreferThrowsTag .class ,
11431142 PrimitiveAtomicReference .class ,
@@ -1314,6 +1313,7 @@ public static ScannerSupplier warningChecks() {
13141313 ParameterComment .class ,
13151314 ParameterMissingNullable .class ,
13161315 PreferJavaTimeOverload .class ,
1316+ PreferPreconditions .class ,
13171317 PreferredInterfaceType .class ,
13181318 PrimitiveArrayPassedToVarargsMethod .class ,
13191319 PrivateConstructorForNoninstantiableModule .class ,
You can’t perform that action at this time.
0 commit comments