Skip to content

Commit bbe0342

Browse files
committed
if_let_chain: rename to if_chain, allow mixing if-lets with normal ifs
1 parent 79bf774 commit bbe0342

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/returns.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,18 @@ impl ReturnPass {
6767
// Check for "let x = EXPR; x"
6868
fn check_let_return(&mut self, cx: &Context, block: &Block) {
6969
// we need both a let-binding stmt and an expr
70-
if_let_chain! {
70+
if_chain! {
7171
[
72-
Some(stmt) = block.stmts.last(),
73-
StmtDecl(ref decl, _) = stmt.node,
74-
DeclLocal(ref local) = decl.node,
75-
Some(ref initexpr) = local.init,
76-
PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
77-
Some(ref retexpr) = block.expr,
78-
ExprPath(_, ref path) = retexpr.node
72+
let Some(stmt) = block.stmts.last(),
73+
let StmtDecl(ref decl, _) = stmt.node,
74+
let DeclLocal(ref local) = decl.node,
75+
let Some(ref initexpr) = local.init,
76+
let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
77+
let Some(ref retexpr) = block.expr,
78+
let ExprPath(_, ref path) = retexpr.node,
79+
match_path(path, &[&*id.name.as_str()])
7980
], {
80-
if match_path(path, &[&*id.name.as_str()]) {
81-
self.emit_let_lint(cx, retexpr.span, initexpr.span);
82-
}
81+
self.emit_let_lint(cx, retexpr.span, initexpr.span);
8382
}
8483
}
8584
}

src/utils.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn walk_ptrs_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> {
9595

9696
/// Produce a nested chain of if-lets from the patterns:
9797
///
98-
/// if_let_chain! {[Some(y) = x, Some(z) = y],
98+
/// if_chain! {[Some(y) = x, Some(z) = y],
9999
/// {
100100
/// block
101101
/// }
@@ -109,15 +109,25 @@ pub fn walk_ptrs_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> {
109109
/// }
110110
/// }
111111
#[macro_export]
112-
macro_rules! if_let_chain {
113-
([$pat:pat = $expr:expr, $($p2:pat = $e2:expr),+], $block:block) => {
112+
macro_rules! if_chain {
113+
([let $pat:pat = $expr:expr, $($tt:tt)+], $block:block) => {
114114
if let $pat = $expr {
115-
if_let_chain!{ [$($p2 = $e2),+], $block }
115+
if_chain!{ [$($tt)+], $block }
116116
}
117117
};
118-
([$pat:pat = $expr:expr], $block:block) => {
118+
([let $pat:pat = $expr:expr], $block:block) => {
119119
if let $pat = $expr {
120120
$block
121121
}
122122
};
123+
([$expr:expr, $($tt:tt)+], $block:block) => {
124+
if $expr {
125+
if_chain!{ [$($tt)+], $block }
126+
}
127+
};
128+
([$expr:expr], $block:block) => {
129+
if $expr {
130+
$block
131+
}
132+
};
123133
}

0 commit comments

Comments
 (0)