Skip to content
Merged
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
2 changes: 1 addition & 1 deletion clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ fn check_if_try_match<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
let mut sugg = reindent_multiline(&arm_body_snippet, true, Some(indent));
let binding_snippet = snippet_with_applicability(cx, binding_span, "..", &mut applicability);
let inner_indent = " ".repeat(indent + 4);
if matches!(arm_body.kind, ExprKind::Block(..)) {
if matches!(arm_body.kind, ExprKind::Block(..)) && sugg.starts_with('{') {
sugg.insert_str(
1,
&format!("\n{inner_indent}let {binding_snippet} = {scrutinee_snippet}?;"),
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,14 @@ fn issue16751(mut v: Option<usize>) -> Option<usize> {
if n > 10 { Some(42) } else { None }
}
}

fn issue_destructuring_assignment() -> Option<(i32, i32)> {
let mut a = 0i32;
let mut b = 0i32;
let opt: Option<(i32, i32)> = Some((1, 2));
{
let x = opt?;
(a, b) = x
}
Some((a, b))
}
12 changes: 12 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,15 @@ fn issue16751(mut v: Option<usize>) -> Option<usize> {
None => return None,
}
}

fn issue_destructuring_assignment() -> Option<(i32, i32)> {
let mut a = 0i32;
let mut b = 0i32;
let opt: Option<(i32, i32)> = Some((1, 2));
match opt {
//~^ question_mark
Some(x) => (a, b) = x,
None => return None,
}
Some((a, b))
}
20 changes: 19 additions & 1 deletion tests/ui/question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,23 @@ LL + if n > 10 { Some(42) } else { None }
LL + }
|

error: aborting due to 46 previous errors
error: this `match` expression can be replaced with `?`
--> tests/ui/question_mark.rs:718:5
|
LL | / match opt {
LL | |
LL | | Some(x) => (a, b) = x,
LL | | None => return None,
LL | | }
| |_____^
|
help: try instead
|
LL ~ {
LL + let x = opt?;
LL + (a, b) = x
LL + }
|

error: aborting due to 47 previous errors

Loading