Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Optional::orElseThrow over Optional::get #2950

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ public void test() {
"public class Test {",
" Stream<String> f1(Stream<Collection<Optional<String>>> in) {",
" return in.flatMap(Collection::stream)"
+ ".filter(Optional::isPresent).map(Optional::get);",
+ ".filter(Optional::isPresent).map(Optional::orElseThrow);",
" }",
" Stream<String> f2(Stream<Collection<Optional<String>>> in) {",
" return in.flatMap(list -> list.stream()"
+ ".filter(Optional::isPresent).map(Optional::get));",
+ ".filter(Optional::isPresent).map(Optional::orElseThrow));",
" }",
" Stream<String> f3(Stream<Collection<Optional<String>>> in) {",
" return in.flatMap(list -> list.stream())"
+ ".filter(Optional::isPresent).map(Optional::get);",
+ ".filter(Optional::isPresent).map(Optional::orElseThrow);",
" }",
" Stream<String> f4(Stream<Optional<Optional<String>>> 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();
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2950.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Prefer Optional::orElseThrow over Optional::get
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: Do we also want a more generic PreferOptionalOrElseThrow check to rewrite Optional::get & opt.get() to Optional::orElseThrow & opt.orElseThrow()

Copy link
Member

@pkoenig10 pkoenig10 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favor of this. Especially to fix consumers that have already applied StreamFlatMapOptional.

links:
- https://github.com/palantir/gradle-baseline/pull/2950