diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java index 703ef11c6fd..60dbdab8506 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java @@ -1,7 +1,7 @@ package tech.picnic.errorprone.bugpatterns; import static com.google.errorprone.BugPattern.LinkType.CUSTOM; -import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; import static com.google.errorprone.BugPattern.StandardTags.PERFORMANCE; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; @@ -17,22 +17,22 @@ import com.sun.source.tree.MethodInvocationTree; /** - * A {@link BugChecker} that flags usages of Mongo $text filters used for full text searches. + * A {@link BugChecker} that flags usages of Mongo {@code $text} filters used for full text + * searches. * * @see Mongo Text Search */ @AutoService(BugChecker.class) @BugPattern( - summary = - "Usage of Mongo for full-text search queries via the `$text` operator is discouraged.", + summary = "Avoid the `$text` operator in Mongo's full-text search queries", link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", linkType = CUSTOM, - severity = WARNING, + severity = SUGGESTION, tags = PERFORMANCE) public final class MongoFullTextSearchQueryUsage extends BugChecker implements MethodInvocationTreeMatcher { private static final long serialVersionUID = 1L; - private static final Matcher TEXT_FILTER_INVOCATION = + private static final Matcher MONGO_FILTERS_TEXT_METHOD = staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); /** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */ @@ -40,9 +40,8 @@ public MongoFullTextSearchQueryUsage() {} @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - if (!TEXT_FILTER_INVOCATION.matches(tree, state)) { - return Description.NO_MATCH; - } - return describeMatch(tree); + return MONGO_FILTERS_TEXT_METHOD.matches(tree, state) + ? describeMatch(tree) + : Description.NO_MATCH; } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java index 9138797020a..ad474628803 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java @@ -32,7 +32,6 @@ public enum ThirdPartyLibrary { * @see Guava on GitHub */ GUAVA("com.google.common.collect.ImmutableList"), - /** * New Relic's Java agent API. * diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java index fb5e28c5efd..2677c0dbb42 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java @@ -11,16 +11,16 @@ void identification() { "A.java", "import com.mongodb.client.model.Filters;", "import com.mongodb.client.model.TextSearchOptions;", - "import org.bson.conversions.Bson;", "", "class A {", - "", " void m() {", - " Bson allowed = Filters.eq(\"a\", \"b\");", + " Filters.eq(\"foo\", \"bar\");", + " // BUG: Diagnostic contains:", + " Filters.text(\"foo\");", " // BUG: Diagnostic contains:", - " Bson textSearch = Filters.text(\"Some text\");", + " Filters.text(\"foo\", new TextSearchOptions());", " // BUG: Diagnostic contains:", - " Bson textSearch2 = Filters.text(\"Some text\", new TextSearchOptions().caseSensitive(true));", + " Filters.text(\"foo\", new TextSearchOptions().caseSensitive(true));", " }", "}") .doTest(); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java index 053dc76f17d..090ef579b51 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibraryTest.java @@ -23,8 +23,7 @@ void isIntroductionAllowed() { CompilationTestHelper.newInstance(TestChecker.class, getClass()) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", "class A {}") .doTest(); } @@ -39,8 +38,7 @@ void isIntroductionAllowedWitnessClassesInSymtab() { "import org.assertj.core.api.Assertions;", "import reactor.core.publisher.Flux;", "", - "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: true, GUAVA: true, NEW_RELIC_AGENT_API: true, REACTOR: true", "class A {", " void m(Class clazz) {", " m(Assertions.class);", @@ -58,8 +56,7 @@ void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() { .withClasspath(ImmutableList.class, Flux.class) .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false,", - "// REACTOR: true", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: true, NEW_RELIC_AGENT_API: false, REACTOR: true", "class A {}") .doTest(); } @@ -70,8 +67,7 @@ void isIntroductionAllowedWitnessClassesNotOnClassPath() { .withClasspath() .addSourceLines( "A.java", - "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false,", - "// REACTOR:", + "// BUG: Diagnostic contains: ASSERTJ: false, GUAVA: false, NEW_RELIC_AGENT_API: false, REACTOR:", "// false", "class A {}") .doTest();