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 let else with ? statement where applicable #8755

Closed
matthiaskrgr opened this issue Apr 27, 2022 · 4 comments · Fixed by #10924
Closed

suggest replacing let else with ? statement where applicable #8755

matthiaskrgr opened this issue Apr 27, 2022 · 4 comments · Fixed by #10924

Comments

@matthiaskrgr
Copy link
Member

Sorry, filed via mobil which doesn't know about issue templates. 😅

might be nice if clippy could lint patterns like this: rust-lang/rust#96471

@est31
Copy link
Member

est31 commented Apr 27, 2022

I guess the question_mark lint could be extended to handle this case? Also, maybe one needs to watch out to not suggest let-else first (#8437) to then suggest ?, but suggest ? right away. IDK how conflicts like this are handled.

@est31
Copy link
Member

est31 commented Jan 11, 2023

Another example where this lint would have been useful: rust-lang/cargo@cf716fc

@est31
Copy link
Member

est31 commented Jan 11, 2023

Given the two samples that we have, I think that most of the times the lint would fire is for functions that return Option. If we simplify for Option, that means that checking for the pattern should be pretty easy. For Result it doesn't make that much sense, as the else clause can't access the thing inside Err.

@MarijnS95
Copy link
Contributor

Yes, would love to see the question_mark lint extended to let-else, and also the mention by @est31 to not first suggest manual_let_else on a block of code, followed by question_mark on the refactored code.

We currently have two patterns in our codebase with both Option and Result where the question mark operator could/should be suggested:

    let x = None::<u32>;
    // Suggest ? here
    let Some(x) = x else {
        return None;
    };

    // Suggest .ok()? here
    let Ok(x) = std::fs::read_to_string("foo") else {
        return None;
    };

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants