Function clippy_utils::pat_and_expr_can_be_question_mark

source ·
pub fn pat_and_expr_can_be_question_mark<'a, 'hir>(
    cx: &LateContext<'_>,
    pat: &'a Pat<'hir>,
    else_body: &Expr<'_>
) -> Option<&'a Pat<'hir>>
Expand description

Returns whether the given let pattern and else body can be turned into a question mark

For this example:

let FooBar { a, b } = if let Some(a) = ex { a } else { return None };

We get as parameters:

pat: Some(a)
else_body: return None

And for this example:

let Some(FooBar { a, b }) = ex else { return None };

We get as parameters:

pat: Some(FooBar { a, b })
else_body: return None

We output Some(a) in the first instance, and Some(FooBar { a, b }) in the second, because the question mark operator is applicable here. Callers have to check whether we are in a constant or not.