[question_mark]: also trigger on return statements

This commit is contained in:
y21 2023-12-21 14:01:37 +01:00
parent dd857f8207
commit dfb4ff8bbf
7 changed files with 59 additions and 15 deletions

View File

@ -63,9 +63,7 @@ pub(super) fn check_with<'tcx, F>(
return None; return None;
} }
let Some(some_expr) = get_some_expr_fn(cx, some_pat, some_expr, expr_ctxt) else { let some_expr = get_some_expr_fn(cx, some_pat, some_expr, expr_ctxt)?;
return None;
};
// These two lints will go back and forth with each other. // These two lints will go back and forth with each other.
if cx.typeck_results().expr_ty(some_expr.expr) == cx.tcx.types.unit if cx.typeck_results().expr_ty(some_expr.expr) == cx.tcx.types.unit

View File

@ -128,9 +128,7 @@ fn find_method_and_type<'tcx>(
if is_wildcard || is_rest { if is_wildcard || is_rest {
let res = cx.typeck_results().qpath_res(qpath, check_pat.hir_id); let res = cx.typeck_results().qpath_res(qpath, check_pat.hir_id);
let Some(id) = res.opt_def_id().map(|ctor_id| cx.tcx.parent(ctor_id)) else { let id = res.opt_def_id().map(|ctor_id| cx.tcx.parent(ctor_id))?;
return None;
};
let lang_items = cx.tcx.lang_items(); let lang_items = cx.tcx.lang_items();
if Some(id) == lang_items.result_ok_variant() { if Some(id) == lang_items.result_ok_variant() {
Some(("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty))) Some(("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty)))

View File

@ -96,6 +96,24 @@ enum IfBlockType<'hir> {
), ),
} }
fn find_let_else_ret_expression<'hir>(block: &'hir Block<'hir>) -> Option<&'hir Expr<'hir>> {
if let Block {
stmts: &[],
expr: Some(els),
..
} = block
{
Some(els)
} else if let [stmt] = block.stmts
&& let StmtKind::Semi(expr) = stmt.kind
&& let ExprKind::Ret(..) = expr.kind
{
Some(expr)
} else {
None
}
}
fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) { fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
if let StmtKind::Local(Local { if let StmtKind::Local(Local {
pat, pat,
@ -103,11 +121,7 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
els: Some(els), els: Some(els),
.. ..
}) = stmt.kind }) = stmt.kind
&& let Block { && let Some(els) = find_let_else_ret_expression(els)
stmts: &[],
expr: Some(els),
..
} = els
&& let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, els) && let Some(inner_pat) = pat_and_expr_can_be_question_mark(cx, pat, els)
{ {
let mut applicability = Applicability::MaybeIncorrect; let mut applicability = Applicability::MaybeIncorrect;

View File

@ -680,9 +680,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos
}) })
.filter(|(_, text)| !text.is_empty()); .filter(|(_, text)| !text.is_empty());
let Some((line_start, line)) = lines.next() else { let (line_start, line) = lines.next()?;
return None;
};
// Check for a sequence of line comments. // Check for a sequence of line comments.
if line.starts_with("//") { if line.starts_with("//") {
let (mut line, mut line_start) = (line, line_start); let (mut line, mut line_start) = (line, line_start);

View File

@ -60,3 +60,16 @@ fn foo() -> Option<()> {
Some(()) Some(())
} }
// lint not just `return None`, but also `return None;` (note the semicolon)
fn issue11993(y: Option<i32>) -> Option<i32> {
let x = y?;
// don't lint: more than one statement in the else body
let Some(x) = y else {
todo!();
return None;
};
None
}

View File

@ -65,3 +65,18 @@ fn foo() -> Option<()> {
Some(()) Some(())
} }
// lint not just `return None`, but also `return None;` (note the semicolon)
fn issue11993(y: Option<i32>) -> Option<i32> {
let Some(x) = y else {
return None;
};
// don't lint: more than one statement in the else body
let Some(x) = y else {
todo!();
return None;
};
None
}

View File

@ -53,5 +53,13 @@ error: this could be rewritten as `let...else`
LL | let v = if let Some(v_some) = g() { v_some } else { return None }; LL | let v = if let Some(v_some) = g() { v_some } else { return None };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { return None };` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Some(v) = g() else { return None };`
error: aborting due to 6 previous errors error: this `let...else` may be rewritten with the `?` operator
--> $DIR/manual_let_else_question_mark.rs:71:5
|
LL | / let Some(x) = y else {
LL | | return None;
LL | | };
| |______^ help: replace it with: `let x = y?;`
error: aborting due to 7 previous errors