Skip to content

Commit 10a8327

Browse files
committed
match_single_binding: allow macros in scrutinee and patterns
1 parent 8eed350 commit 10a8327

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

clippy_lints/src/matches/match_single_binding.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::HirNode;
3-
use clippy_utils::source::{indent_of, snippet, snippet_block_with_context, snippet_with_applicability};
3+
use clippy_utils::source::{indent_of, snippet, snippet_block_with_context, snippet_with_context};
44
use clippy_utils::{get_parent_expr, is_refutable, peel_blocks};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind, StmtKind};
@@ -24,16 +24,10 @@ 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 mut snippet_body = snippet_block_with_context(
28-
cx,
29-
match_body.span,
30-
arms[0].span.ctxt(),
31-
"..",
32-
Some(expr.span),
33-
&mut app,
34-
)
35-
.0
36-
.to_string();
27+
let ctxt = expr.span.ctxt();
28+
let mut snippet_body = snippet_block_with_context(cx, match_body.span, ctxt, "..", Some(expr.span), &mut app)
29+
.0
30+
.to_string();
3731

3832
// Do we need to add ';' to suggestion ?
3933
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)
@@ -77,10 +71,10 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
7771
span,
7872
format!(
7973
"let {} = {};\n{}let {} = {snippet_body};",
80-
snippet_with_applicability(cx, bind_names, "..", &mut app),
81-
snippet_with_applicability(cx, matched_vars, "..", &mut app),
74+
snippet_with_context(cx, bind_names, ctxt, "..", &mut app).0,
75+
snippet_with_context(cx, matched_vars, ctxt, "..", &mut app).0,
8276
" ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
83-
snippet_with_applicability(cx, pat_span, "..", &mut app)
77+
snippet_with_context(cx, pat_span, ctxt, "..", &mut app).0
8478
),
8579
),
8680
None => {
@@ -204,14 +198,17 @@ fn sugg_with_curlies<'a>(
204198
s
205199
});
206200

201+
let ctxt = match_expr.span.ctxt();
207202
let scrutinee = if needs_var_binding {
208203
format!(
209204
"let {} = {}",
210-
snippet_with_applicability(cx, bind_names, "..", applicability),
211-
snippet_with_applicability(cx, matched_vars, "..", applicability)
205+
snippet_with_context(cx, bind_names, ctxt, "..", applicability).0,
206+
snippet_with_context(cx, matched_vars, ctxt, "..", applicability).0
212207
)
213208
} else {
214-
snippet_with_applicability(cx, matched_vars, "..", applicability).to_string()
209+
snippet_with_context(cx, matched_vars, ctxt, "..", applicability)
210+
.0
211+
.to_string()
215212
};
216213

217214
format!("{cbrace_start}{scrutinee};\n{indent}{assignment_str}{snippet_body}{cbrace_end}")

tests/ui/match_single_binding.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,20 @@ fn issue_10447() -> usize {
171171

172172
2
173173
}
174+
175+
fn issue14634() {
176+
macro_rules! id {
177+
($i:ident) => {
178+
$i
179+
};
180+
}
181+
dbg!(3);
182+
println!("here");
183+
//~^^^ match_single_binding
184+
let id!(a) = dbg!(3);
185+
println!("found {a}");
186+
//~^^^ match_single_binding
187+
let id!(b) = dbg!(3);
188+
let id!(_a) = dbg!(b + 1);
189+
//~^^^ match_single_binding
190+
}

tests/ui/match_single_binding.rs

+20
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,23 @@ fn issue_10447() -> usize {
229229

230230
2
231231
}
232+
233+
fn issue14634() {
234+
macro_rules! id {
235+
($i:ident) => {
236+
$i
237+
};
238+
}
239+
match dbg!(3) {
240+
_ => println!("here"),
241+
}
242+
//~^^^ match_single_binding
243+
match dbg!(3) {
244+
id!(a) => println!("found {a}"),
245+
}
246+
//~^^^ match_single_binding
247+
let id!(_a) = match dbg!(3) {
248+
id!(b) => dbg!(b + 1),
249+
};
250+
//~^^^ match_single_binding
251+
}

tests/ui/match_single_binding.stderr

+43-1
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,47 @@ LL | | _ => println!("1"),
336336
LL | | },
337337
| |_________^ help: consider using the match body instead: `println!("1")`
338338

339-
error: aborting due to 24 previous errors
339+
error: this match could be replaced by its scrutinee and body
340+
--> tests/ui/match_single_binding.rs:239:5
341+
|
342+
LL | / match dbg!(3) {
343+
LL | | _ => println!("here"),
344+
LL | | }
345+
| |_____^
346+
|
347+
help: consider using the scrutinee and body instead
348+
|
349+
LL ~ dbg!(3);
350+
LL + println!("here");
351+
|
352+
353+
error: this match could be written as a `let` statement
354+
--> tests/ui/match_single_binding.rs:243:5
355+
|
356+
LL | / match dbg!(3) {
357+
LL | | id!(a) => println!("found {a}"),
358+
LL | | }
359+
| |_____^
360+
|
361+
help: consider using a `let` statement
362+
|
363+
LL ~ let id!(a) = dbg!(3);
364+
LL + println!("found {a}");
365+
|
366+
367+
error: this match could be written as a `let` statement
368+
--> tests/ui/match_single_binding.rs:247:5
369+
|
370+
LL | / let id!(_a) = match dbg!(3) {
371+
LL | | id!(b) => dbg!(b + 1),
372+
LL | | };
373+
| |______^
374+
|
375+
help: consider using a `let` statement
376+
|
377+
LL ~ let id!(b) = dbg!(3);
378+
LL + let id!(_a) = dbg!(b + 1);
379+
|
380+
381+
error: aborting due to 27 previous errors
340382

0 commit comments

Comments
 (0)