`must_use` when matching on a `Result<MustUse, _>`

Do you think the following should warn, or counted as being explicitly ignored? The current behavior is that they don't warn.

#[must_use = "You must use me"]
struct UseMe;
#[derive(Debug)]
struct Error;

fn main() {
    //use_my_ret().unwrap(); // <- warns
    // The following don't warn
    if let Err(e) = use_my_ret() {
        eprintln!("Error: {e:?}");
    }
    match use_my_ret() {
        Err(e) => {
            eprintln!("Error: {e:?}");
        }
        _ => {}
    }
}

fn use_my_ret() -> Result<UseMe, Error> {
    Ok(UseMe)
}

I personally believe counting these as "explicitly ignoring" is not ideal, because they focus on the Err variant, rather than the must_use value inside Ok. Ideally, the user would have to deal with the value inside Ok explicitly. For example:

    match use_my_ret() {
        Err(e) => {
            eprintln!("Error: {e:?}");
        }
        Ok(_) => {} // <- Acceptable, because the user has to at least reach inside the `Ok` to ignore it
    }

Pre-RFC: `#[must_use]` on a Result's Ok type seems related.

8 Likes