Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr:

let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
let requires_semi = matches!(cx.tcx.parent_hir_node(expr.hir_id), Node::Stmt(_));
let parent = cx.tcx.parent_hir_node(expr.hir_id);
let requires_semi = matches!(parent, Node::Stmt(_)) || cx.typeck_results().expr_ty(expr).is_unit();
let method_call_str = match by_ref {
ByRef::Yes(_, Mutability::Mut) => ".as_mut()",
ByRef::Yes(_, Mutability::Not) => ".as_ref()",
Expand All @@ -512,7 +513,7 @@ fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr:
"{receiver_str}{method_call_str}?{}",
if requires_semi { ";" } else { "" }
);
if is_else_clause(cx.tcx, expr) {
if is_else_clause(cx.tcx, expr) || (requires_semi && !matches!(parent, Node::Stmt(_) | Node::Block(_))) {
sugg = format!("{{ {sugg} }}");
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,16 @@ fn issue16429(b: i32) -> Option<i32> {

Some(0)
}

fn issue16654() -> Result<(), i32> {
let result = func_returning_result();

#[allow(clippy::collapsible_if)]
if true {
result?;
}

_ = [{ result?; }];

Ok(())
}
19 changes: 19 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,22 @@ fn issue16429(b: i32) -> Option<i32> {

Some(0)
}

fn issue16654() -> Result<(), i32> {
let result = func_returning_result();

#[allow(clippy::collapsible_if)]
if true {
if let Err(err) = result {
//~^ question_mark
return Err(err);
}
}

_ = [if let Err(err) = result {
//~^ question_mark
return Err(err);
}];

Ok(())
}
21 changes: 20 additions & 1 deletion tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,24 @@ LL | | return None;
LL | | };
| |_____^ help: replace it with: `{ a? }`

error: aborting due to 38 previous errors
error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:658:9
|
LL | / if let Err(err) = result {
LL | |
LL | | return Err(err);
LL | | }
| |_________^ help: replace it with: `result?;`

error: this block may be rewritten with the `?` operator
--> tests/ui/question_mark.rs:664:10
|
LL | _ = [if let Err(err) = result {
| __________^
LL | |
LL | | return Err(err);
LL | | }];
| |_____^ help: replace it with: `{ result?; }`

error: aborting due to 40 previous errors

Loading