-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Recommends lazily evaluated alternatives for Option::or
and Result::or
#46548
Conversation
the use of lazily evaluated alternatives when appropriate. These comments are intended to echo a clippy lint on the same topic (see https://rust-lang-nursery.github.io/rust-clippy/master/index.html#or_fun_call)
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libcore/option.rs
Outdated
@@ -338,6 +338,10 @@ impl<T> Option<T> { | |||
|
|||
/// Returns the contained value or a default. | |||
/// | |||
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing | |||
/// the result of a function call, it is recommended to use `unwrap_or_else`, |
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.
Could you link to the unwrap_or_else
method? i.e.
/// ... it is recommended to use [`unwrap_or_else`],
///
/// ...
///
/// [`unwrap_or_else`]: #method.unwrap_or_else
Similar for the other additions.
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.
sure - will do
src/libcore/result.rs
Outdated
@@ -690,6 +694,10 @@ impl<T, E> Result<T, E> { | |||
/// Unwraps a result, yielding the content of an [`Ok`]. | |||
/// Else, it returns `optb`. | |||
/// | |||
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing |
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.
There is a trailing whitespace on this line, causing the CI to reject this PR. Other places affected:
[00:04:39] tidy error: /checkout/src/libcore/result.rs:697: trailing whitespace
[00:04:39] tidy error: /checkout/src/libcore/result.rs:698: trailing whitespace
[00:04:39] tidy error: /checkout/src/libcore/result.rs:699: trailing whitespace
[00:04:39] tidy error: /checkout/src/libcore/option.rs:341: trailing whitespace
[00:04:39] tidy error: /checkout/src/libcore/option.rs:342: trailing whitespace
[00:04:39] tidy error: /checkout/src/libcore/option.rs:343: trailing whitespace
[00:04:41] some tidy checks failed
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.
fixed
@bors r+ rollup |
📌 Commit 5847d0b has been approved by |
Recommends lazily evaluated alternatives for `Option::or` and `Result::or` Adds language to docs for `Option` and `Result` recommending the use of lazily evaluated alternatives when appropriate. These comments are intended to echo a [clippy lint] on the same topic. The [reddit discussion] may also be of interest. [clippy lint]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#or_fun_call [reddit discussion]: https://www.reddit.com/r/rust/comments/7hutqn/perils_of_optionor_and_resultor/
…JohnTitor Add a warning about `Option/Result::and()` being eagerly evaluated Copied from `or()`. Inspired by [this StackOverflow question](https://stackoverflow.com/questions/73461846/why-is-in-rust-the-expression-in-option-and-evaluated-if-option-is-none). [The PR for `or()`](rust-lang#46548) mentions the Clippy lint `or_fun_call` which doesn't exist for `and()` (although there is `unnecessary_lazy_evaluations`). I still think this warning is also good for `and()`. Feel free to close if you disagree.
Add a warning about `Option/Result::and()` being eagerly evaluated Copied from `or()`. Inspired by [this StackOverflow question](https://stackoverflow.com/questions/73461846/why-is-in-rust-the-expression-in-option-and-evaluated-if-option-is-none). [The PR for `or()`](rust-lang/rust#46548) mentions the Clippy lint `or_fun_call` which doesn't exist for `and()` (although there is `unnecessary_lazy_evaluations`). I still think this warning is also good for `and()`. Feel free to close if you disagree.
Adds language to docs for
Option
andResult
recommending the use of lazily evaluated alternatives when appropriate. These comments are intended to echo a clippy lint on the same topic. The reddit discussion may also be of interest.