Skip to content

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

@SandaruKasa

Description

@SandaruKasa
#[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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions