Skip to content

Commit

Permalink
Auto merge of rust-lang#5397 - pmk21:macro-single-match, r=flip1995
Browse files Browse the repository at this point in the history
Avoid single_match lint in macro rules

changelog: none
fixes rust-lang#5359
  • Loading branch information
bors committed Mar 31, 2020
2 parents 1cac2f9 + 79ab054 commit 09fe163
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ fn single_match_know_enum() {
}
}

fn main() {}
macro_rules! single_match {
($num:literal) => {
match $num {
15 => println!("15"),
_ => (),
}
};
}

fn main() {
single_match!(5);
}
16 changes: 15 additions & 1 deletion tests/ui/single_match_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 09fe163

Please sign in to comment.