3 use syntax_pos::{Span, NO_EXPANSION};
4 use utils::{snippet, span_lint_and_then};
6 /// **What it does:** Checks for variable declarations immediately followed by a
7 /// conditional affectation.
9 /// **Why is this bad?** This is not idiomatic Rust.
11 /// **Known problems:** None.
23 /// let mut baz = None;
33 /// let foo = if bar() {
39 /// let baz = if bar() {
46 pub USELESS_LET_IF_SEQ,
48 "unidiomatic `let mut` declaration followed by initialization in `if`"
54 impl LintPass for LetIfSeq {
55 fn get_lints(&self) -> LintArray {
56 lint_array!(USELESS_LET_IF_SEQ)
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
61 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
62 let mut it = block.stmts.iter().peekable();
63 while let Some(stmt) = it.next() {
65 let Some(expr) = it.peek(),
66 let hir::StmtDecl(ref decl, _) = stmt.node,
67 let hir::DeclLocal(ref decl) = decl.node,
68 let hir::PatKind::Binding(mode, def_id, ref name, None) = decl.pat.node,
69 let hir::StmtExpr(ref if_, _) = expr.node,
70 let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
71 !used_in_expr(cx, def_id, cond),
72 let hir::ExprBlock(ref then) = then.node,
73 let Some(value) = check_assign(cx, def_id, &*then),
74 !used_in_expr(cx, def_id, value),
76 let span = Span { lo: stmt.span.lo, hi: if_.span.hi, ctxt: NO_EXPANSION };
78 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
79 if let hir::ExprBlock(ref else_) = else_.node {
80 if let Some(default) = check_assign(cx, def_id, else_) {
81 (else_.stmts.len() > 1, default)
82 } else if let Some(ref default) = decl.init {
90 } else if let Some(ref default) = decl.init {
96 let mutability = match mode {
97 hir::BindByRef(hir::MutMutable) | hir::BindByValue(hir::MutMutable) => "<mut> ",
101 // FIXME: this should not suggest `mut` if we can detect that the variable is not
102 // use mutably after the `if`
105 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
108 cond=snippet(cx, cond.span, "_"),
109 then=if then.stmts.len() > 1 { " ..;" } else { "" },
110 else=if default_multi_stmts { " ..;" } else { "" },
111 value=snippet(cx, value.span, "<value>"),
112 default=snippet(cx, default.span, "<default>"),
114 span_lint_and_then(cx,
117 "`if _ { .. } else { .. }` is an expression",
119 db.span_suggestion(span,
120 "it is more idiomatic to write",
122 if !mutability.is_empty() {
123 db.note("you might not need `mut` at all");
131 struct UsedVisitor<'a, 'tcx: 'a> {
132 cx: &'a LateContext<'a, 'tcx>,
133 id: hir::def_id::DefId,
137 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
138 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
140 let hir::ExprPath(ref qpath) = expr.node,
141 self.id == self.cx.tables.qpath_def(qpath, expr.id).def_id(),
146 hir::intravisit::walk_expr(self, expr);
148 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
149 hir::intravisit::NestedVisitorMap::All(&self.cx.tcx.hir)
153 fn check_assign<'a, 'tcx>(
154 cx: &LateContext<'a, 'tcx>,
155 decl: hir::def_id::DefId,
156 block: &'tcx hir::Block
157 ) -> Option<&'tcx hir::Expr> {
159 block.expr.is_none(),
160 let Some(expr) = block.stmts.iter().last(),
161 let hir::StmtSemi(ref expr, _) = expr.node,
162 let hir::ExprAssign(ref var, ref value) = expr.node,
163 let hir::ExprPath(ref qpath) = var.node,
164 decl == cx.tables.qpath_def(qpath, var.id).def_id(),
166 let mut v = UsedVisitor {
172 for s in block.stmts.iter().take(block.stmts.len()-1) {
173 hir::intravisit::walk_stmt(&mut v, s);
186 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: hir::def_id::DefId, expr: &'tcx hir::Expr) -> bool {
187 let mut v = UsedVisitor {
192 hir::intravisit::walk_expr(&mut v, expr);