Remove return statements at the end of a block

Rust pattern

It is more idiomatic to remove the return keyword and the semicolon.


Apply with the Grit CLI
grit apply no_needless_return

Removes return at the end of a block

BEFORE
fn foo(x: usize) -> usize {
    println!("Hi!");
    return x;
}
AFTER
fn foo(x: usize) -> usize {
    println!("Hi!");
    x
}

Does not remove return in the middle of a block

BEFORE
fn foo(x: usize) -> usize {
    if x == 0 {
        return x + 1;
    }
    return x;
}
AFTER
fn foo(x: usize) -> usize {
    if x == 0 {
        return x + 1;
    }
    x
}

Removes ending return with complex expression

BEFORE
fn foo(x: usize) -> usize {
    println!("Hi!");
    return if x == 0 {
        x + 1
    } else {
        x
    };
}
AFTER
fn foo(x: usize) -> usize {
    println!("Hi!");
    if x == 0 {
        x + 1
    } else {
        x
    }
}