Skip to content

Apache Lucene open-source search software

error-prone: replace ComparingThisWithNull with ast-grep rule (#14810)

Robert Muir5e860e172ead
diff --git a/build-tools/build-infra/src/main/groovy/lucene.validation.error-prone.gradle b/build-tools/build-infra/src/main/groovy/lucene.validation.error-prone.gradle
index a18b40f69dd..fd53032805c 100644
--- a/build-tools/build-infra/src/main/groovy/lucene.validation.error-prone.gradle
+++ b/build-tools/build-infra/src/main/groovy/lucene.validation.error-prone.gradle
@@ -162,7 +162,7 @@ allprojects { prj ->
// '-Xep:CheckReturnValue:OFF', // we don't use these annotations
'-Xep:CollectionToArraySafeParameter:ERROR',
// '-Xep:ComparableType:OFF',
- '-Xep:ComparingThisWithNull:ERROR',
+ // '-Xep:ComparingThisWithNull:ERROR', // we use ast-grep for this check
'-Xep:ComparisonOutOfRange:ERROR',
// '-Xep:CompatibleWithAnnotationMisuse:OFF', // we don't use this annotation
// '-Xep:CompileTimeConstant:OFF', // we don't use this annotation
diff --git a/gradle/validation/ast-grep/rules/errorprone.yml b/gradle/validation/ast-grep/rules/errorprone.yml
new file mode 100644
index 00000000000..346f5867f86
--- /dev/null
+++ b/gradle/validation/ast-grep/rules/errorprone.yml
@@ -0,0 +1,34 @@
+# Lint rules from https://errorprone.info/
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: WildcardImport
+language: java
+rule:
+ kind: asterisk
+ inside:
+ kind: import_declaration
+severity: error
+message: Wildcard imports, static or otherwise, should not be used
+note: Use a regular, single-type import
+url: https://errorprone.info/bugpattern/WildcardImport
+files:
+ - "**/*.java"
+ignores:
+ - "**/JavascriptParser.java"
+ - "**/JavascriptLexer.java"
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: ComparingThisWithNull
+language: java
+# we don't even bother matching specific operators, there are other binary ops,
+# but they are even more senseless for this + null.
+rule:
+ kind: binary_expression
+ all:
+ - has:
+ kind: this
+ - has:
+ kind: null_literal
+severity: error
+message: this == null is always false, this != null is always true
+url: https://errorprone.info/bugpattern/ComparingThisWithNull
diff --git a/gradle/validation/ast-grep/rules/no-wildcard-imports.sg.yaml b/gradle/validation/ast-grep/rules/no-wildcard-imports.sg.yaml
deleted file mode 100644
index 16945d8a145..00000000000
--- a/gradle/validation/ast-grep/rules/no-wildcard-imports.sg.yaml
+++ /dev/null
@@ -1,16 +0,0 @@
-# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
-id: wildcard-import-not-allowed
-language: java
-rule:
- kind: asterisk
- inside:
- kind: import_declaration
-severity: error
-message: don't use wildcard imports
-note: please use full imports instead
-url: https://errorprone.info/bugpattern/WildcardImport
-files:
- - "**/*.java"
-ignores:
- - "**/JavascriptParser.java"
- - "**/JavascriptLexer.java"
diff --git a/gradle/validation/ast-grep/tests/errorprone.yml b/gradle/validation/ast-grep/tests/errorprone.yml
new file mode 100644
index 00000000000..42efd1de931
--- /dev/null
+++ b/gradle/validation/ast-grep/tests/errorprone.yml
@@ -0,0 +1,22 @@
+---
+id: WildcardImport
+valid:
+ - import foo.bar.Baz;
+ - import static foo.bar.Baz;
+invalid:
+ - import foo.bar.*;
+ - import static foo.bar.*;
+---
+id: ComparingThisWithNull
+valid:
+ - if (this == that) {}
+ - if (that == null) {}
+ - if (null == null) {}
+ - if (this == this) {}
+invalid:
+ - if (this == null) {}
+ - if (null == this) {}
+ - |
+ x = this == null ? 1 : 0;
+ - |
+ x = null == this ? 1 : 0;
diff --git a/gradle/validation/ast-grep/tests/test-no-wildcard-imports.sg.yaml b/gradle/validation/ast-grep/tests/test-no-wildcard-imports.sg.yaml
deleted file mode 100644
index 013537cf5e4..00000000000
--- a/gradle/validation/ast-grep/tests/test-no-wildcard-imports.sg.yaml
+++ /dev/null
@@ -1,7 +0,0 @@
-id: wildcard-import-not-allowed
-valid:
- - import foo.bar.Baz;
- - import static foo.bar.Baz;
-invalid:
- - import foo.bar.*;
- - import static foo.bar.*;
\ No newline at end of file