Skip to content

Commit cb9fb76

Browse files
committed
Fix semicolon insertion in match_single_binding
1 parent 56fbfe5 commit cb9fb76

File tree

6 files changed

+188
-41
lines changed

6 files changed

+188
-41
lines changed

clippy_lints/src/matches/match_single_binding.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::source::{indent_of, snippet, snippet_block, snippet_with_appli
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::{get_parent_expr, is_refutable, peel_blocks};
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind};
7+
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind, StmtKind};
88
use rustc_lint::LateContext;
99
use rustc_span::Span;
1010

@@ -31,11 +31,15 @@ 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-
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(';');
34+
if let Node::Stmt(stmt) = cx.tcx.hir().get_parent(expr.hir_id)
35+
&& let StmtKind::Expr(_) = stmt.kind
36+
&& match match_body.kind {
37+
// We don't need to add a ; to blocks, unless that block is from a macro expansion
38+
ExprKind::Block(block, _) => block.span.from_expansion(),
39+
_ => true,
3840
}
41+
{
42+
snippet_body.push(';');
3943
}
4044

4145
let mut applicability = Applicability::MaybeIncorrect;

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)