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

Suggest replacing .unwrap_or_else(|| panic!("...")) with .expect("...") on Option<T: Debug> #11271

Open
torokati44 opened this issue Aug 1, 2023 · 2 comments
Labels
A-lint Area: New lints

Comments

@torokati44
Copy link

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();

(https://github.com/ruffle-rs/ruffle/blob/2621dd78eefab17e98dc03fafa25ed421784a712/desktop/src/gui/controller.rs#L213)

Could be written as:

                full_output.platform_output.cursor_icon = player
                    .ui()
                    .downcast_ref::<DesktopUiBackend>()
                    .expect!("UI Backend should be DesktopUiBackend") // <--
                    .cursor();
@torokati44 torokati44 added the A-lint Area: New lints label Aug 1, 2023
@torokati44
Copy link
Author

I think this may be related: #19

@Centri3
Copy link
Member

Centri3 commented Aug 1, 2023

This could also lint unreachable! and friends as well, though I tend to write that instead to convey it's 100% unreachable. Could be a configuration option as I do think in any particularly serious projects expect should convey the same meaning as unreachable!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants