diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java index 03e13d8e2e..5e1bc51819 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java @@ -15,16 +15,19 @@ private CharSequenceRules() {} * Prefer {@link CharSequence#isEmpty()} over alternatives that consult the char sequence's * length. */ + // XXX: Drop this rule once we (and OpenRewrite) no longer support projects targeting Java 14 or + // below. static final class CharSequenceIsEmpty { @BeforeTemplate - boolean before(CharSequence ch) { - return Refaster.anyOf(ch.length() == 0, ch.length() <= 0, ch.length() < 1); + boolean before(CharSequence charSequence) { + return Refaster.anyOf( + charSequence.length() == 0, charSequence.length() <= 0, charSequence.length() < 1); } @AfterTemplate @AlsoNegation - boolean after(CharSequence ch) { - return ch.isEmpty(); + boolean after(CharSequence charSequence) { + return charSequence.isEmpty(); } } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java index c3706dd0d9..af81c81bd1 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StringRules.java @@ -29,12 +29,14 @@ final class StringRules { private StringRules() {} /** Prefer {@link String#isEmpty()} over alternatives that consult the string's length. */ - // XXX: Now that we build with JDK 15+, this rule can be generalized to cover all `CharSequence` - // subtypes. This does require a mechanism (perhaps an annotation, or a separate Maven module) to - // make sure that non-String expressions are rewritten only if client code also targets JDK 15+. - @SuppressWarnings("CharSequenceIsEmpty" /* This is a more specific template. */) + // XXX: Drop this rule once we (and OpenRewrite) no longer support projects targeting Java 14 or + // below. The `CharSequenceIsEmpty` rule then suffices. (This rule exists so that e.g. projects + // that target JDK 11 can disable `CharSequenceIsEmpty` without losing a valuable rule.) + // XXX: Look into a more general approach to supporting different Java language levels, such as + // rule selection based on some annotation, or a separate Maven module. static final class StringIsEmpty { @BeforeTemplate + @SuppressWarnings("CharSequenceIsEmpty" /* This is a more specific template. */) boolean before(String str) { return Refaster.anyOf(str.length() == 0, str.length() <= 0, str.length() < 1); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java index a656612fe1..6a449f84fd 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java @@ -6,11 +6,11 @@ final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase { ImmutableSet testCharSequenceIsEmpty() { return ImmutableSet.of( - ((CharSequence) "foo").length() == 0, - ((CharSequence) "bar").length() <= 0, - ((CharSequence) "baz").length() < 1, - ((CharSequence) "foo").length() != 0, - ((CharSequence) "bar").length() > 0, - ((CharSequence) "baz").length() >= 1); + new StringBuilder("foo").length() == 0, + new StringBuilder("bar").length() <= 0, + new StringBuilder("baz").length() < 1, + new StringBuilder("qux").length() != 0, + new StringBuilder("quux").length() > 0, + new StringBuilder("corge").length() >= 1); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java index bfa8b9204e..0a506beb57 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java @@ -6,11 +6,11 @@ final class CharSequenceRulesTest implements RefasterRuleCollectionTestCase { ImmutableSet testCharSequenceIsEmpty() { return ImmutableSet.of( - ((CharSequence) "foo").isEmpty(), - ((CharSequence) "bar").isEmpty(), - ((CharSequence) "baz").isEmpty(), - !((CharSequence) "foo").isEmpty(), - !((CharSequence) "bar").isEmpty(), - !((CharSequence) "baz").isEmpty()); + new StringBuilder("foo").isEmpty(), + new StringBuilder("bar").isEmpty(), + new StringBuilder("baz").isEmpty(), + !new StringBuilder("qux").isEmpty(), + !new StringBuilder("quux").isEmpty(), + !new StringBuilder("corge").isEmpty()); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java index 39476e3636..d189a5df3b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestInput.java @@ -28,9 +28,9 @@ ImmutableSet testStringIsEmpty() { "foo".length() == 0, "bar".length() <= 0, "baz".length() < 1, - "foo".length() != 0, - "bar".length() > 0, - "baz".length() >= 1); + "qux".length() != 0, + "quux".length() > 0, + "corge".length() >= 1); } boolean testStringIsEmptyPredicate() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java index 7973d10d04..3717a85f00 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StringRulesTestOutput.java @@ -31,9 +31,9 @@ ImmutableSet testStringIsEmpty() { "foo".isEmpty(), "bar".isEmpty(), "baz".isEmpty(), - !"foo".isEmpty(), - !"bar".isEmpty(), - !"baz".isEmpty()); + !"qux".isEmpty(), + !"quux".isEmpty(), + !"corge".isEmpty()); } boolean testStringIsEmptyPredicate() {