Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix [match_single_binding] suggestion introduced an extra semicolon #10060

Merged
merged 1 commit into from
Dec 29, 2022
Merged
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
18 changes: 5 additions & 13 deletions clippy_lints/src/matches/match_single_binding.rs
Original file line number Diff line number Diff line change
@@ -31,19 +31,11 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
};

// Do we need to add ';' to suggestion ?
match match_body.kind {
ExprKind::Block(block, _) => {
// macro + expr_ty(body) == ()
if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
snippet_body.push(';');
}
},
_ => {
// expr_ty(body) == ()
if cx.typeck_results().expr_ty(match_body).is_unit() {
snippet_body.push(';');
}
},
if let ExprKind::Block(block, _) = match_body.kind {
// macro + expr_ty(body) == ()
if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
snippet_body.push(';');
}
}

let mut applicability = Applicability::MaybeIncorrect;
13 changes: 13 additions & 0 deletions tests/ui/match_single_binding.fixed
Original file line number Diff line number Diff line change
@@ -133,3 +133,16 @@ fn issue_9575() {
println!("Needs curlies");
};
}

#[allow(dead_code)]
fn issue_9725(r: Option<u32>) {
let x = r;
match x {
Some(_) => {
println!("Some");
},
None => {
println!("None");
},
};
}
14 changes: 14 additions & 0 deletions tests/ui/match_single_binding.rs
Original file line number Diff line number Diff line change
@@ -148,3 +148,17 @@ fn issue_9575() {
_ => println!("Needs curlies"),
};
}

#[allow(dead_code)]
fn issue_9725(r: Option<u32>) {
match r {
x => match x {
Some(_) => {
println!("Some");
},
None => {
println!("None");
},
},
};
}
27 changes: 26 additions & 1 deletion tests/ui/match_single_binding.stderr
Original file line number Diff line number Diff line change
@@ -213,5 +213,30 @@ LL + println!("Needs curlies");
LL ~ };
|

error: aborting due to 14 previous errors
error: this match could be written as a `let` statement
--> $DIR/match_single_binding.rs:154:5
|
LL | / match r {
LL | | x => match x {
LL | | Some(_) => {
LL | | println!("Some");
... |
LL | | },
LL | | };
| |_____^
|
help: consider using a `let` statement
|
LL ~ let x = r;
LL + match x {
LL + Some(_) => {
LL + println!("Some");
LL + },
LL + None => {
LL + println!("None");
LL + },
LL ~ };
|

error: aborting due to 15 previous errors