From 35081366c40a4f280907a8427835be2b02524f1c Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Thu, 4 Jan 2024 18:48:44 +0100 Subject: [PATCH 1/4] Extend `OptionalIdentity` Refaster rule --- .../tech/picnic/errorprone/refasterrules/OptionalRules.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java index a8eb97b452..2f0607d536 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java @@ -388,6 +388,8 @@ static final class OptionalIdentity { @BeforeTemplate Optional before(Optional optional, Comparator comparator) { return Refaster.anyOf( + optional.or(() -> Optional.empty()), + optional.or(Optional::empty), optional.stream().findFirst(), optional.stream().findAny(), optional.stream().min(comparator), From 8feb3d4ee134efe80c965dbcd0ef6c12f83523f5 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Thu, 4 Jan 2024 18:55:02 +0100 Subject: [PATCH 2/4] Add tests --- .../refasterrules/OptionalRulesTestInput.java | 10 ++++++---- .../refasterrules/OptionalRulesTestOutput.java | 7 ++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java index c09719bea5..652c7c6a61 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java @@ -118,10 +118,12 @@ ImmutableSet> testOptionalOrOtherOptional() { ImmutableSet> testOptionalIdentity() { return ImmutableSet.of( - Optional.of("foo").stream().findFirst(), - Optional.of("bar").stream().findAny(), - Optional.of("baz").stream().min(String::compareTo), - Optional.of("qux").stream().max(String::compareTo)); + Optional.of("foo").or(() -> Optional.empty()), + Optional.of("bar").or(Optional::empty), + Optional.of("baz").stream().findFirst(), + Optional.of("qux").stream().findAny(), + Optional.of("corge").stream().min(String::compareTo), + Optional.of("grault").stream().max(String::compareTo)); } ImmutableSet> testOptionalFilter() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java index 8881529247..3506eb477e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java @@ -115,7 +115,12 @@ ImmutableSet> testOptionalOrOtherOptional() { ImmutableSet> testOptionalIdentity() { return ImmutableSet.of( - Optional.of("foo"), Optional.of("bar"), Optional.of("baz"), Optional.of("qux")); + Optional.of("foo"), + Optional.of("bar"), + Optional.of("baz"), + Optional.of("qux"), + Optional.of("corge"), + Optional.of("grault")); } ImmutableSet> testOptionalFilter() { From 7181751a8aa37d8a1fe697ca8ff21d59179f384b Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Thu, 4 Jan 2024 19:02:32 +0100 Subject: [PATCH 3/4] Is this notiation prefered? --- .../tech/picnic/errorprone/refasterrules/OptionalRules.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java index 2f0607d536..1491b18eac 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/OptionalRules.java @@ -388,8 +388,7 @@ static final class OptionalIdentity { @BeforeTemplate Optional before(Optional optional, Comparator comparator) { return Refaster.anyOf( - optional.or(() -> Optional.empty()), - optional.or(Optional::empty), + optional.or(Refaster.anyOf(() -> Optional.empty(), Optional::empty)), optional.stream().findFirst(), optional.stream().findAny(), optional.stream().min(comparator), From ca1132e53930116504cc4353f68b484bc18a1b25 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Fri, 5 Jan 2024 08:05:43 +0100 Subject: [PATCH 4/4] Tweak --- .../errorprone/refasterrules/OptionalRulesTestInput.java | 4 ++-- .../errorprone/refasterrules/OptionalRulesTestOutput.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java index 652c7c6a61..20eb7fe221 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestInput.java @@ -122,8 +122,8 @@ ImmutableSet> testOptionalIdentity() { Optional.of("bar").or(Optional::empty), Optional.of("baz").stream().findFirst(), Optional.of("qux").stream().findAny(), - Optional.of("corge").stream().min(String::compareTo), - Optional.of("grault").stream().max(String::compareTo)); + Optional.of("quux").stream().min(String::compareTo), + Optional.of("quuz").stream().max(String::compareTo)); } ImmutableSet> testOptionalFilter() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java index 3506eb477e..cf589e9d94 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/OptionalRulesTestOutput.java @@ -119,8 +119,8 @@ ImmutableSet> testOptionalIdentity() { Optional.of("bar"), Optional.of("baz"), Optional.of("qux"), - Optional.of("corge"), - Optional.of("grault")); + Optional.of("quux"), + Optional.of("quuz")); } ImmutableSet> testOptionalFilter() {