Skip to content

Commit 9e7782b

Browse files
authored
Fix match_single_binding misses curlies on type signatures (#15017)
Closes rust-lang/rust-clippy#14991 ---- changelog: [`match_single_binding`] fix missing curlies on type signatures
2 parents 3af333e + b5eac24 commit 9e7782b

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

clippy_lints/src/matches/match_single_binding.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::HirNode;
33
use clippy_utils::source::{indent_of, snippet, snippet_block_with_context, snippet_with_context};
4-
use clippy_utils::{get_parent_expr, is_refutable, peel_blocks};
4+
use clippy_utils::{is_refutable, peel_blocks};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind, StmtKind};
77
use rustc_lint::LateContext;
88
use rustc_span::Span;
99

1010
use super::MATCH_SINGLE_BINDING;
1111

12+
#[derive(Debug)]
1213
enum AssignmentExpr {
1314
Assign { span: Span, match_span: Span },
1415
Local { span: Span, pat_span: Span },
@@ -160,6 +161,17 @@ fn opt_parent_assign_span<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<Ass
160161
None
161162
}
162163

164+
fn expr_parent_requires_curlies<'a>(cx: &LateContext<'a>, match_expr: &Expr<'a>) -> bool {
165+
let parent = cx.tcx.parent_hir_node(match_expr.hir_id);
166+
matches!(
167+
parent,
168+
Node::Expr(Expr {
169+
kind: ExprKind::Closure { .. },
170+
..
171+
}) | Node::AnonConst(..)
172+
)
173+
}
174+
163175
fn sugg_with_curlies<'a>(
164176
cx: &LateContext<'a>,
165177
(ex, match_expr): (&Expr<'a>, &Expr<'a>),
@@ -172,9 +184,7 @@ fn sugg_with_curlies<'a>(
172184
let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
173185

174186
let (mut cbrace_start, mut cbrace_end) = (String::new(), String::new());
175-
if let Some(parent_expr) = get_parent_expr(cx, match_expr)
176-
&& let ExprKind::Closure { .. } = parent_expr.kind
177-
{
187+
if expr_parent_requires_curlies(cx, match_expr) {
178188
cbrace_end = format!("\n{indent}}}");
179189
// Fix body indent due to the closure
180190
indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));

tests/ui/match_single_binding.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,19 @@ fn issue14634() {
188188
let id!(_a) = dbg!(b + 1);
189189
//~^^^ match_single_binding
190190
}
191+
192+
mod issue14991 {
193+
struct AnnoConstWOBlock {
194+
inner: [(); {
195+
let _n = 1;
196+
42
197+
}],
198+
}
199+
200+
struct AnnoConstWBlock {
201+
inner: [(); {
202+
let _n = 1;
203+
42
204+
}],
205+
}
206+
}

tests/ui/match_single_binding.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,21 @@ fn issue14634() {
249249
};
250250
//~^^^ match_single_binding
251251
}
252+
253+
mod issue14991 {
254+
struct AnnoConstWOBlock {
255+
inner: [(); match 1 {
256+
//~^ match_single_binding
257+
_n => 42,
258+
}],
259+
}
260+
261+
struct AnnoConstWBlock {
262+
inner: [(); {
263+
match 1 {
264+
//~^ match_single_binding
265+
_n => 42,
266+
}
267+
}],
268+
}
269+
}

tests/ui/match_single_binding.stderr

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,5 +378,38 @@ LL ~ let id!(b) = dbg!(3);
378378
LL + let id!(_a) = dbg!(b + 1);
379379
|
380380

381-
error: aborting due to 27 previous errors
381+
error: this match could be written as a `let` statement
382+
--> tests/ui/match_single_binding.rs:255:21
383+
|
384+
LL | inner: [(); match 1 {
385+
| _____________________^
386+
LL | |
387+
LL | | _n => 42,
388+
LL | | }],
389+
| |_________^
390+
|
391+
help: consider using a `let` statement
392+
|
393+
LL ~ inner: [(); {
394+
LL + let _n = 1;
395+
LL + 42
396+
LL ~ }],
397+
|
398+
399+
error: this match could be written as a `let` statement
400+
--> tests/ui/match_single_binding.rs:263:13
401+
|
402+
LL | / match 1 {
403+
LL | |
404+
LL | | _n => 42,
405+
LL | | }
406+
| |_____________^
407+
|
408+
help: consider using a `let` statement
409+
|
410+
LL ~ let _n = 1;
411+
LL + 42
412+
|
413+
414+
error: aborting due to 29 previous errors
382415

0 commit comments

Comments
 (0)