Skip to content

Commit a33930f

Browse files
committed
Fix single-match-else in the presence of macros expressions expanding to blocks
1 parent a161d3f commit a33930f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

clippy_lints/src/matches.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::ast::LitKind;
1010
use syntax::codemap::Span;
1111
use utils::paths;
1212
use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty,
13-
is_expn_of};
13+
is_expn_of, remove_blocks};
1414
use utils::sugg::Sugg;
1515

1616
/// **What it does:** Checks for matches with a single arm where an `if let`
@@ -179,11 +179,12 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
179179
if arms.len() == 2 &&
180180
arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
181181
arms[1].pats.len() == 1 && arms[1].guard.is_none() {
182-
let els = if is_unit_expr(&arms[1].body) {
182+
let els = remove_blocks(&arms[1].body);
183+
let els = if is_unit_expr(els) {
183184
None
184-
} else if let ExprBlock(_) = arms[1].body.node {
185+
} else if let ExprBlock(_) = els.node {
185186
// matches with blocks that contain statements are prettier as `if let + else`
186-
Some(&*arms[1].body)
187+
Some(els)
187188
} else {
188189
// allow match arms with just expressions
189190
return;

tests/run-pass/single-match-else.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
#![warn(single_match_else)]
4+
5+
fn main() {
6+
let n = match (42, 43) {
7+
(42, n) => n,
8+
_ => panic!("typeck error"),
9+
};
10+
assert_eq!(n, 43);
11+
}

0 commit comments

Comments
 (0)