From ba6a3280f52c2ad6a6bec5e8792ecd0ce2006062 Mon Sep 17 00:00:00 2001 From: pmk21 Date: Tue, 31 Mar 2020 15:49:27 +0530 Subject: [PATCH 1/4] Added test for single_match in macro --- tests/ui/single_match.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/ui/single_match.rs b/tests/ui/single_match.rs index 980ce9a0fabe..6766a6409b22 100644 --- a/tests/ui/single_match.rs +++ b/tests/ui/single_match.rs @@ -81,4 +81,13 @@ fn single_match_know_enum() { } } -fn main() {} +macro_rules! single_match { + ($num:literal) => { + match $num { + 15 => println!("15"), + _ => (), + } + }; +} + +fn main() {single_match!(5);} From 793403a2a8fc5243770f13f8f0f225f5b14a41cd Mon Sep 17 00:00:00 2001 From: pmk21 Date: Tue, 31 Mar 2020 15:49:49 +0530 Subject: [PATCH 2/4] Added test for single_match_else in macro --- tests/ui/single_match_else.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/ui/single_match_else.rs b/tests/ui/single_match_else.rs index 37a99de8832c..34193be0b75e 100644 --- a/tests/ui/single_match_else.rs +++ b/tests/ui/single_match_else.rs @@ -18,4 +18,18 @@ fn unwrap_addr() -> Option<&'static ExprNode> { } } -fn main() {} +macro_rules! unwrap_addr { + ($expression:expr) => { + match $expression { + ExprNode::ExprAddrOf => Some(&NODE), + _ => { + let x = 5; + None + }, + } + }; +} + +fn main() { + unwrap_addr!(ExprNode::Unicorns); +} From 4cac9786c527bf4f9e14bfc03e8b5adf5cef2b1d Mon Sep 17 00:00:00 2001 From: pmk21 Date: Tue, 31 Mar 2020 15:50:15 +0530 Subject: [PATCH 3/4] Skip single_match lints in macro rules --- clippy_lints/src/matches.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index a124bf8b8db9..20b793f95ded 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -447,6 +447,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches { #[rustfmt::skip] fn check_single_match(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) { if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() { + if in_macro(expr.span) { + // Don't lint match expressions present in + // macro_rules! block + return; + } if let PatKind::Or(..) = arms[0].pat.kind { // don't lint for or patterns for now, this makes // the lint noisy in unnecessary situations From 79ab05458fb8eea29f1ebf9c15e73de9326eb0d5 Mon Sep 17 00:00:00 2001 From: pmk21 Date: Tue, 31 Mar 2020 16:13:51 +0530 Subject: [PATCH 4/4] Small formatting change --- tests/ui/single_match.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/ui/single_match.rs b/tests/ui/single_match.rs index 6766a6409b22..1c55af5dfb67 100644 --- a/tests/ui/single_match.rs +++ b/tests/ui/single_match.rs @@ -90,4 +90,6 @@ macro_rules! single_match { }; } -fn main() {single_match!(5);} +fn main() { + single_match!(5); +}