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 Option::map/etc over match wherever it improves clarity #52658

Merged
merged 4 commits into from
Jul 25, 2018

Conversation

Wallacoloo
Copy link
Contributor

This isn't intended to change behavior anywhere. A lot of times statements like match x { None => None, Some(y) => [...] } can be rewritten using Option::map or Option::and_then in a way that preserves or improves clarity, so that's what I've done here.

I think it's particularly valuable to keep things in libcore and libstd pretty/idiomatic since it's not uncommon to follow the [src] links when browsing the rust-lang.org docs for std/core. If there's any concern about pushing style-based changes though, I'll happily back out the non-std/core commits here.

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 24, 2018
@cramertj
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 24, 2018

📌 Commit cbe5f1c has been approved by cramertj

@bors bors 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 Jul 24, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jul 24, 2018
…s, r=cramertj

Prefer `Option::map`/etc over `match` wherever it improves clarity

This isn't intended to change behavior anywhere. A lot of times statements like `match x { None => None, Some(y) => [...] }` can be rewritten using `Option::map` or `Option::and_then` in a way that preserves or improves clarity, so that's what I've done here.

I think it's particularly valuable to keep things in `libcore` and `libstd` pretty/idiomatic since it's not uncommon to follow the `[src]` links when browsing the rust-lang.org docs for std/core. If there's any concern about pushing style-based changes though, I'll happily back out the non-std/core commits here.
self.iter.next_back().map(|ch| {
let index = self.front_offset + self.iter.iter.len();
(index, ch)
})
Copy link
Member

Choose a reason for hiding this comment

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

The other possibility for code like this is to let ch = self.iter.next_back()?;, though I don't know if that's better, nor if it's canonical style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooh, I forgot ? had been extended to support more than just Result types. If you want, I can spin up another PR for that - as it looks like these commits were just merged - but otherwise I suppose I'll just leave this be.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not confident that it's better, so it's probably not worth bothering with changing -- going to something other than the manual match, as you did, is the important part here.

bors added a commit that referenced this pull request Jul 25, 2018
Rollup of 7 pull requests

Successful merges:

 - #52391 (Add unaligned volatile intrinsics)
 - #52402 (impl PartialEq+Eq for BuildHasherDefault)
 - #52645 (Allow declaring existential types inside blocks)
 - #52656 (Stablize Redox Unix Sockets)
 - #52658 (Prefer `Option::map`/etc over `match` wherever it improves clarity)
 - #52668 (clarify pointer offset function safety concerns)
 - #52677 (Release notes: add some missing 1.28 libs stabilization)

Failed merges:

r? @ghost
@bors bors merged commit cbe5f1c into rust-lang:master Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

None yet

5 participants