Skip to content

Commit c75dd09

Browse files
cushonError Prone Team
authored andcommitted
Move PreferPreconditions to JavaCodeClarity to run on changed lines
and only emit findings if `Preconditions` is already on the classpath. This check is producing a lot of findings on unchanged lines and for third_party/ code that doesn't depend on Guava (like SafeRE). PiperOrigin-RevId: 942497251
1 parent 2be3abd commit c75dd09

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

core/src/main/java/com/google/errorprone/bugpatterns/PreferPreconditions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.errorprone.fixes.SuggestedFix;
3636
import com.google.errorprone.matchers.Description;
3737
import com.google.errorprone.matchers.Matcher;
38+
import com.google.errorprone.suppliers.Supplier;
3839
import com.sun.source.tree.BinaryTree;
3940
import com.sun.source.tree.BlockTree;
4041
import com.sun.source.tree.ExpressionTree;
@@ -55,6 +56,7 @@
5556
import java.util.List;
5657
import javax.inject.Inject;
5758
import javax.lang.model.element.ElementKind;
59+
import javax.lang.model.element.TypeElement;
5860
import org.jspecify.annotations.Nullable;
5961

6062
/**
@@ -66,6 +68,10 @@
6668
severity = WARNING)
6769
public 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
}

core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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,

0 commit comments

Comments
 (0)