Skip to content

Commit

Permalink
Suggestions and partially revert unrelated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie authored and Stephan202 committed Jul 23, 2023
1 parent 4e23e2e commit 772f8db
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,32 +17,31 @@
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 <a href="https://www.mongodb.com/docs/manual/text-search/">Mongo Text Search</a>
*/
@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<ExpressionTree> TEXT_FILTER_INVOCATION =
private static final Matcher<ExpressionTree> MONGO_FILTERS_TEXT_METHOD =
staticMethod().onClass("com.mongodb.client.model.Filters").named("text");

/** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public enum ThirdPartyLibrary {
* @see <a href="https://github.com/google/guava">Guava on GitHub</a>
*/
GUAVA("com.google.common.collect.ImmutableList"),

/**
* New Relic's Java agent API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);",
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down

0 comments on commit 772f8db

Please sign in to comment.