-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[FlowSensitive] [StatusOr] [14/N] Support nested StatusOrs #170950
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
Merged
fmayer
merged 11 commits into
main
from
users/fmayer/spr/flowsensitive-statusor-15n-support-nested-statusors
Dec 10, 2025
+207
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
06aaba5
[𝘀𝗽𝗿] changes to main this commit is based on
fmayer 698cd1d
[𝘀𝗽𝗿] initial version
fmayer 24d0cf0
[𝘀𝗽𝗿] changes introduced through rebase
fmayer 09c625d
rebase
fmayer 53835d4
[𝘀𝗽𝗿] changes introduced through rebase
fmayer 629660f
rebase\
fmayer 8e8d92c
[𝘀𝗽𝗿] changes introduced through rebase
fmayer affe730
update
fmayer 454f37b
types
fmayer f02146d
[𝘀𝗽𝗿] changes introduced through rebase
razvanlupusoru 382c828
rebase
fmayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3419,6 +3419,15 @@ TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOr) { | |
| result.value(); | ||
| } | ||
| )cc"); | ||
| ExpectDiagnosticsFor(R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| void target() { | ||
| absl::StatusOr<STATUSOR_INT> result = Make<STATUSOR_INT>(); | ||
| if (result.value().ok()) | ||
| result.value().value(); | ||
| } | ||
| )cc"); | ||
| } | ||
|
|
||
| TEST_P(UncheckedStatusOrAccessModelTest, PtrConstruct) { | ||
|
|
@@ -3739,6 +3748,168 @@ TEST_P(UncheckedStatusOrAccessModelTest, UniquePtrReset) { | |
| )cc"); | ||
| } | ||
|
|
||
| TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOrInStatusOrStruct) { | ||
| // Non-standard assignment with a nested StatusOr. | ||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Inner { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| struct Outer { | ||
| absl::StatusOr<Inner> inner; | ||
| }; | ||
|
|
||
| void target() { | ||
| Outer foo = Make<Outer>(); | ||
| foo.inner->sor = "a"; // [[unsafe]] | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(const absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo->sor.ok()) foo->sor.value(); | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(const absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo.value().sor.ok()) foo->sor.value(); | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(const absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && (*foo).sor.ok()) (*foo).sor.value(); | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo->sor.ok()) *foo->sor; | ||
| } | ||
| )cc"); | ||
| // With assignment. | ||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo->sor.ok()) { | ||
| foo->sor = Make<absl::StatusOr<std::string>>(); | ||
| foo->sor.value(); // [[unsafe]] | ||
| } | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo->sor.ok()) { | ||
| auto n = Make<absl::StatusOr<std::string>>(); | ||
| if (n.ok()) foo->sor = n; | ||
| foo->sor.value(); | ||
| } | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo) { | ||
| if (foo.ok() && foo->sor.ok()) { | ||
| auto n = Make<absl::StatusOr<std::string>>(); | ||
| if (n.ok()) foo->sor = std::move(n); | ||
| foo->sor.value(); | ||
| } | ||
| } | ||
| )cc"); | ||
|
|
||
| // More complicated conditionals. | ||
| ExpectDiagnosticsFor( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a comment to split this from the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo) { | ||
| if (!foo.ok()) return; | ||
| if (!foo->sor.ok()) | ||
| foo->sor.value(); // [[unsafe]] | ||
| else | ||
| foo->sor.value(); | ||
| } | ||
| )cc"); | ||
|
|
||
| ExpectDiagnosticsFor( | ||
| R"cc( | ||
| #include "unchecked_statusor_access_test_defs.h" | ||
|
|
||
| struct Foo { | ||
| absl::StatusOr<std::string> sor; | ||
| }; | ||
|
|
||
| void target(absl::StatusOr<Foo>& foo, bool b) { | ||
| if (!foo.ok()) return; | ||
| if (b) { | ||
| if (!foo->sor.ok()) return; | ||
| foo->sor.value(); | ||
| } else { | ||
| if (!foo->sor.ok()) return; | ||
| foo->sor.value(); | ||
| } | ||
| foo->sor.value(); | ||
| } | ||
| )cc"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::string | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are probably most common for nesting, but could it be possible to have a
foo.value().value()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done