Attribute Macro auto_unwrap::auto_unwrap

source ·
#[auto_unwrap]
Expand description

Automatically replaces every instance of the ? operator with .unwrap()

Will not preform this replacement for statements following an #[ignore]

Example

use auto_unwrap::auto_unwrap;

#[auto_unwrap]
fn fn_1() -> i32 {
    let s = "does it detect this question mark? (no)";
    println!("{}", s);
    let x: Result<i32, ()> = Ok(23);
    return x?; // gets replaced with x.unwrap();
}

assert_eq!(fn_1(), 23);
use auto_unwrap::auto_unwrap;

#[auto_unwrap]
fn fn_2() {
    #[skip_auto_default] // skips until (and including) the next brace-delimited group or semicolon
    let closure = || -> Result<u32, f32> {
        let ok: Result<u32, f32> = Ok(1);
        assert_eq!(ok?, ok.unwrap());

        let err: Result<u32, f32> = Err(2.0);
        assert_eq!(err?, err.unwrap()); // without the skip this would panic!

        Ok(2)
    };

    assert_eq!(closure(), Err(2.0));
}