3 use rustc::hir::BindingAnnotation;
4 use rustc::hir::def::Def;
6 use 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() {
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 let Some(expr) = it.peek(),
68 let hir::StmtDecl(ref decl, _) = stmt.node,
69 let hir::DeclLocal(ref decl) = decl.node,
70 let hir::PatKind::Binding(mode, canonical_id, ref name, None) = decl.pat.node,
71 let hir::StmtExpr(ref if_, _) = expr.node,
72 let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
73 !used_in_expr(cx, canonical_id, cond),
74 let hir::ExprBlock(ref then) = then.node,
75 let Some(value) = check_assign(cx, canonical_id, &*then),
76 !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::ExprBlock(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");
133 struct UsedVisitor<'a, 'tcx: 'a> {
134 cx: &'a LateContext<'a, 'tcx>,
139 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
140 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
142 let hir::ExprPath(ref qpath) = expr.node,
143 let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id),
149 hir::intravisit::walk_expr(self, expr);
151 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
152 hir::intravisit::NestedVisitorMap::None
156 fn check_assign<'a, 'tcx>(
157 cx: &LateContext<'a, 'tcx>,
159 block: &'tcx hir::Block,
160 ) -> Option<&'tcx hir::Expr> {
162 block.expr.is_none(),
163 let Some(expr) = block.stmts.iter().last(),
164 let hir::StmtSemi(ref expr, _) = expr.node,
165 let hir::ExprAssign(ref var, ref value) = expr.node,
166 let hir::ExprPath(ref qpath) = var.node,
167 let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id),
170 let mut v = UsedVisitor {
176 for s in block.stmts.iter().take(block.stmts.len()-1) {
177 hir::intravisit::walk_stmt(&mut v, s);
190 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
191 let mut v = UsedVisitor {
196 hir::intravisit::walk_expr(&mut v, expr);