diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StreamFlatMapOptional.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StreamFlatMapOptional.java index 7aa6231f4..3c3d52583 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StreamFlatMapOptional.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StreamFlatMapOptional.java @@ -39,7 +39,7 @@ link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", linkType = BugPattern.LinkType.CUSTOM, severity = BugPattern.SeverityLevel.WARNING, - summary = "`Stream.filter(Optional::isPresent).map(Optional::get)` is more efficient than " + summary = "`Stream.filter(Optional::isPresent).map(Optional::orElseThrow)` is more efficient than " + "`Stream.flatMap(Optional::stream)`") public final class StreamFlatMapOptional extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { private static final long serialVersionUID = 1L; @@ -67,7 +67,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState if (receiver != null) { SuggestedFix.Builder fix = SuggestedFix.builder(); String optionalType = SuggestedFixes.qualifyType(state, fix, Optional.class.getCanonicalName()); - String replacement = ".filter(" + optionalType + "::isPresent).map(" + optionalType + "::get)"; + String replacement = ".filter(" + optionalType + "::isPresent).map(" + optionalType + "::orElseThrow)"; return buildDescription(tree) .addFix(fix.replace(state.getEndPosition(receiver), state.getEndPosition(tree), replacement) .build()) diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StreamFlatmapOptionalTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StreamFlatmapOptionalTest.java index 24fd9e05b..f969aff73 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StreamFlatmapOptionalTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StreamFlatmapOptionalTest.java @@ -48,19 +48,19 @@ public void test() { "public class Test {", " Stream f1(Stream>> in) {", " return in.flatMap(Collection::stream)" - + ".filter(Optional::isPresent).map(Optional::get);", + + ".filter(Optional::isPresent).map(Optional::orElseThrow);", " }", " Stream f2(Stream>> in) {", " return in.flatMap(list -> list.stream()" - + ".filter(Optional::isPresent).map(Optional::get));", + + ".filter(Optional::isPresent).map(Optional::orElseThrow));", " }", " Stream f3(Stream>> in) {", " return in.flatMap(list -> list.stream())" - + ".filter(Optional::isPresent).map(Optional::get);", + + ".filter(Optional::isPresent).map(Optional::orElseThrow);", " }", " Stream f4(Stream>> in) {", - " return in.filter(Optional::isPresent).map(Optional::get)" - + ".filter(Optional::isPresent).map(Optional::get);", + " return in.filter(Optional::isPresent).map(Optional::orElseThrow)" + + ".filter(Optional::isPresent).map(Optional::orElseThrow);", " }", "}") .doTest(); diff --git a/changelog/@unreleased/pr-2950.v2.yml b/changelog/@unreleased/pr-2950.v2.yml new file mode 100644 index 000000000..c2ed612cf --- /dev/null +++ b/changelog/@unreleased/pr-2950.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Prefer Optional::orElseThrow over Optional::get + links: + - https://github.com/palantir/gradle-baseline/pull/2950