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

return statements in else branch of let-else do not get wrapped #68

Open
SandaruKasa opened this issue Mar 30, 2023 · 0 comments
Open

Comments

@SandaruKasa
Copy link

#[throws(Error)]
fn foo(a: Option<u8>) -> u8 {
    let Some(a) = a else {
        return 0;
    };
    a
}

does not work.

You need to do

#[throws(Error)]
fn foo(a: Option<u8>) -> u8 {
    let Some(a) = a else {
        return Ok(0);
    };
    a
}

to make it compile.

In contrast, there is no need for this Ok(...) if you use if let - else:

#[throws(Error)]
fn foo(a: Option<u8>) -> u8 {
    if let Some(a) = a {
        a
    } else {
        return 0;
    }
}

But I mean, let-else breaks even rustfmt (I had to manually format the code) and github syntax highlighting (else in the first code snippet does not get highlighted as a keyword, return does not get highlighted in the first two), so yeah, a breakage in a 2yo procmacro is not really surprising. (Wonder if using a separate keyword like guard would be a better decision or having to bump the edition again would be to much of a hustle.)

Anyways, nice article (and the whole blog too):
https://without.boats/blog/why-ok-wrapping/
Btw, do you happen to know if any progress has been made on this feature in rust proper over the last two years since the blogpost? I, personally, would love to see this in Rust, but from what I can tell, it's a bit controversial with some people and definitely does not seem to be a high priority for the language right now.

Nemo157 added a commit to Nemo157/culpa that referenced this issue Jun 29, 2023
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

No branches or pull requests

1 participant