Use a more flexible if_chain! macro
This commit is contained in:
parent
f991ebaa78
commit
b2ff33fea6
2 changed files with 25 additions and 61 deletions
maud_macros/src/lints
|
@ -20,23 +20,21 @@ impl LintPass for DoctypeHtml {
|
|||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DoctypeHtml {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
||||
if_let_chain! {[
|
||||
if_chain! {
|
||||
// It's a function call...
|
||||
let ExprCall(ref path_expr, ref args) = expr.node,
|
||||
if let ExprCall(ref path_expr, ref args) = expr.node;
|
||||
// ... where the argument is a literal "<!doctype html>"
|
||||
let Some(first_arg) = args.first(),
|
||||
let ExprLit(ref lit) = first_arg.node,
|
||||
let LitKind::Str(s, _) = lit.node,
|
||||
s.as_str().eq_ignore_ascii_case("<!doctype html>"),
|
||||
], {
|
||||
if let Some(first_arg) = args.first();
|
||||
if let ExprLit(ref lit) = first_arg.node;
|
||||
if let LitKind::Str(s, _) = lit.node;
|
||||
if s.as_str().eq_ignore_ascii_case("<!doctype html>");
|
||||
// ... and the callee is `maud::PreEscaped`
|
||||
if let ExprPath(ref qpath) = path_expr.node {
|
||||
let def_id = cx.tcx.tables().qpath_def(qpath, path_expr.id).def_id();
|
||||
if match_def_path(cx, def_id, &["maud", "PreEscaped", "{{constructor}}"]) {
|
||||
cx.struct_span_lint(MAUD_DOCTYPE_HTML, expr.span,
|
||||
"use `maud::DOCTYPE_HTML` instead").emit();
|
||||
}
|
||||
if let ExprPath(ref qpath) = path_expr.node;
|
||||
let def_id = cx.tcx.tables().qpath_def(qpath, path_expr.id).def_id();
|
||||
if match_def_path(cx, def_id, &["maud", "PreEscaped", "{{constructor}}"]) {
|
||||
cx.struct_span_lint(MAUD_DOCTYPE_HTML, expr.span,
|
||||
"use `maud::DOCTYPE_HTML` instead").emit();
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,60 +7,26 @@ use rustc::lint::LateContext;
|
|||
use rustc::ty;
|
||||
use syntax::symbol::{InternedString, Symbol};
|
||||
|
||||
/// Produce a nested chain of if-lets and ifs from the patterns:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// if_let_chain! {[
|
||||
/// let Some(y) = x,
|
||||
/// y.len() == 2,
|
||||
/// let Some(z) = y,
|
||||
/// ], {
|
||||
/// block
|
||||
/// }}
|
||||
/// ```
|
||||
///
|
||||
/// becomes
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// if let Some(y) = x {
|
||||
/// if y.len() == 2 {
|
||||
/// if let Some(z) = y {
|
||||
/// block
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! if_let_chain {
|
||||
([let $pat:pat = $expr:expr, $($tt:tt)+], $block:block) => {
|
||||
macro_rules! if_chain {
|
||||
(let $pat:pat = $expr:expr; $($tt:tt)+) => {
|
||||
{
|
||||
let $pat = $expr;
|
||||
if_chain! { $($tt)+ }
|
||||
}
|
||||
};
|
||||
(if let $pat:pat = $expr:expr; $($tt:tt)+) => {
|
||||
if let $pat = $expr {
|
||||
if_let_chain!{ [$($tt)+], $block }
|
||||
if_chain! { $($tt)+ }
|
||||
}
|
||||
};
|
||||
([let $pat:pat = $expr:expr], $block:block) => {
|
||||
if let $pat = $expr {
|
||||
$block
|
||||
}
|
||||
};
|
||||
([let $pat:pat = $expr:expr,], $block:block) => {
|
||||
if let $pat = $expr {
|
||||
$block
|
||||
}
|
||||
};
|
||||
([$expr:expr, $($tt:tt)+], $block:block) => {
|
||||
(if $expr:expr; $($tt:tt)+) => {
|
||||
if $expr {
|
||||
if_let_chain!{ [$($tt)+], $block }
|
||||
if_chain! { $($tt)+ }
|
||||
}
|
||||
};
|
||||
([$expr:expr], $block:block) => {
|
||||
if $expr {
|
||||
$block
|
||||
}
|
||||
};
|
||||
([$expr:expr,], $block:block) => {
|
||||
if $expr {
|
||||
$block
|
||||
}
|
||||
($expr:expr) => {
|
||||
$expr
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue