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