diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.java new file mode 100644 index 00000000000..d8922258dba --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.java @@ -0,0 +1,44 @@ +package tech.picnic.errorprone.refasterrules; + +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 before(Pattern pattern) { + return Predicates.contains(pattern); + } + + @AfterTemplate + Predicate after(Pattern pattern) { + return pattern.asPredicate(); + } + } + + /** Prefer {@link Pattern#asPredicate()} over non-JDK alternatives. */ + static final class PatternCompileAsPredicate { + @BeforeTemplate + Predicate before(String pattern) { + return Predicates.containsPattern(pattern); + } + + @AfterTemplate + Predicate after(String pattern) { + return Pattern.compile(pattern).asPredicate(); + } + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestInput.java new file mode 100644 index 00000000000..2b19d9ea887 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestInput.java @@ -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 elidedTypesAndStaticImports() { + return ImmutableSet.of(Predicates.class); + } + + Predicate testPatternAsPredicate() { + return Predicates.contains(Pattern.compile("foo")); + } + + Predicate testPatternCompileAsPredicate() { + return Predicates.containsPattern("foo"); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestOutput.java new file mode 100644 index 00000000000..9e774fd4742 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestOutput.java @@ -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 elidedTypesAndStaticImports() { + return ImmutableSet.of(Predicates.class); + } + + Predicate testPatternAsPredicate() { + return Pattern.compile("foo").asPredicate(); + } + + Predicate testPatternCompileAsPredicate() { + return Pattern.compile("foo").asPredicate(); + } +}