From 2e411c254a0be731c257e2fe3c2b251935a10cdb Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 7 Dec 2022 18:31:41 +0100 Subject: [PATCH] Prevent `ReverseOrder` Refaster rule from introducing a static import This is a workaround for the issue resolved by google/error-prone#3584. After application of this Refaster rule, any static imports of `java.util.Collections.reverseOrder` are obsolete. These can be removed by running Google Java Format or Error Prone's `RemoveUnusedImports` check. Where possible, subsequent application of the `StaticImport` checker will statically import `java.util.Comparator.reverseOrder`. --- .../tech/picnic/errorprone/refasterrules/ComparatorRules.java | 3 ++- .../errorprone/refasterrules/ComparatorRulesTestOutput.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java index 438fbe8b6c2..c15b466aa46 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ComparatorRules.java @@ -62,8 +62,9 @@ Comparator before() { Comparator.naturalOrder().reversed()); } + // XXX: Add `@UseImportPolicy(STATIC_IMPORT_ALWAYS)` if/when + // https://github.com/google/error-prone/pull/3584 is merged and released. @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) Comparator after() { return reverseOrder(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java index 743b859c598..9c27527ddfb 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ComparatorRulesTestOutput.java @@ -32,7 +32,8 @@ ImmutableSet> testNaturalOrder() { } ImmutableSet> testReverseOrder() { - return ImmutableSet.of(reverseOrder(), reverseOrder(), reverseOrder()); + return ImmutableSet.of( + Comparator.reverseOrder(), Comparator.reverseOrder(), Comparator.reverseOrder()); } ImmutableSet> testCustomComparator() {