Skip to content

Avoid single_match lint in macro rules #5397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}