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 new file mode 100644 index 0000000000..03e13d8e2e --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java @@ -0,0 +1,30 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.errorprone.refaster.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.AlsoNegation; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to expressions dealing with {@link CharSequence}s. */ +@OnlineDocumentation +final class CharSequenceRules { + private CharSequenceRules() {} + + /** + * Prefer {@link CharSequence#isEmpty()} over alternatives that consult the char sequence's + * length. + */ + static final class CharSequenceIsEmpty { + @BeforeTemplate + boolean before(CharSequence ch) { + return Refaster.anyOf(ch.length() == 0, ch.length() <= 0, ch.length() < 1); + } + + @AfterTemplate + @AlsoNegation + boolean after(CharSequence ch) { + return ch.isEmpty(); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index fd5cb29672..3f9250eb13 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -36,6 +36,7 @@ final class RefasterRulesTest { AssortedRules.class, BigDecimalRules.class, BugCheckerRules.class, + CharSequenceRules.class, ClassRules.class, CollectionRules.class, ComparatorRules.class, 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 new file mode 100644 index 0000000000..a656612fe1 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java @@ -0,0 +1,16 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +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); + } +} 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 new file mode 100644 index 0000000000..bfa8b9204e --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java @@ -0,0 +1,16 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +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()); + } +}