Search code examples
rustrust-clippy

how to rephrase this code to get past clippy::manual_let_else


after some upgrade of msrv I get this (In a project I dont own, just doing the msrv upgrade, so removing the 'pedantic' is not OK)

error: this could be rewritten as `let...else`
   --> asyncgit/src/sync/config.rs:99:2
    |
99  | /     let entry = match entry_res {
100 | |         Ok(ent) => ent,
101 | |         Err(_) => return Ok(None),
102 | |     };
    | |______^ help: consider writing: `let Ok(ent) = entry_res else { return Ok(None) };`

on this code

let entry_res = cfg.get_entry(key);

let entry = match entry_res {
    Ok(ent) => ent,
    Err(_) => return Ok(None),
 };

get_entry returns a Result<T,...>

The suggested fix is not valid code - unless I missed something obvious

Edit. When I say not valid code, I mean it won't compile, I am sure the syntax is correct and that it's my fault for not being able to follow what it's trying to do. But it's for sure a clippy bug

EDIT2 : apparently a known bug https://github.com/rust-lang/rust-clippy/issues/10171


Solution

  • The suggested fix is not valid code - unless I missed something obvious

    The suggested fix seems to have a pretty simple error (which you should report): it should be

    let Ok(entry) = entry_res else { return Ok(None) };
    

    Otherwise it creates the wrong top-level binding (ent rather than entry).

    Aside from that, the suggested code has been valid code since 1.65.0, released November 2022 (and longer than that on nightly).

    Though it is possible clippy does not properly take MSRV in account, I've no idea.