Skip to content

Commit 555f568

Browse files
committed
Fix semicolon insertion in match_single_binding
1 parent e65ad6f commit 555f568

6 files changed

+192
-44
lines changed

clippy_lints/src/matches/match_single_binding.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::macros::HirNode;
33
use clippy_utils::source::{indent_of, snippet, snippet_block_with_context, snippet_with_applicability};
44
use clippy_utils::{get_parent_expr, is_refutable, peel_blocks};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind};
6+
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind, StmtKind};
77
use rustc_lint::LateContext;
88
use rustc_span::Span;
99

@@ -24,22 +24,27 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
2424
let bind_names = arms[0].pat.span;
2525
let match_body = peel_blocks(arms[0].body);
2626
let mut app = Applicability::MaybeIncorrect;
27-
let (snippet_body, from_macro) = snippet_block_with_context(
27+
let mut snippet_body = snippet_block_with_context(
2828
cx,
2929
match_body.span,
3030
arms[0].span.ctxt(),
3131
"..",
3232
Some(expr.span),
3333
&mut app,
34-
);
35-
let mut snippet_body = snippet_body.to_string();
34+
)
35+
.0
36+
.to_string();
3637

3738
// Do we need to add ';' to suggestion ?
38-
if matches!(match_body.kind, ExprKind::Block(..)) {
39-
// macro + expr_ty(body) == ()
40-
if from_macro && cx.typeck_results().expr_ty(match_body).is_unit() {
41-
snippet_body.push(';');
39+
if let Node::Stmt(stmt) = cx.tcx.hir().get_parent(expr.hir_id)
40+
&& let StmtKind::Expr(_) = stmt.kind
41+
&& match match_body.kind {
42+
// We don't need to add a ; to blocks, unless that block is from a macro expansion
43+
ExprKind::Block(block, _) => block.span.from_expansion(),
44+
_ => true,
4245
}
46+
{
47+
snippet_body.push(';');
4348
}
4449

4550
match arms[0].pat.kind {

tests/ui/match_single_binding.fixed

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::match_single_binding)]
3-
#![allow(unused_variables)]
4-
#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
3+
#![allow(
4+
unused,
5+
clippy::let_unit_value,
6+
clippy::no_effect,
7+
clippy::toplevel_ref_arg,
8+
clippy::uninlined_format_args
9+
)]
510

611
struct Point {
712
x: i32,
@@ -109,10 +114,9 @@ fn main() {
109114

110115
// Lint
111116
let x = 1;
112-
println!("Not an array index start");
117+
println!("Not an array index start")
113118
}
114119

115-
#[allow(dead_code)]
116120
fn issue_8723() {
117121
let (mut val, idx) = ("a b", 1);
118122

@@ -125,16 +129,15 @@ fn issue_8723() {
125129
let _ = val;
126130
}
127131

128-
#[allow(dead_code)]
132+
fn side_effects() {}
133+
129134
fn issue_9575() {
130-
fn side_effects() {}
131135
let _ = || {
132136
side_effects();
133-
println!("Needs curlies");
137+
println!("Needs curlies")
134138
};
135139
}
136140

137-
#[allow(dead_code)]
138141
fn issue_9725(r: Option<u32>) {
139142
let x = r;
140143
match x {
@@ -146,3 +149,25 @@ fn issue_9725(r: Option<u32>) {
146149
},
147150
};
148151
}
152+
153+
fn issue_10447() -> usize {
154+
();
155+
156+
let a = ();
157+
158+
side_effects();
159+
160+
let b = side_effects();
161+
162+
println!("1");
163+
164+
let c = println!("1");
165+
166+
let in_expr = [
167+
(),
168+
side_effects(),
169+
println!("1"),
170+
];
171+
172+
2
173+
}

tests/ui/match_single_binding.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// run-rustfix
22
#![warn(clippy::match_single_binding)]
3-
#![allow(unused_variables)]
4-
#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
3+
#![allow(
4+
unused,
5+
clippy::let_unit_value,
6+
clippy::no_effect,
7+
clippy::toplevel_ref_arg,
8+
clippy::uninlined_format_args
9+
)]
510

611
struct Point {
712
x: i32,
@@ -127,7 +132,6 @@ fn main() {
127132
}
128133
}
129134

130-
#[allow(dead_code)]
131135
fn issue_8723() {
132136
let (mut val, idx) = ("a b", 1);
133137

@@ -141,15 +145,14 @@ fn issue_8723() {
141145
let _ = val;
142146
}
143147

144-
#[allow(dead_code)]
148+
fn side_effects() {}
149+
145150
fn issue_9575() {
146-
fn side_effects() {}
147151
let _ = || match side_effects() {
148152
_ => println!("Needs curlies"),
149153
};
150154
}
151155

152-
#[allow(dead_code)]
153156
fn issue_9725(r: Option<u32>) {
154157
match r {
155158
x => match x {
@@ -162,3 +165,43 @@ fn issue_9725(r: Option<u32>) {
162165
},
163166
};
164167
}
168+
169+
fn issue_10447() -> usize {
170+
match 1 {
171+
_ => (),
172+
}
173+
174+
let a = match 1 {
175+
_ => (),
176+
};
177+
178+
match 1 {
179+
_ => side_effects(),
180+
}
181+
182+
let b = match 1 {
183+
_ => side_effects(),
184+
};
185+
186+
match 1 {
187+
_ => println!("1"),
188+
}
189+
190+
let c = match 1 {
191+
_ => println!("1"),
192+
};
193+
194+
let in_expr = [
195+
match 1 {
196+
_ => (),
197+
},
198+
match 1 {
199+
_ => side_effects(),
200+
},
201+
match 1 {
202+
_ => println!("1"),
203+
},
204+
];
205+
206+
2
207+
}

0 commit comments

Comments
 (0)