rust-lang / rust-lang/rust-clippy
Suggest replacing `.unwrap_or_else(|| panic!("..."))` with `.expect("...")` on `Option<T: Debug>`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Makes this roundabout way of writing .expect actually an expect call where it's feasible.
Advantage
The code is shorter and more straightforward in what it does, and IMHO more idiomatic.
Drawbacks
The parameter to expect is evaluated even when it doesn't end up being used.
Cf. unwrap_or vs. unwrap_or_else.
Not a big deal for literals, but could be significant in rare cases.
Example
This is a straight-up real-world example. I can trim it down upon request.
full_output.platform_output.cursor_icon = player
.ui()
.downcast_ref::<DesktopUiBackend>()
.unwrap_or_else(|| panic!("UI Backend should be DesktopUiBackend")) // <--
.cursor();
Could be written as:
full_output.platform_output.cursor_icon = player
.ui()
.downcast_ref::<DesktopUiBackend>()
.expect!("UI Backend should be DesktopUiBackend") // <--
.cursor();
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked example in desktop/src/gui/controller.rs and compare the shown unwrap_or_else/panic form with the proposed expect call. Then locate the relevant Clippy lint entry point and tests; done means the lint handles feasible Option<T: Debug> cases and covers the transformation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100