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

Lint simplifyable ok_or_else usage #4676

Open
oli-cosmian opened this issue Oct 16, 2019 · 2 comments
Open

Lint simplifyable ok_or_else usage #4676

oli-cosmian opened this issue Oct 16, 2019 · 2 comments
Assignees
Labels
A-lint Area: New lints L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@oli-cosmian
Copy link
Contributor

The following code uses String::from

fn bar() -> Result<i32, String> {
    let x = Some(52.3).ok_or_else(|| String::from("foo"))?;
    Ok(42)
}

but Some(52.3).ok_or("foo")?; would also compile

So if an ok_or_else is followed by a ? and the inner function is calling From::from (or a specific version of it), we should be able to convert it to an ok_or without that call.

@flip1995 flip1995 added L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions A-lint Area: New lints labels Oct 16, 2019
@HMPerson1
Copy link
Contributor

@rustbot claim

@7ERr0r
Copy link

7ERr0r commented Sep 10, 2023

Sometimes it's the other way.

Performance intensive code:

FromPrimitive::from_u8(mode).ok_or(
    MyReadError::Custom(Box::new(format!("unknown mode: {mode}")))
)

Should not call fmt::format, but instead:

FromPrimitive::from_u8(mode).ok_or_else(|| {
    MyReadError::Custom(Box::new(format!("unknown mode: {mode}")))
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-complexity Lint: Belongs in the complexity lint group L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants