mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
Format all if_let_chain
consistently
This commit is contained in:
parent
44cb6106a7
commit
d85b8062e3
10 changed files with 175 additions and 199 deletions
|
@ -120,7 +120,6 @@ impl<'a, 'tcx, 'v, 'b> Visitor<'v> for InsertVisitor<'a, 'tcx, 'b> {
|
|||
get_item_name(self.cx, self.map) == get_item_name(self.cx, &*params[0]),
|
||||
SpanlessEq::new(self.cx).eq_expr(self.key, ¶ms[1])
|
||||
], {
|
||||
|
||||
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
|
||||
&format!("usage of `contains_key` followed by `insert` on `{}`", self.ty), |db| {
|
||||
if self.sole_expr {
|
||||
|
|
|
@ -667,8 +667,7 @@ impl<'v, 't> Visitor<'v> for VarVisitor<'v, 't> {
|
|||
if let ExprPath(None, ref path) = expr.node {
|
||||
if path.segments.len() == 1 && path.segments[0].name == self.var {
|
||||
// we are referencing our variable! now check if it's as an index
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let Some(parexpr) = get_parent_expr(self.cx, expr),
|
||||
let ExprIndex(ref seqexpr, _) = parexpr.node,
|
||||
let ExprPath(None, ref seqvar) = seqexpr.node,
|
||||
|
@ -689,8 +688,7 @@ impl<'v, 't> Visitor<'v> for VarVisitor<'v, 't> {
|
|||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
// we are not indexing anything, record that
|
||||
self.nonindex = true;
|
||||
return;
|
||||
|
|
|
@ -27,8 +27,7 @@ impl LateLintPass for MapClonePass {
|
|||
if name.node.as_str() == "map" && args.len() == 2 {
|
||||
match args[1].node {
|
||||
ExprClosure(_, ref decl, ref blk, _) => {
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
// just one expression in the closure
|
||||
blk.stmts.is_empty(),
|
||||
let Some(ref closure_expr) = blk.expr,
|
||||
|
@ -61,8 +60,7 @@ impl LateLintPass for MapClonePass {
|
|||
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
ExprPath(_, ref path) => {
|
||||
if match_path(path, &paths::CLONE) {
|
||||
|
|
|
@ -55,8 +55,7 @@ impl LateLintPass for TopLevelRefPass {
|
|||
}
|
||||
}
|
||||
fn check_stmt(&mut self, cx: &LateContext, s: &Stmt) {
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let StmtDecl(ref d, _) = s.node,
|
||||
let DeclLocal(ref l) = d.node,
|
||||
let PatKind::Binding(BindByRef(_), i, None) = l.pat.node,
|
||||
|
@ -80,8 +79,7 @@ impl LateLintPass for TopLevelRefPass {
|
|||
snippet(cx, init.span, "_")));
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -49,8 +49,7 @@ impl LateLintPass for StepByZero {
|
|||
} else if name.as_str() == "zip" && args.len() == 2 {
|
||||
let iter = &args[0].node;
|
||||
let zip_arg = &args[1];
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
// .iter() call
|
||||
let ExprMethodCall( Spanned { node: ref iter_name, .. }, _, ref iter_args ) = *iter,
|
||||
iter_name.as_str() == "iter",
|
||||
|
@ -70,8 +69,7 @@ impl LateLintPass for StepByZero {
|
|||
expr.span,
|
||||
&format!("It is more idiomatic to use {}.iter().enumerate()",
|
||||
snippet(cx, iter_args[0].span, "_")));
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,13 +89,11 @@ impl ReturnPass {
|
|||
// Check for "let x = EXPR; x"
|
||||
fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
|
||||
// we need both a let-binding stmt and an expr
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let Some(stmt) = block.stmts.last(),
|
||||
let Some(ref retexpr) = block.expr,
|
||||
let StmtKind::Decl(ref decl, _) = stmt.node,
|
||||
let DeclKind::Local(ref local) = decl.node,
|
||||
local.ty.is_none(),
|
||||
let Some(ref initexpr) = local.init,
|
||||
let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
|
||||
let ExprKind::Path(_, ref path) = retexpr.node,
|
||||
|
@ -109,8 +107,7 @@ impl ReturnPass {
|
|||
Consider returning the expression directly.",
|
||||
initexpr.span,
|
||||
"this expression can be directly returned");
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -57,8 +57,7 @@ impl LateLintPass for TypePass {
|
|||
if let Some(did) = cx.tcx.def_map.borrow().get(&ast_ty.id) {
|
||||
if let def::Def::Struct(..) = did.full_def() {
|
||||
if Some(did.def_id()) == cx.tcx.lang_items.owned_box() {
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let TyPath(_, ref path) = ast_ty.node,
|
||||
let Some(ref last) = path.segments.last(),
|
||||
let PathParameters::AngleBracketedParameters(ref ag) = last.parameters,
|
||||
|
@ -66,15 +65,13 @@ impl LateLintPass for TypePass {
|
|||
let Some(did) = cx.tcx.def_map.borrow().get(&vec.id),
|
||||
let def::Def::Struct(..) = did.full_def(),
|
||||
match_def_path(cx, did.def_id(), &paths::VEC),
|
||||
],
|
||||
{
|
||||
], {
|
||||
span_help_and_lint(cx,
|
||||
BOX_VEC,
|
||||
ast_ty.span,
|
||||
"you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
|
||||
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation.");
|
||||
}
|
||||
}
|
||||
}}
|
||||
} else if match_def_path(cx, did.def_id(), &paths::LINKED_LIST) {
|
||||
span_help_and_lint(cx,
|
||||
LINKEDLIST,
|
||||
|
|
|
@ -30,16 +30,13 @@ pub type MethodArgs = HirVec<P<Expr>>;
|
|||
|
||||
/// Produce a nested chain of if-lets and ifs from the patterns:
|
||||
///
|
||||
/// if_let_chain! {
|
||||
/// [
|
||||
/// if_let_chain! {[
|
||||
/// let Some(y) = x,
|
||||
/// y.len() == 2,
|
||||
/// let Some(z) = y,
|
||||
/// ],
|
||||
/// {
|
||||
/// ], {
|
||||
/// block
|
||||
/// }
|
||||
/// }
|
||||
/// }}
|
||||
///
|
||||
/// becomes
|
||||
///
|
||||
|
@ -323,14 +320,13 @@ pub fn get_item_name(cx: &LateContext, expr: &Expr) -> Option<Name> {
|
|||
|
||||
/// Checks if a `let` decl is from a `for` loop desugaring.
|
||||
pub fn is_from_for_desugar(decl: &Decl) -> bool {
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let DeclLocal(ref loc) = decl.node,
|
||||
let Some(ref expr) = loc.init,
|
||||
let ExprMatch(_, _, MatchSource::ForLoopDesugar) = expr.node
|
||||
],
|
||||
{ return true; }
|
||||
};
|
||||
], {
|
||||
return true;
|
||||
}}
|
||||
false
|
||||
}
|
||||
|
||||
|
@ -821,8 +817,7 @@ pub fn same_tys<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, a: ty::Ty<'tcx>, b: ty::Ty
|
|||
/// Recover the essential nodes of a desugared for loop:
|
||||
/// `for pat in arg { body }` becomes `(pat, arg, body)`.
|
||||
pub fn recover_for_loop(expr: &Expr) -> Option<(&Pat, &Expr, &Expr)> {
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let ExprMatch(ref iterexpr, ref arms, _) = expr.node,
|
||||
let ExprCall(_, ref iterargs) = iterexpr.node,
|
||||
iterargs.len() == 1 && arms.len() == 1 && arms[0].guard.is_none(),
|
||||
|
@ -837,7 +832,6 @@ pub fn recover_for_loop(expr: &Expr) -> Option<(&Pat, &Expr, &Expr)> {
|
|||
return Some((&somepats[0],
|
||||
&iterargs[0],
|
||||
&innerarms[0].body));
|
||||
}
|
||||
}
|
||||
}}
|
||||
None
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@ impl LintPass for ZeroDivZeroPass {
|
|||
impl LateLintPass for ZeroDivZeroPass {
|
||||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||
// check for instances of 0.0/0.0
|
||||
if_let_chain! {
|
||||
[
|
||||
if_let_chain! {[
|
||||
let ExprBinary(ref op, ref left, ref right) = expr.node,
|
||||
let BinOp_::BiDiv = op.node,
|
||||
// TODO - constant_simple does not fold many operations involving floats.
|
||||
|
@ -41,8 +40,7 @@ impl LateLintPass for ZeroDivZeroPass {
|
|||
let Some(Constant::Float(ref rhs_value, rhs_width)) = constant_simple(right),
|
||||
let Some(0.0) = lhs_value.parse().ok(),
|
||||
let Some(0.0) = rhs_value.parse().ok()
|
||||
],
|
||||
{
|
||||
], {
|
||||
// since we're about to suggest a use of std::f32::NaN or std::f64::NaN,
|
||||
// match the precision of the literals that are given.
|
||||
let float_type = match (lhs_width, rhs_width) {
|
||||
|
@ -53,7 +51,6 @@ impl LateLintPass for ZeroDivZeroPass {
|
|||
span_help_and_lint(cx, ZERO_DIVIDED_BY_ZERO, expr.span,
|
||||
"constant division of 0.0 with 0.0 will always result in NaN",
|
||||
&format!("Consider using `std::{}::NAN` if you would like a constant representing NaN", float_type));
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue