From 406f1b32c14bc1d2a4bbbc3de4386aa6bd320f66 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Thu, 25 Jan 2024 08:57:33 +0100 Subject: [PATCH] Disallow static import of {Mono,Flux} and partially allow `empty` --- .../picnic/errorprone/bugpatterns/NonStaticImport.java | 7 +++++-- .../picnic/errorprone/bugpatterns/NonStaticImportTest.java | 3 +++ .../picnic/errorprone/bugpatterns/StaticImportTest.java | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonStaticImport.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonStaticImport.java index 47d4d6471da..459097d2ae8 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonStaticImport.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonStaticImport.java @@ -33,6 +33,7 @@ import java.time.ZoneOffset; import java.util.Collections; import java.util.Locale; +import java.util.Optional; import java.util.regex.Pattern; import org.jspecify.annotations.Nullable; import tech.picnic.errorprone.bugpatterns.util.SourceCode; @@ -71,7 +72,9 @@ public final class NonStaticImport extends BugChecker implements CompilationUnit Strings.class.getCanonicalName(), VisitorState.class.getCanonicalName(), ZoneOffset.class.getCanonicalName(), - "com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode"); + "com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode", + "reactor.core.publisher.Flux", + "reactor.core.publisher.Mono"); /** * Type members that should never be statically imported. @@ -103,6 +106,7 @@ public final class NonStaticImport extends BugChecker implements CompilationUnit "sort", "swap") .put(Locale.class.getCanonicalName(), "ROOT") + .put(Optional.class.getCanonicalName(), "empty") .putAll(Pattern.class.getCanonicalName(), "compile", "matches", "quote") .put(Predicates.class.getCanonicalName(), "contains") .put("org.springframework.http.MediaType", "ALL") @@ -125,7 +129,6 @@ public final class NonStaticImport extends BugChecker implements CompilationUnit "builder", "copyOf", "create", - "empty", "from", "getDefaultInstance", "INSTANCE", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonStaticImportTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonStaticImportTest.java index bdad493c4bb..bfa02b583ce 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonStaticImportTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonStaticImportTest.java @@ -65,6 +65,8 @@ void identification() { "// BUG: Diagnostic contains:", "import static java.util.Optional.empty;", "import static pkg.A.WithMethodThatIsSelectivelyFlagged.list;", + "// BUG: Diagnostic contains:", + "import static reactor.core.publisher.Flux.just;", "", "import com.google.common.collect.ImmutableList;", "import com.google.common.collect.ImmutableSet;", @@ -90,6 +92,7 @@ void identification() { " Locale english = ENGLISH;", " Locale root = ROOT;", " empty();", + " just();", "", " list();", " new INSTANCE();", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java index 4dde83f6e1c..9ac112a7a6a 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java @@ -47,6 +47,7 @@ void identification() { "import com.google.common.collect.ImmutableSet;", "import com.google.errorprone.refaster.ImportPolicy;", "import com.google.errorprone.refaster.annotation.UseImportPolicy;", + "import com.mongodb.client.model.Filters;", "import java.nio.charset.StandardCharsets;", "import java.time.ZoneOffset;", "import java.util.Optional;", @@ -99,6 +100,8 @@ void identification() { " // BUG: Diagnostic contains:", " MediaType t2 = MediaType.APPLICATION_JSON;", "", + " // BUG: Diagnostic contains:", + " Filters.empty();", " Optional.empty();", "", " // BUG: Diagnostic contains:",