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

Recommends lazily evaluated alternatives for Option::or and Result::or #46548

Merged
merged 2 commits into from
Dec 8, 2017

Conversation

jonathanstrong
Copy link

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.

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)
@rust-highfive
Copy link
Collaborator

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.

@@ -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`,
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

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

sure - will do

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Dec 7, 2017
@@ -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
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@dtolnay
Copy link
Member

dtolnay commented Dec 7, 2017

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Dec 7, 2017

📌 Commit 5847d0b has been approved by dtolnay

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 7, 2017
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Dec 7, 2017
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/
bors added a commit that referenced this pull request Dec 8, 2017
Rollup of 5 pull requests

- Successful merges: #46416, #46444, #46526, #46539, #46548
- Failed merges:
@bors bors merged commit 5847d0b into rust-lang:master Dec 8, 2017
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Aug 24, 2022
…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.
workingjubilee pushed a commit to tcdi/postgrestd that referenced this pull request Sep 15, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants