Skip to content

Commit 22af8fe

Browse files
committed
Auto merge of rust-lang#10060 - alex-semenyuk:match_single_binding_fix, r=llogiq
Fix [match_single_binding] suggestion introduced an extra semicolon Fix rust-lang#9725 --- changelog: [`match_single_binding`]: suggestion no longer introduces unneeded semicolons [rust-lang#10060](rust-lang/rust-clippy#10060) <!-- changelog_checked -->
2 parents 266eef7 + 3b6bbf7 commit 22af8fe

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

clippy_lints/src/matches/match_single_binding.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,11 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
3131
};
3232

3333
// Do we need to add ';' to suggestion ?
34-
match match_body.kind {
35-
ExprKind::Block(block, _) => {
36-
// macro + expr_ty(body) == ()
37-
if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
38-
snippet_body.push(';');
39-
}
40-
},
41-
_ => {
42-
// expr_ty(body) == ()
43-
if cx.typeck_results().expr_ty(match_body).is_unit() {
44-
snippet_body.push(';');
45-
}
46-
},
34+
if let ExprKind::Block(block, _) = match_body.kind {
35+
// macro + expr_ty(body) == ()
36+
if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
37+
snippet_body.push(';');
38+
}
4739
}
4840

4941
let mut applicability = Applicability::MaybeIncorrect;

tests/ui/match_single_binding.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,16 @@ fn issue_9575() {
133133
println!("Needs curlies");
134134
};
135135
}
136+
137+
#[allow(dead_code)]
138+
fn issue_9725(r: Option<u32>) {
139+
let x = r;
140+
match x {
141+
Some(_) => {
142+
println!("Some");
143+
},
144+
None => {
145+
println!("None");
146+
},
147+
};
148+
}

tests/ui/match_single_binding.rs

+14
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,17 @@ fn issue_9575() {
148148
_ => println!("Needs curlies"),
149149
};
150150
}
151+
152+
#[allow(dead_code)]
153+
fn issue_9725(r: Option<u32>) {
154+
match r {
155+
x => match x {
156+
Some(_) => {
157+
println!("Some");
158+
},
159+
None => {
160+
println!("None");
161+
},
162+
},
163+
};
164+
}

tests/ui/match_single_binding.stderr

+26-1
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,30 @@ LL + println!("Needs curlies");
213213
LL ~ };
214214
|
215215

216-
error: aborting due to 14 previous errors
216+
error: this match could be written as a `let` statement
217+
--> $DIR/match_single_binding.rs:154:5
218+
|
219+
LL | / match r {
220+
LL | | x => match x {
221+
LL | | Some(_) => {
222+
LL | | println!("Some");
223+
... |
224+
LL | | },
225+
LL | | };
226+
| |_____^
227+
|
228+
help: consider using a `let` statement
229+
|
230+
LL ~ let x = r;
231+
LL + match x {
232+
LL + Some(_) => {
233+
LL + println!("Some");
234+
LL + },
235+
LL + None => {
236+
LL + println!("None");
237+
LL + },
238+
LL ~ };
239+
|
240+
241+
error: aborting due to 15 previous errors
217242

0 commit comments

Comments
 (0)