-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
CharSequenceRules
Refaster rule collection
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...r-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CharSequenceRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...b/src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../src/test/resources/tech/picnic/errorprone/refasterrules/CharSequenceRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |