Skip to content

Commit d85b806

Browse files
committed
Format all if_let_chain consistently
1 parent 44cb610 commit d85b806

File tree

10 files changed

+175
-199
lines changed

10 files changed

+175
-199
lines changed

clippy_lints/src/entry.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl<'a, 'tcx, 'v, 'b> Visitor<'v> for InsertVisitor<'a, 'tcx, 'b> {
120120
get_item_name(self.cx, self.map) == get_item_name(self.cx, &*params[0]),
121121
SpanlessEq::new(self.cx).eq_expr(self.key, &params[1])
122122
], {
123-
124123
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
125124
&format!("usage of `contains_key` followed by `insert` on `{}`", self.ty), |db| {
126125
if self.sole_expr {

clippy_lints/src/loops.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -667,30 +667,28 @@ impl<'v, 't> Visitor<'v> for VarVisitor<'v, 't> {
667667
if let ExprPath(None, ref path) = expr.node {
668668
if path.segments.len() == 1 && path.segments[0].name == self.var {
669669
// we are referencing our variable! now check if it's as an index
670-
if_let_chain! {
671-
[
672-
let Some(parexpr) = get_parent_expr(self.cx, expr),
673-
let ExprIndex(ref seqexpr, _) = parexpr.node,
674-
let ExprPath(None, ref seqvar) = seqexpr.node,
675-
seqvar.segments.len() == 1
676-
], {
677-
let def_map = self.cx.tcx.def_map.borrow();
678-
if let Some(def) = def_map.get(&seqexpr.id) {
679-
match def.base_def {
680-
Def::Local(..) | Def::Upvar(..) => {
681-
let extent = self.cx.tcx.region_maps.var_scope(def.base_def.var_id());
682-
self.indexed.insert(seqvar.segments[0].name, Some(extent));
683-
return; // no need to walk further
684-
}
685-
Def::Static(..) | Def::Const(..) => {
686-
self.indexed.insert(seqvar.segments[0].name, None);
687-
return; // no need to walk further
688-
}
689-
_ => (),
670+
if_let_chain! {[
671+
let Some(parexpr) = get_parent_expr(self.cx, expr),
672+
let ExprIndex(ref seqexpr, _) = parexpr.node,
673+
let ExprPath(None, ref seqvar) = seqexpr.node,
674+
seqvar.segments.len() == 1
675+
], {
676+
let def_map = self.cx.tcx.def_map.borrow();
677+
if let Some(def) = def_map.get(&seqexpr.id) {
678+
match def.base_def {
679+
Def::Local(..) | Def::Upvar(..) => {
680+
let extent = self.cx.tcx.region_maps.var_scope(def.base_def.var_id());
681+
self.indexed.insert(seqvar.segments[0].name, Some(extent));
682+
return; // no need to walk further
690683
}
684+
Def::Static(..) | Def::Const(..) => {
685+
self.indexed.insert(seqvar.segments[0].name, None);
686+
return; // no need to walk further
687+
}
688+
_ => (),
691689
}
692690
}
693-
}
691+
}}
694692
// we are not indexing anything, record that
695693
self.nonindex = true;
696694
return;

clippy_lints/src/map_clone.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ impl LateLintPass for MapClonePass {
2727
if name.node.as_str() == "map" && args.len() == 2 {
2828
match args[1].node {
2929
ExprClosure(_, ref decl, ref blk, _) => {
30-
if_let_chain! {
31-
[
30+
if_let_chain! {[
3231
// just one expression in the closure
3332
blk.stmts.is_empty(),
3433
let Some(ref closure_expr) = blk.expr,
@@ -37,32 +36,31 @@ impl LateLintPass for MapClonePass {
3736
let Some(arg_ident) = get_arg_name(&*decl.inputs[0].pat),
3837
// the method is being called on a known type (option or iterator)
3938
let Some(type_name) = get_type_name(cx, expr, &args[0])
40-
], {
41-
// look for derefs, for .map(|x| *x)
42-
if only_derefs(cx, &*closure_expr, arg_ident) &&
43-
// .cloned() only removes one level of indirection, don't lint on more
44-
walk_ptrs_ty_depth(cx.tcx.pat_ty(&*decl.inputs[0].pat)).1 == 1
39+
], {
40+
// look for derefs, for .map(|x| *x)
41+
if only_derefs(cx, &*closure_expr, arg_ident) &&
42+
// .cloned() only removes one level of indirection, don't lint on more
43+
walk_ptrs_ty_depth(cx.tcx.pat_ty(&*decl.inputs[0].pat)).1 == 1
44+
{
45+
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
46+
"you seem to be using .map() to clone the contents of an {}, consider \
47+
using `.cloned()`", type_name),
48+
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
49+
}
50+
// explicit clone() calls ( .map(|x| x.clone()) )
51+
else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node {
52+
if clone_call.node.as_str() == "clone" &&
53+
clone_args.len() == 1 &&
54+
match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
55+
expr_eq_name(&clone_args[0], arg_ident)
4556
{
4657
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
4758
"you seem to be using .map() to clone the contents of an {}, consider \
4859
using `.cloned()`", type_name),
4960
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
5061
}
51-
// explicit clone() calls ( .map(|x| x.clone()) )
52-
else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node {
53-
if clone_call.node.as_str() == "clone" &&
54-
clone_args.len() == 1 &&
55-
match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
56-
expr_eq_name(&clone_args[0], arg_ident)
57-
{
58-
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
59-
"you seem to be using .map() to clone the contents of an {}, consider \
60-
using `.cloned()`", type_name),
61-
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
62-
}
63-
}
6462
}
65-
}
63+
}}
6664
}
6765
ExprPath(_, ref path) => {
6866
if match_path(path, &paths::CLONE) {

clippy_lints/src/misc.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,31 @@ impl LateLintPass for TopLevelRefPass {
5555
}
5656
}
5757
fn check_stmt(&mut self, cx: &LateContext, s: &Stmt) {
58-
if_let_chain! {
59-
[
58+
if_let_chain! {[
6059
let StmtDecl(ref d, _) = s.node,
6160
let DeclLocal(ref l) = d.node,
6261
let PatKind::Binding(BindByRef(_), i, None) = l.pat.node,
6362
let Some(ref init) = l.init
64-
], {
65-
let tyopt = if let Some(ref ty) = l.ty {
66-
format!(": {}", snippet(cx, ty.span, "_"))
67-
} else {
68-
"".to_owned()
69-
};
70-
span_lint_and_then(cx,
71-
TOPLEVEL_REF_ARG,
72-
l.pat.span,
73-
"`ref` on an entire `let` pattern is discouraged, take a reference with & instead",
74-
|db| {
75-
db.span_suggestion(s.span,
76-
"try",
77-
format!("let {}{} = &{};",
78-
snippet(cx, i.span, "_"),
79-
tyopt,
80-
snippet(cx, init.span, "_")));
81-
}
82-
);
83-
}
84-
};
63+
], {
64+
let tyopt = if let Some(ref ty) = l.ty {
65+
format!(": {}", snippet(cx, ty.span, "_"))
66+
} else {
67+
"".to_owned()
68+
};
69+
span_lint_and_then(cx,
70+
TOPLEVEL_REF_ARG,
71+
l.pat.span,
72+
"`ref` on an entire `let` pattern is discouraged, take a reference with & instead",
73+
|db| {
74+
db.span_suggestion(s.span,
75+
"try",
76+
format!("let {}{} = &{};",
77+
snippet(cx, i.span, "_"),
78+
tyopt,
79+
snippet(cx, init.span, "_")));
80+
}
81+
);
82+
}}
8583
}
8684
}
8785

clippy_lints/src/overflow_check_conditional.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ impl LateLintPass for OverflowCheckConditional {
2626
// a + b < a, a > a + b, a < a - b, a - b > a
2727
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
2828
if_let_chain! {[
29-
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
30-
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node,
31-
let Expr_::ExprPath(_,ref path1) = ident1.node,
32-
let Expr_::ExprPath(_, ref path2) = ident2.node,
33-
let Expr_::ExprPath(_, ref path3) = second.node,
34-
&path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
35-
cx.tcx.expr_ty(ident1).is_integral(),
36-
cx.tcx.expr_ty(ident2).is_integral()
29+
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
30+
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node,
31+
let Expr_::ExprPath(_,ref path1) = ident1.node,
32+
let Expr_::ExprPath(_, ref path2) = ident2.node,
33+
let Expr_::ExprPath(_, ref path3) = second.node,
34+
&path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
35+
cx.tcx.expr_ty(ident1).is_integral(),
36+
cx.tcx.expr_ty(ident2).is_integral()
3737
], {
3838
if let BinOp_::BiLt = op.node {
3939
if let BinOp_::BiAdd = op2.node {
@@ -48,14 +48,14 @@ impl LateLintPass for OverflowCheckConditional {
4848
}}
4949

5050
if_let_chain! {[
51-
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
52-
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node,
53-
let Expr_::ExprPath(_,ref path1) = ident1.node,
54-
let Expr_::ExprPath(_, ref path2) = ident2.node,
55-
let Expr_::ExprPath(_, ref path3) = first.node,
56-
&path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
57-
cx.tcx.expr_ty(ident1).is_integral(),
58-
cx.tcx.expr_ty(ident2).is_integral()
51+
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
52+
let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node,
53+
let Expr_::ExprPath(_,ref path1) = ident1.node,
54+
let Expr_::ExprPath(_, ref path2) = ident2.node,
55+
let Expr_::ExprPath(_, ref path3) = first.node,
56+
&path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
57+
cx.tcx.expr_ty(ident1).is_integral(),
58+
cx.tcx.expr_ty(ident2).is_integral()
5959
], {
6060
if let BinOp_::BiGt = op.node {
6161
if let BinOp_::BiAdd = op2.node {

clippy_lints/src/ranges.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,27 @@ impl LateLintPass for StepByZero {
4949
} else if name.as_str() == "zip" && args.len() == 2 {
5050
let iter = &args[0].node;
5151
let zip_arg = &args[1];
52-
if_let_chain! {
53-
[
54-
// .iter() call
55-
let ExprMethodCall( Spanned { node: ref iter_name, .. }, _, ref iter_args ) = *iter,
56-
iter_name.as_str() == "iter",
57-
// range expression in .zip() call: 0..x.len()
58-
let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), .. }) = unsugar_range(zip_arg),
59-
is_integer_literal(start, 0),
60-
// .len() call
61-
let ExprMethodCall(Spanned { node: ref len_name, .. }, _, ref len_args) = end.node,
62-
len_name.as_str() == "len" && len_args.len() == 1,
63-
// .iter() and .len() called on same Path
64-
let ExprPath(_, Path { segments: ref iter_path, .. }) = iter_args[0].node,
65-
let ExprPath(_, Path { segments: ref len_path, .. }) = len_args[0].node,
66-
iter_path == len_path
67-
], {
68-
span_lint(cx,
69-
RANGE_ZIP_WITH_LEN,
70-
expr.span,
71-
&format!("It is more idiomatic to use {}.iter().enumerate()",
72-
snippet(cx, iter_args[0].span, "_")));
73-
}
74-
}
52+
if_let_chain! {[
53+
// .iter() call
54+
let ExprMethodCall( Spanned { node: ref iter_name, .. }, _, ref iter_args ) = *iter,
55+
iter_name.as_str() == "iter",
56+
// range expression in .zip() call: 0..x.len()
57+
let Some(UnsugaredRange { start: Some(ref start), end: Some(ref end), .. }) = unsugar_range(zip_arg),
58+
is_integer_literal(start, 0),
59+
// .len() call
60+
let ExprMethodCall(Spanned { node: ref len_name, .. }, _, ref len_args) = end.node,
61+
len_name.as_str() == "len" && len_args.len() == 1,
62+
// .iter() and .len() called on same Path
63+
let ExprPath(_, Path { segments: ref iter_path, .. }) = iter_args[0].node,
64+
let ExprPath(_, Path { segments: ref len_path, .. }) = len_args[0].node,
65+
iter_path == len_path
66+
], {
67+
span_lint(cx,
68+
RANGE_ZIP_WITH_LEN,
69+
expr.span,
70+
&format!("It is more idiomatic to use {}.iter().enumerate()",
71+
snippet(cx, iter_args[0].span, "_")));
72+
}}
7573
}
7674
}
7775
}

clippy_lints/src/returns.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,25 @@ impl ReturnPass {
8989
// Check for "let x = EXPR; x"
9090
fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
9191
// we need both a let-binding stmt and an expr
92-
if_let_chain! {
93-
[
94-
let Some(stmt) = block.stmts.last(),
95-
let Some(ref retexpr) = block.expr,
96-
let StmtKind::Decl(ref decl, _) = stmt.node,
97-
let DeclKind::Local(ref local) = decl.node,
98-
local.ty.is_none(),
99-
let Some(ref initexpr) = local.init,
100-
let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
101-
let ExprKind::Path(_, ref path) = retexpr.node,
102-
match_path_ast(path, &[&id.name.as_str()]),
103-
!in_external_macro(cx, initexpr.span),
104-
], {
92+
if_let_chain! {[
93+
let Some(stmt) = block.stmts.last(),
94+
let Some(ref retexpr) = block.expr,
95+
let StmtKind::Decl(ref decl, _) = stmt.node,
96+
let DeclKind::Local(ref local) = decl.node,
97+
let Some(ref initexpr) = local.init,
98+
let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
99+
let ExprKind::Path(_, ref path) = retexpr.node,
100+
match_path_ast(path, &[&id.name.as_str()]),
101+
!in_external_macro(cx, initexpr.span),
102+
], {
105103
span_note_and_lint(cx,
106104
LET_AND_RETURN,
107105
retexpr.span,
108106
"returning the result of a let binding from a block. \
109107
Consider returning the expression directly.",
110108
initexpr.span,
111109
"this expression can be directly returned");
112-
}
113-
}
110+
}}
114111
}
115112
}
116113

clippy_lints/src/types.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,21 @@ impl LateLintPass for TypePass {
5757
if let Some(did) = cx.tcx.def_map.borrow().get(&ast_ty.id) {
5858
if let def::Def::Struct(..) = did.full_def() {
5959
if Some(did.def_id()) == cx.tcx.lang_items.owned_box() {
60-
if_let_chain! {
61-
[
62-
let TyPath(_, ref path) = ast_ty.node,
63-
let Some(ref last) = path.segments.last(),
64-
let PathParameters::AngleBracketedParameters(ref ag) = last.parameters,
65-
let Some(ref vec) = ag.types.get(0),
66-
let Some(did) = cx.tcx.def_map.borrow().get(&vec.id),
67-
let def::Def::Struct(..) = did.full_def(),
68-
match_def_path(cx, did.def_id(), &paths::VEC),
69-
],
70-
{
71-
span_help_and_lint(cx,
72-
BOX_VEC,
73-
ast_ty.span,
74-
"you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
75-
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation.");
76-
}
77-
}
60+
if_let_chain! {[
61+
let TyPath(_, ref path) = ast_ty.node,
62+
let Some(ref last) = path.segments.last(),
63+
let PathParameters::AngleBracketedParameters(ref ag) = last.parameters,
64+
let Some(ref vec) = ag.types.get(0),
65+
let Some(did) = cx.tcx.def_map.borrow().get(&vec.id),
66+
let def::Def::Struct(..) = did.full_def(),
67+
match_def_path(cx, did.def_id(), &paths::VEC),
68+
], {
69+
span_help_and_lint(cx,
70+
BOX_VEC,
71+
ast_ty.span,
72+
"you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
73+
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation.");
74+
}}
7875
} else if match_def_path(cx, did.def_id(), &paths::LINKED_LIST) {
7976
span_help_and_lint(cx,
8077
LINKEDLIST,

0 commit comments

Comments
 (0)