-
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 (#1480)
Resolves #1394.
- Loading branch information
Showing
7 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...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,33 @@ | ||
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. | ||
*/ | ||
// 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 charSequence) { | ||
return Refaster.anyOf( | ||
charSequence.length() == 0, charSequence.length() <= 0, charSequence.length() < 1); | ||
} | ||
|
||
@AfterTemplate | ||
@AlsoNegation | ||
boolean after(CharSequence charSequence) { | ||
return charSequence.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
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( | ||
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); | ||
} | ||
} |
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( | ||
new StringBuilder("foo").isEmpty(), | ||
new StringBuilder("bar").isEmpty(), | ||
new StringBuilder("baz").isEmpty(), | ||
!new StringBuilder("qux").isEmpty(), | ||
!new StringBuilder("quux").isEmpty(), | ||
!new StringBuilder("corge").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
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