3 use rustc::hir::BindingAnnotation;
4 use syntax_pos::{Span, NO_EXPANSION};
5 use utils::{snippet, span_lint_and_then};
7 /// **What it does:** Checks for variable declarations immediately followed by a
8 /// conditional affectation.
10 /// **Why is this bad?** This is not idiomatic Rust.
12 /// **Known problems:** None.
24 /// let mut baz = None;
34 /// let foo = if bar() {
40 /// let baz = if bar() {
47 pub USELESS_LET_IF_SEQ,
49 "unidiomatic `let mut` declaration followed by initialization in `if`"
55 impl LintPass for LetIfSeq {
56 fn get_lints(&self) -> LintArray {
57 lint_array!(USELESS_LET_IF_SEQ)
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
62 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
63 let mut it = block.stmts.iter().peekable();
64 while let Some(stmt) = it.next() {
66 let Some(expr) = it.peek(),
67 let hir::StmtDecl(ref decl, _) = stmt.node,
68 let hir::DeclLocal(ref decl) = decl.node,
69 let hir::PatKind::Binding(mode, def_id, ref name, None) = decl.pat.node,
70 let hir::StmtExpr(ref if_, _) = expr.node,
71 let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
72 !used_in_expr(cx, def_id, cond),
73 let hir::ExprBlock(ref then) = then.node,
74 let Some(value) = check_assign(cx, def_id, &*then),
75 !used_in_expr(cx, def_id, value),
77 let span = Span { lo: stmt.span.lo, hi: if_.span.hi, ctxt: NO_EXPANSION };
79 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
80 if let hir::ExprBlock(ref else_) = else_.node {
81 if let Some(default) = check_assign(cx, def_id, else_) {
82 (else_.stmts.len() > 1, default)
83 } else if let Some(ref default) = decl.init {
91 } else if let Some(ref default) = decl.init {
97 let mutability = match mode {
98 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
102 // FIXME: this should not suggest `mut` if we can detect that the variable is not
103 // use mutably after the `if`
106 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
109 cond=snippet(cx, cond.span, "_"),
110 then=if then.stmts.len() > 1 { " ..;" } else { "" },
111 else=if default_multi_stmts { " ..;" } else { "" },
112 value=snippet(cx, value.span, "<value>"),
113 default=snippet(cx, default.span, "<default>"),
115 span_lint_and_then(cx,
118 "`if _ { .. } else { .. }` is an expression",
120 db.span_suggestion(span,
121 "it is more idiomatic to write",
123 if !mutability.is_empty() {
124 db.note("you might not need `mut` at all");
132 struct UsedVisitor<'a, 'tcx: 'a> {
133 cx: &'a LateContext<'a, 'tcx>,
134 id: hir::def_id::DefId,
138 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
139 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
141 let hir::ExprPath(ref qpath) = expr.node,
142 self.id == self.cx.tables.qpath_def(qpath, expr.id).def_id(),
147 hir::intravisit::walk_expr(self, expr);
149 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
150 hir::intravisit::NestedVisitorMap::None
154 fn check_assign<'a, 'tcx>(
155 cx: &LateContext<'a, 'tcx>,
156 decl: hir::def_id::DefId,
157 block: &'tcx hir::Block
158 ) -> Option<&'tcx hir::Expr> {
160 block.expr.is_none(),
161 let Some(expr) = block.stmts.iter().last(),
162 let hir::StmtSemi(ref expr, _) = expr.node,
163 let hir::ExprAssign(ref var, ref value) = expr.node,
164 let hir::ExprPath(ref qpath) = var.node,
165 decl == cx.tables.qpath_def(qpath, var.id).def_id(),
167 let mut v = UsedVisitor {
173 for s in block.stmts.iter().take(block.stmts.len()-1) {
174 hir::intravisit::walk_stmt(&mut v, s);
187 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: hir::def_id::DefId, expr: &'tcx hir::Expr) -> bool {
188 let mut v = UsedVisitor {
193 hir::intravisit::walk_expr(&mut v, expr);