Skip to content

Commit

Permalink
Introduce CharSequenceRules Refaster rule collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Dec 25, 2024
1 parent 72b701c commit f0e1014
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class RefasterRulesTest {
AssortedRules.class,
BigDecimalRules.class,
BugCheckerRules.class,
CharSequenceRules.class,
ClassRules.class,
CollectionRules.class,
ComparatorRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> testCharSequenceIsEmpty() {
return ImmutableSet.of(
((CharSequence) "foo").isEmpty(),
((CharSequence) "bar").isEmpty(),
((CharSequence) "baz").isEmpty(),
!((CharSequence) "foo").isEmpty(),
!((CharSequence) "bar").isEmpty(),
!((CharSequence) "baz").isEmpty());
}
}

0 comments on commit f0e1014

Please sign in to comment.