-
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
PatternRules
Refaster rule collection
While there, configure `StaticImport` to not require static importing of `com.google.common.base.Predicates.contains`. The new rules triggered cleanup of some `CompilationTestHelper` usages. See google/guava#6483.
- Loading branch information
1 parent
7779631
commit 8583a5e
Showing
8 changed files
with
134 additions
and
50 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.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,46 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import static com.google.common.base.Predicates.containsPattern; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
/** Refaster rules related to code dealing with regular expressions. */ | ||
@OnlineDocumentation | ||
final class PatternRules { | ||
private PatternRules() {} | ||
|
||
/** Prefer {@link Pattern#asPredicate()} over non-JDK alternatives. */ | ||
// XXX: This rule could also replace `s -> pattern.matcher(s).find()`, though the lambda | ||
// expression may match functional interfaces other than `Predicate`. If we do add such a rule, we | ||
// should also add a rule that replaces `s -> pattern.matcher(s).matches()` with | ||
// `pattern.asMatchPredicate()`. | ||
static final class PatternAsPredicate { | ||
@BeforeTemplate | ||
Predicate<CharSequence> before(Pattern pattern) { | ||
return Predicates.contains(pattern); | ||
} | ||
|
||
@AfterTemplate | ||
Predicate<String> after(Pattern pattern) { | ||
return pattern.asPredicate(); | ||
} | ||
} | ||
|
||
/** Prefer {@link Pattern#asPredicate()} over non-JDK alternatives. */ | ||
static final class PatternCompileAsPredicate { | ||
@BeforeTemplate | ||
Predicate<CharSequence> before(String pattern) { | ||
return containsPattern(pattern); | ||
} | ||
|
||
@AfterTemplate | ||
Predicate<String> after(String pattern) { | ||
return Pattern.compile(pattern).asPredicate(); | ||
} | ||
} | ||
} |
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
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
22 changes: 22 additions & 0 deletions
22
...ontrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestInput.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,22 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.common.collect.ImmutableSet; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class PatternRulesTest implements RefasterRuleCollectionTestCase { | ||
@Override | ||
public ImmutableSet<Object> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(Predicates.class); | ||
} | ||
|
||
Predicate<?> testPatternAsPredicate() { | ||
return Predicates.contains(Pattern.compile("foo")); | ||
} | ||
|
||
Predicate<?> testPatternCompileAsPredicate() { | ||
return Predicates.containsPattern("foo"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ntrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestOutput.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,22 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.common.collect.ImmutableSet; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class PatternRulesTest implements RefasterRuleCollectionTestCase { | ||
@Override | ||
public ImmutableSet<Object> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(Predicates.class); | ||
} | ||
|
||
Predicate<?> testPatternAsPredicate() { | ||
return Pattern.compile("foo").asPredicate(); | ||
} | ||
|
||
Predicate<?> testPatternCompileAsPredicate() { | ||
return Pattern.compile("foo").asPredicate(); | ||
} | ||
} |
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