RS.CLIPPY.NEEDLESS_RETURN_WITH_QUESTION_MARK

Using a return statement like `return Err(expr)?;` where removing it would suffice

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: needless_return_with_question_mark. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for return statements on Err paired with the ? operator.

Why is this bad?

The return is unnecessary.

Returns may be used to add attributes to the return expression. Return statements with attributes are therefore be accepted by this lint.

Example

fn foo(x: usize) -> Result<(), Box<dyn Error>> {
    if x == 0 {
        return Err(...)?;
    }
    Ok(())
}

simplify to

fn foo(x: usize) -> Result<(), Box<dyn Error>> {
    if x == 0 {
        Err(...)?;
    }
    Ok(())
}

if paired with try_err, use instead:

fn foo(x: usize) -> Result<(), Box<dyn Error>> {
    if x == 0 {
        return Err(...);
    }
    Ok(())
}