From a373a277cd6d6d79f8e80f38aba7abbfde0e1f8b Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 11 May 2023 07:38:06 +0200 Subject: [PATCH] Introduce `PatternRules` Refaster rule collection While there, configure `StaticImport` to not require static importing of `com.google.common.base.Predicates.contains`. See google/guava#6483. --- .../errorprone/bugpatterns/StaticImport.java | 1 + .../refasterrules/PatternRules.java | 46 +++++++++++++++++++ .../refasterrules/PatternRulesTestInput.java | 22 +++++++++ .../refasterrules/PatternRulesTestOutput.java | 22 +++++++++ 4 files changed, 91 insertions(+) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PatternRulesTestOutput.java diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java index 6063562002f..c5b3e524277 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java @@ -155,6 +155,7 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa @VisibleForTesting static final ImmutableSetMultimap STATIC_IMPORT_EXEMPTED_MEMBERS = ImmutableSetMultimap.builder() + .put("com.google.common.base.Predicates", "contains") .put("com.mongodb.client.model.Filters", "empty") .putAll( "java.util.Collections", 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..39ad88262b4 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PatternRules.java @@ -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 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 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(); + } +}