dtolnay/case-studies

Recommend using match instead of let for autoref specialization

Kestrer opened this issue · 1 comments

The problem with a macro definition such as:

macro_rules! anyhow {
    ($err:expr) => ({
        #[allow(unused_imports)]
        use $crate::{DisplayKind, StdErrorKind};
        let error = $err;
        (&error).anyhow_kind().new(error)
    });
}

Is that it won't work if $err relies on temporaries (e.g. it is format_args!). However, the following will work:

macro_rules! anyhow {
    ($err:expr) => ({
        #[allow(unused_imports)]
        use $crate::{DisplayKind, StdErrorKind};
        match $err {
            error => (&error).anyhow_kind().new(error),
        }
    });
}

as it's just one expression.

Good call, fixed. The reason this didn't come up in the real-world case in anyhow is that anyhow::Error requires a 'static argument for other reasons anyway.