3 use rustc::hir::BindingAnnotation;
4 use rustc::hir::def::Def;
6 use crate::utils::{snippet, span_lint_and_then};
8 /// **What it does:** Checks for variable declarations immediately followed by a
9 /// conditional affectation.
11 /// **Why is this bad?** This is not idiomatic Rust.
13 /// **Known problems:** None.
25 /// let mut baz = None;
35 /// let foo = if bar() {
41 /// let baz = if bar() {
47 declare_clippy_lint! {
48 pub USELESS_LET_IF_SEQ,
50 "unidiomatic `let mut` declaration followed by initialization in `if`"
53 #[derive(Copy, Clone)]
56 impl LintPass for LetIfSeq {
57 fn get_lints(&self) -> LintArray {
58 lint_array!(USELESS_LET_IF_SEQ)
62 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
63 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
64 let mut it = block.stmts.iter().peekable();
65 while let Some(stmt) = it.next() {
67 if let Some(expr) = it.peek();
68 if let hir::StmtKind::Decl(ref decl, _) = stmt.node;
69 if let hir::DeclLocal(ref decl) = decl.node;
70 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = decl.pat.node;
71 if let hir::StmtKind::Expr(ref if_, _) = expr.node;
72 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
73 if !used_in_expr(cx, canonical_id, cond);
74 if let hir::ExprKind::Block(ref then, _) = then.node;
75 if let Some(value) = check_assign(cx, canonical_id, &*then);
76 if !used_in_expr(cx, canonical_id, value);
78 let span = stmt.span.to(if_.span);
80 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
81 if let hir::ExprKind::Block(ref else_, _) = else_.node {
82 if let Some(default) = check_assign(cx, canonical_id, else_) {
83 (else_.stmts.len() > 1, default)
84 } else if let Some(ref default) = decl.init {
92 } else if let Some(ref default) = decl.init {
98 let mutability = match mode {
99 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
103 // FIXME: this should not suggest `mut` if we can detect that the variable is not
104 // use mutably after the `if`
107 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
110 cond=snippet(cx, cond.span, "_"),
111 then=if then.stmts.len() > 1 { " ..;" } else { "" },
112 else=if default_multi_stmts { " ..;" } else { "" },
113 value=snippet(cx, value.span, "<value>"),
114 default=snippet(cx, default.span, "<default>"),
116 span_lint_and_then(cx,
119 "`if _ { .. } else { .. }` is an expression",
121 db.span_suggestion(span,
122 "it is more idiomatic to write",
124 if !mutability.is_empty() {
125 db.note("you might not need `mut` at all");
134 struct UsedVisitor<'a, 'tcx: 'a> {
135 cx: &'a LateContext<'a, 'tcx>,
140 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
141 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
143 if let hir::ExprKind::Path(ref qpath) = expr.node;
144 if let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id);
145 if self.id == local_id;
151 hir::intravisit::walk_expr(self, expr);
153 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
154 hir::intravisit::NestedVisitorMap::None
158 fn check_assign<'a, 'tcx>(
159 cx: &LateContext<'a, 'tcx>,
161 block: &'tcx hir::Block,
162 ) -> Option<&'tcx hir::Expr> {
164 if block.expr.is_none();
165 if let Some(expr) = block.stmts.iter().last();
166 if let hir::StmtKind::Semi(ref expr, _) = expr.node;
167 if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
168 if let hir::ExprKind::Path(ref qpath) = var.node;
169 if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);
172 let mut v = UsedVisitor {
178 for s in block.stmts.iter().take(block.stmts.len()-1) {
179 hir::intravisit::walk_stmt(&mut v, s);
193 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
194 let mut v = UsedVisitor {
199 hir::intravisit::walk_expr(&mut v, expr);