Skip to content

Apache Lucene open-source search software

build: replace six simple error prone checks (#14831)

Robert Muirc0e612fd29f2
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 c1ac36e4898..e7e05d87258 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
@@ -233,8 +233,8 @@ allprojects { prj ->
'-Xep:IsInstanceOfClass:ERROR',
// '-Xep:IsLoggableTagLength:OFF', // we don't use android
// '-Xep:JUnit3TestNotRun:OFF', // we don't use junit3
- '-Xep:JUnit4ClassAnnotationNonStatic:ERROR',
- '-Xep:JUnit4EmptyMethods:ERROR',
+ // '-Xep:JUnit4ClassAnnotationNonStatic:OFF', // we use ast-grep for this check
+ // '-Xep:JUnit4EmptyMethods:OFF', // we use ast-grep for this check
// '-Xep:JUnit4SetUpNotRun:OFF', // LuceneTestCase takes care
// '-Xep:JUnit4TearDownNotRun:OFF', // LuceneTestCase takes care
// '-Xep:JUnit4TestNotRun:OFF', // noisy
@@ -279,7 +279,7 @@ allprojects { prj ->
'-Xep:OptionalOfRedundantMethod:ERROR',
// '-Xep:OverlappingQualifierAndScopeAnnotation:OFF', // we don't use this annotation
// '-Xep:OverridesJavaxInjectableMethod:OFF', // we don't use this annotation
- '-Xep:PackageInfo:ERROR',
+ // '-Xep:PackageInfo:OFF', // we use ast-grep for this check
'-Xep:ParametersButNotParameterized:ERROR',
// '-Xep:ParcelableCreator:OFF', // we don't use android
// '-Xep:PeriodFrom:OFF', // we don't use Period
@@ -312,7 +312,7 @@ allprojects { prj ->
'-Xep:StreamToString:ERROR',
'-Xep:StringBuilderInitWithChar:ERROR',
// '-Xep:StringConcatToTextBlock:OFF', // TODO: there are problems
- '-Xep:SubstringOfZero:ERROR',
+ // '-Xep:SubstringOfZero:OFF', // we use ast-grep for this check
'-Xep:SuppressWarningsDeprecated:ERROR',
// '-Xep:TemporalAccessorGetChronoField:OFF', // we don't use TemporalAccessor.get
// '-Xep:TestParametersNotInitialized:OFF', // we don't use this annotation
@@ -331,7 +331,7 @@ allprojects { prj ->
'-Xep:UnsafeWildcard:ERROR',
'-Xep:UnusedAnonymousClass:ERROR',
'-Xep:UnusedCollectionModifiedInPlace:ERROR',
- // "-Xep:WildcardImport:ERROR", // we use ast-grep for this check.
+ // "-Xep:WildcardImport:OFF", // we use ast-grep for this check.
'-Xep:VarTypeName:ERROR',
// '-Xep:WrongOneof:OFF', // we don't use protobuf
'-Xep:XorPower:ERROR',
@@ -424,14 +424,14 @@ allprojects { prj ->
'-Xep:GetClassOnEnum:WARN',
// '-Xep:GuiceNestedCombine:OFF', // we don't use guice
// '-Xep:HidingField:OFF', // noisy
- '-Xep:ICCProfileGetInstance:WARN',
+ // '-Xep:ICCProfileGetInstance:OFF', // we use forbidden-apis for this
// '-Xep:IdentityHashMapUsage:OFF', // noisy
// '-Xep:ImmutableAnnotationChecker:OFF', // we don't use this annotation
// '-Xep:ImmutableEnumChecker:OFF', // noisy
// '-Xep:InconsistentCapitalization:OFF', // TODO: there are problems
// '-Xep:InconsistentHashCode:OFF', // noisy
// '-Xep:IgnoredPureGetter:OFF', // we don't use these google libraries
- '-Xep:IncorrectMainMethod:WARN',
+ // '-Xep:IncorrectMainMethod:OFF', // we use ast-grep for this
'-Xep:IncrementInForLoopAndHeader:WARN',
'-Xep:InheritDoc:WARN',
// '-Xep:InjectOnBugCheckers: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
index 346f5867f86..f099288f86e 100644
--- a/gradle/validation/ast-grep/rules/errorprone.yml
+++ b/gradle/validation/ast-grep/rules/errorprone.yml
@@ -32,3 +32,120 @@ rule:
severity: error
message: this == null is always false, this != null is always true
url: https://errorprone.info/bugpattern/ComparingThisWithNull
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: SubstringOfZero
+language: java
+rule:
+ pattern: $$RECEIVER.substring(0)
+ kind: method_invocation
+fix: $$RECEIVER
+severity: error
+message: String.substring(0) returns the original String
+url: https://errorprone.info/bugpattern/SubstringOfZero
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: JUnit4ClassAnnotationNonStatic
+language: java
+rule:
+ kind: method_declaration
+ has:
+ kind: modifiers
+ all:
+ # has a BeforeClass/AfterClass annotation
+ - has:
+ kind: marker_annotation
+ any:
+ - pattern: "@BeforeClass"
+ - pattern: "@AfterClass"
+ # isn't a static method
+ - not:
+ has:
+ pattern: static
+severity: error
+message: JUnit4ClassAnnotationNonStatic
+note: This method should be `static`
+url: https://errorprone.info/bugpattern/JUnit4ClassAnnotationNonStatic
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: JUnit4EmptyMethods
+language: java
+rule:
+ kind: method_declaration
+ all:
+ # has an empty/all-whitespace body
+ - has:
+ field: body
+ kind: block
+ pattern: "{}"
+ - has:
+ kind: modifiers
+ all:
+ # has a junit annotation
+ - has:
+ kind: marker_annotation
+ any:
+ - pattern: "@Before"
+ - pattern: "@After"
+ - pattern: "@BeforeClass"
+ - pattern: "@AfterClass"
+ # doesn't have an override annotation
+ - not:
+ has:
+ kind: marker_annotation
+ pattern: "@Override"
+fix: ""
+severity: error
+message: Empty JUnit4 @Before, @After, @BeforeClass, and @AfterClass methods are unnecessary and should be deleted.
+url: https://errorprone.info/bugpattern/JUnit4EmptyMethods
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: PackageInfo
+language: java
+files:
+ - "**/package-info.java"
+rule:
+ kind: program
+ # look for any child node not in an allow-list
+ has:
+ not:
+ any:
+ # allow pkg decl and associated docs / imports only
+ - kind: package_declaration
+ - kind: block_comment
+ - kind: line_comment
+ - kind: import_declaration
+severity: error
+message: Declaring types inside package-info.java files is very bad form
+url: https://errorprone.info/bugpattern/PackageInfo
+---
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/refs/heads/main/schemas/java_rule.json
+id: IncorrectMainMethod
+language: java
+rule:
+ kind: method_declaration
+ all:
+ # named main
+ - has:
+ field: name
+ pattern: main
+ - any:
+ # doesn't have void type
+ - not:
+ has:
+ kind: void_type
+ # doesn't have both public and static modifiers
+ - has:
+ kind: modifiers
+ not:
+ all:
+ - has:
+ not:
+ pattern: public
+ - has:
+ not:
+ pattern: static
+severity: error
+message: |
+ `main` methods must be `public`, `static`, and `void`
+url: https://errorprone.info/bugpattern/IncorrectMainMethod
diff --git a/gradle/validation/ast-grep/tests/errorprone.yml b/gradle/validation/ast-grep/tests/errorprone.yml
index 42efd1de931..318317f19a9 100644
--- a/gradle/validation/ast-grep/tests/errorprone.yml
+++ b/gradle/validation/ast-grep/tests/errorprone.yml
@@ -20,3 +20,86 @@ invalid:
x = this == null ? 1 : 0;
- |
x = null == this ? 1 : 0;
+---
+id: SubstringOfZero
+valid:
+ - x.substring(1)
+ - x.substring(0, 1)
+ - x.foobar(0)
+invalid:
+ - x.substring(0)
+ - x.y().substring(0)
+---
+id: JUnit4ClassAnnotationNonStatic
+valid:
+ - |
+ @Before
+ void t() {}
+ - |
+ @BeforeClass
+ static void t() {}
+invalid:
+ - |
+ @BeforeClass
+ void t() {}
+ - |
+ @BeforeClass
+ @SomethingElse
+ void t() {}
+---
+id: JUnit4EmptyMethods
+valid:
+ - |
+ void t() {}
+ - |
+ @Before
+ void t() {
+ int x;
+ }
+ - |
+ @Before
+ void t() {
+ // comment
+ }
+ - |
+ @Before
+ @Override
+ void t() {}
+invalid:
+ - |
+ @Before
+ void t() {}
+ - |
+ @BeforeClass
+ @Foo()
+ public static void t() {
+ }
+---
+id: PackageInfo
+valid:
+ - |
+ package foo.bar.baz;
+ - |
+ /** javadoc */
+ package foo.bar.baz;
+ - |
+ import x.y.Z; // javadocs
+ /** javadoc {@link Z} */
+ package foo.bar.baz;
+invalid:
+ - |
+ class Foo {}
+ - |
+ interface Bar {}
+---
+id: IncorrectMainMethod
+valid:
+ - |
+ public static void main() {}
+ - |
+ public void foo() {}
+invalid:
+ - |
+ public void main() {}
+ - |
+ public static int main() {}
diff --git a/gradle/validation/forbidden-apis/defaults.all.txt b/gradle/validation/forbidden-apis/defaults.all.txt
index 2dc3fe3769b..1a3416ce015 100644
--- a/gradle/validation/forbidden-apis/defaults.all.txt
+++ b/gradle/validation/forbidden-apis/defaults.all.txt
@@ -84,3 +84,7 @@ java.lang.invoke.MethodHandles$Lookup#defineClass(**)
java.lang.invoke.MethodHandles$Lookup#defineHiddenClass(**)
java.lang.invoke.MethodHandles$Lookup#defineHiddenClassWithClassData(**)
java.rmi.server.RMIClassLoader
+
+# https://errorprone.info/bugpattern/ICCProfileGetInstance
+@defaultMessage This method searches the class path for the given file, prefer to read the file and call getInstance(byte[]) or getInstance(InputStream)
+java.awt.color.ICC_Profile#getInstance(java.lang.String)
diff --git a/lucene/test-framework/src/test/org/apache/lucene/tests/util/TestBeforeAfterOverrides.java b/lucene/test-framework/src/test/org/apache/lucene/tests/util/TestBeforeAfterOverrides.java
index 89c09d0f736..2408e944eaa 100644
--- a/lucene/test-framework/src/test/org/apache/lucene/tests/util/TestBeforeAfterOverrides.java
+++ b/lucene/test-framework/src/test/org/apache/lucene/tests/util/TestBeforeAfterOverrides.java
@@ -30,7 +30,9 @@ public class TestBeforeAfterOverrides extends WithNestedTests {
public static class Before1 extends WithNestedTests.AbstractNestedTest {
@Before
- public void before() {}
+ public void before() {
+ /* intentionally left blank */
+ }
public void testEmpty() {}
}
@@ -45,7 +47,9 @@ public class TestBeforeAfterOverrides extends WithNestedTests {
public static class After1 extends WithNestedTests.AbstractNestedTest {
@After
- public void after() {}
+ public void after() {
+ /* intentionally left blank */
+ }
public void testEmpty() {}
}
@@ -54,7 +58,9 @@ public class TestBeforeAfterOverrides extends WithNestedTests {
public static class After3 extends Before2 {
@After
- public void after() {}
+ public void after() {
+ /* intentionally left blank */
+ }
}
@Test