indexOf should not compare greater than zero
org.openrewrite.java.cleanup.IndexOfShouldNotCompareGreaterThanZero
Replaces
String#indexOf(String) > 0
and List#indexOf(Object) > 0
with >=1
. Checking indexOf
against >0
ignores the first element, whereas >-1
is inclusive of the first element. For clarity, >=1
is used, because >0
and >=1
are semantically equal. Using >0
may appear to be a mistake with the intent of including all elements. If the intent is to check whether a value in included in a String
or List
, the String#contains(String)
or List#contains(Object)
methods may be better options altogether.- RSPEC-2692
- groupId: org.openrewrite
- artifactId: rewrite-java
- version: 7.40.6
This recipe has no required configuration parameters and comes from a rewrite core library. It can be activated directly without adding any dependencies.
Gradle
Maven POM
Maven Command Line
build.gradle
plugins {
id("org.openrewrite.rewrite") version("5.40.4")
}
rewrite {
activeRecipe("org.openrewrite.java.cleanup.IndexOfShouldNotCompareGreaterThanZero")
}
repositories {
mavenCentral()
}
pom.xml
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.45.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.cleanup.IndexOfShouldNotCompareGreaterThanZero</recipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
shell
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.activeRecipes=org.openrewrite.java.cleanup.IndexOfShouldNotCompareGreaterThanZero
The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.
Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.