mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
utils: implement if_let_chain macro as suggested by isHavvy
This commit is contained in:
parent
854212ce85
commit
6d5f9478b2
3 changed files with 43 additions and 15 deletions
|
@ -13,6 +13,8 @@ extern crate collections;
|
||||||
use rustc::plugin::Registry;
|
use rustc::plugin::Registry;
|
||||||
use rustc::lint::LintPassObject;
|
use rustc::lint::LintPassObject;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod utils;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod misc;
|
pub mod misc;
|
||||||
pub mod eq_op;
|
pub mod eq_op;
|
||||||
|
@ -27,7 +29,6 @@ pub mod len_zero;
|
||||||
pub mod attrs;
|
pub mod attrs;
|
||||||
pub mod collapsible_if;
|
pub mod collapsible_if;
|
||||||
pub mod unicode;
|
pub mod unicode;
|
||||||
pub mod utils;
|
|
||||||
pub mod strings;
|
pub mod strings;
|
||||||
pub mod methods;
|
pub mod methods;
|
||||||
pub mod returns;
|
pub mod returns;
|
||||||
|
|
|
@ -67,20 +67,18 @@ impl ReturnPass {
|
||||||
// Check for "let x = EXPR; x"
|
// Check for "let x = EXPR; x"
|
||||||
fn check_let_return(&mut self, cx: &Context, block: &Block) {
|
fn check_let_return(&mut self, cx: &Context, block: &Block) {
|
||||||
// we need both a let-binding stmt and an expr
|
// we need both a let-binding stmt and an expr
|
||||||
if let Some(stmt) = block.stmts.last() {
|
if_let_chain! {
|
||||||
if let StmtDecl(ref decl, _) = stmt.node {
|
[
|
||||||
if let DeclLocal(ref local) = decl.node {
|
Some(stmt) = block.stmts.last(),
|
||||||
if let Some(ref initexpr) = local.init {
|
StmtDecl(ref decl, _) = stmt.node,
|
||||||
if let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node {
|
DeclLocal(ref local) = decl.node,
|
||||||
if let Some(ref retexpr) = block.expr {
|
Some(ref initexpr) = local.init,
|
||||||
if let ExprPath(_, ref path) = retexpr.node {
|
PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
|
||||||
if match_path(path, &[&*id.name.as_str()]) {
|
Some(ref retexpr) = block.expr,
|
||||||
self.emit_let_lint(cx, retexpr.span, initexpr.span);
|
ExprPath(_, ref path) = retexpr.node
|
||||||
}
|
], {
|
||||||
}
|
if match_path(path, &[&*id.name.as_str()]) {
|
||||||
}
|
self.emit_let_lint(cx, retexpr.span, initexpr.span);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
29
src/utils.rs
29
src/utils.rs
|
@ -92,3 +92,32 @@ pub fn walk_ptrs_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> {
|
||||||
_ => ty
|
_ => ty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Produce a nested chain of if-lets from the patterns:
|
||||||
|
///
|
||||||
|
/// if_let_chain! {[Some(y) = x, Some(z) = y],
|
||||||
|
/// {
|
||||||
|
/// block
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// becomes
|
||||||
|
///
|
||||||
|
/// if let Some(y) = x {
|
||||||
|
/// if let Some(z) = y {
|
||||||
|
/// block
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! if_let_chain {
|
||||||
|
([$pat:pat = $expr:expr, $($p2:pat = $e2:expr),+], $block:block) => {
|
||||||
|
if let $pat = $expr {
|
||||||
|
if_let_chain!{ [$($p2 = $e2),+], $block }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
([$pat:pat = $expr:expr], $block:block) => {
|
||||||
|
if let $pat = $expr {
|
||||||
|
$block
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue