1 use crate::utils::{higher, qpath_res, snippet, span_lint_and_then};
2 use if_chain::if_chain;
4 use rustc::hir::def::Res;
5 use rustc::hir::BindingAnnotation;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_lint_pass, declare_tool_lint};
8 use rustc_errors::Applicability;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for variable declarations immediately followed by a
12 /// conditional affectation.
14 /// **Why is this bad?** This is not idiomatic Rust.
16 /// **Known problems:** None.
28 /// let mut baz = None;
38 /// let foo = if bar() {
44 /// let baz = if bar() {
50 pub USELESS_LET_IF_SEQ,
52 "unidiomatic `let mut` declaration followed by initialization in `if`"
55 declare_lint_pass!(LetIfSeq => [USELESS_LET_IF_SEQ]);
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
58 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
59 let mut it = block.stmts.iter().peekable();
60 while let Some(stmt) = it.next() {
62 if let Some(expr) = it.peek();
63 if let hir::StmtKind::Local(ref local) = stmt.kind;
64 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
65 if let hir::StmtKind::Expr(ref if_) = expr.kind;
66 if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_);
67 if !used_in_expr(cx, canonical_id, cond);
68 if let hir::ExprKind::Block(ref then, _) = then.kind;
69 if let Some(value) = check_assign(cx, canonical_id, &*then);
70 if !used_in_expr(cx, canonical_id, value);
72 let span = stmt.span.to(if_.span);
74 let has_interior_mutability = !cx.tables.node_type(canonical_id).is_freeze(
79 if has_interior_mutability { return; }
81 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
82 if let hir::ExprKind::Block(ref else_, _) = else_.kind {
83 if let Some(default) = check_assign(cx, canonical_id, else_) {
84 (else_.stmts.len() > 1, default)
85 } else if let Some(ref default) = local.init {
93 } else if let Some(ref default) = local.init {
99 let mutability = match mode {
100 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
104 // FIXME: this should not suggest `mut` if we can detect that the variable is not
105 // use mutably after the `if`
108 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
111 cond=snippet(cx, cond.span, "_"),
112 then=if then.stmts.len() > 1 { " ..;" } else { "" },
113 else=if default_multi_stmts { " ..;" } else { "" },
114 value=snippet(cx, value.span, "<value>"),
115 default=snippet(cx, default.span, "<default>"),
117 span_lint_and_then(cx,
120 "`if _ { .. } else { .. }` is an expression",
124 "it is more idiomatic to write",
126 Applicability::HasPlaceholders,
128 if !mutability.is_empty() {
129 db.note("you might not need `mut` at all");
138 struct UsedVisitor<'a, 'tcx> {
139 cx: &'a LateContext<'a, 'tcx>,
144 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
145 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
147 if let hir::ExprKind::Path(ref qpath) = expr.kind;
148 if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id);
149 if self.id == local_id;
155 hir::intravisit::walk_expr(self, expr);
157 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
158 hir::intravisit::NestedVisitorMap::None
162 fn check_assign<'a, 'tcx>(
163 cx: &LateContext<'a, 'tcx>,
165 block: &'tcx hir::Block,
166 ) -> Option<&'tcx hir::Expr> {
168 if block.expr.is_none();
169 if let Some(expr) = block.stmts.iter().last();
170 if let hir::StmtKind::Semi(ref expr) = expr.kind;
171 if let hir::ExprKind::Assign(ref var, ref value) = expr.kind;
172 if let hir::ExprKind::Path(ref qpath) = var.kind;
173 if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
176 let mut v = UsedVisitor {
182 for s in block.stmts.iter().take(block.stmts.len()-1) {
183 hir::intravisit::walk_stmt(&mut v, s);
197 fn used_in_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, id: hir::HirId, expr: &'tcx hir::Expr) -> bool {
198 let mut v = UsedVisitor { cx, id, used: false };
199 hir::intravisit::walk_expr(&mut v, expr);