Skip to content

Commit 00a2d7a

Browse files
committed
Fix bad suggestion that needs curly braces for match_single_binding lint
1 parent 1d3c539 commit 00a2d7a

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

clippy_lints/src/matches.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ fn find_bool_lit(ex: &ExprKind<'_>, desugared: bool) -> Option<bool> {
13531353
}
13541354
}
13551355

1356+
#[allow(clippy::too_many_lines)]
13561357
fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], expr: &Expr<'_>) {
13571358
if in_macro(expr.span) || arms.len() != 1 || is_refutable(cx, arms[0].pat) {
13581359
return;
@@ -1427,7 +1428,18 @@ fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[A
14271428
indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
14281429
cbrace_start = format!("{{\n{}", indent);
14291430
}
1430-
};
1431+
}
1432+
// If the parent is already an arm, and the body is another match statement,
1433+
// we need curly braces around suggestion
1434+
let parent_node_id = cx.tcx.hir().get_parent_node(expr.hir_id);
1435+
if let Node::Arm(arm) = &cx.tcx.hir().get(parent_node_id) {
1436+
if let ExprKind::Match(..) = arm.body.kind {
1437+
cbrace_end = format!("\n{}}}", indent);
1438+
// Fix body indent due to the match
1439+
indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1440+
cbrace_start = format!("{{\n{}", indent);
1441+
}
1442+
}
14311443
(
14321444
expr.span,
14331445
format!(

tests/ui/match_single_binding.fixed

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,33 @@ fn main() {
115115
// =>
116116
_ => println!("Not an array index start"),
117117
}
118+
// Lint (additional curly braces needed, see #6572)
119+
struct AppendIter<I>
120+
where
121+
I: Iterator,
122+
{
123+
inner: Option<(I, <I as Iterator>::Item)>,
124+
}
125+
126+
#[allow(dead_code)]
127+
fn size_hint<I: Iterator>(iter: &AppendIter<I>) -> (usize, Option<usize>) {
128+
match &iter.inner {
129+
Some((iter, _item)) => {
130+
let (min, max) = iter.size_hint();
131+
(min.saturating_add(1), max.and_then(|max| max.checked_add(1)))
132+
},
133+
None => (0, Some(0)),
134+
}
135+
}
136+
// Lint (no additional curly braces needed)
137+
let opt = Some((5, 2));
138+
let get_tup = || -> (i32, i32) { (1, 2) };
139+
match opt {
140+
#[rustfmt::skip]
141+
Some((first, _second)) => {
142+
let (a, b) = get_tup();
143+
println!("a {:?} and b {:?}", a, b);
144+
},
145+
None => println!("nothing"),
146+
}
118147
}

tests/ui/match_single_binding.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,33 @@ fn main() {
132132
// =>
133133
_ => println!("Not an array index start"),
134134
}
135+
// Lint (additional curly braces needed, see #6572)
136+
struct AppendIter<I>
137+
where
138+
I: Iterator,
139+
{
140+
inner: Option<(I, <I as Iterator>::Item)>,
141+
}
142+
143+
#[allow(dead_code)]
144+
fn size_hint<I: Iterator>(iter: &AppendIter<I>) -> (usize, Option<usize>) {
145+
match &iter.inner {
146+
Some((iter, _item)) => match iter.size_hint() {
147+
(min, max) => (min.saturating_add(1), max.and_then(|max| max.checked_add(1))),
148+
},
149+
None => (0, Some(0)),
150+
}
151+
}
152+
// Lint (no additional curly braces needed)
153+
let opt = Some((5, 2));
154+
let get_tup = || -> (i32, i32) { (1, 2) };
155+
match opt {
156+
#[rustfmt::skip]
157+
Some((first, _second)) => {
158+
match get_tup() {
159+
(a, b) => println!("a {:?} and b {:?}", a, b),
160+
}
161+
},
162+
None => println!("nothing"),
163+
}
135164
}

tests/ui/match_single_binding.stderr

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,36 @@ LL | | _ => println!("Single branch"),
178178
LL | | }
179179
| |_____^ help: consider using the match body instead: `println!("Single branch");`
180180

181-
error: aborting due to 12 previous errors
181+
error: this match could be written as a `let` statement
182+
--> $DIR/match_single_binding.rs:146:36
183+
|
184+
LL | Some((iter, _item)) => match iter.size_hint() {
185+
| ____________________________________^
186+
LL | | (min, max) => (min.saturating_add(1), max.and_then(|max| max.checked_add(1))),
187+
LL | | },
188+
| |_____________^
189+
|
190+
help: consider using `let` statement
191+
|
192+
LL | Some((iter, _item)) => {
193+
LL | let (min, max) = iter.size_hint();
194+
LL | (min.saturating_add(1), max.and_then(|max| max.checked_add(1)))
195+
LL | },
196+
|
197+
198+
error: this match could be written as a `let` statement
199+
--> $DIR/match_single_binding.rs:158:13
200+
|
201+
LL | / match get_tup() {
202+
LL | | (a, b) => println!("a {:?} and b {:?}", a, b),
203+
LL | | }
204+
| |_____________^
205+
|
206+
help: consider using `let` statement
207+
|
208+
LL | let (a, b) = get_tup();
209+
LL | println!("a {:?} and b {:?}", a, b);
210+
|
211+
212+
error: aborting due to 14 previous errors
182213

0 commit comments

Comments
 (0)