1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::{declare_lint, lint_array};
3 use if_chain::if_chain;
5 use rustc::hir::BindingAnnotation;
6 use rustc::hir::def::Def;
8 use crate::utils::{snippet, span_lint_and_then};
10 /// **What it does:** Checks for variable declarations immediately followed by a
11 /// conditional affectation.
13 /// **Why is this bad?** This is not idiomatic Rust.
15 /// **Known problems:** None.
27 /// let mut baz = None;
37 /// let foo = if bar() {
43 /// let baz = if bar() {
49 declare_clippy_lint! {
50 pub USELESS_LET_IF_SEQ,
52 "unidiomatic `let mut` declaration followed by initialization in `if`"
55 #[derive(Copy, Clone)]
58 impl LintPass for LetIfSeq {
59 fn get_lints(&self) -> LintArray {
60 lint_array!(USELESS_LET_IF_SEQ)
64 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
65 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
66 let mut it = block.stmts.iter().peekable();
67 while let Some(stmt) = it.next() {
69 if let Some(expr) = it.peek();
70 if let hir::StmtKind::Decl(ref decl, _) = stmt.node;
71 if let hir::DeclKind::Local(ref decl) = decl.node;
72 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = decl.pat.node;
73 if let hir::StmtKind::Expr(ref if_, _) = expr.node;
74 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
75 if !used_in_expr(cx, canonical_id, cond);
76 if let hir::ExprKind::Block(ref then, _) = then.node;
77 if let Some(value) = check_assign(cx, canonical_id, &*then);
78 if !used_in_expr(cx, canonical_id, value);
80 let span = stmt.span.to(if_.span);
82 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
83 if let hir::ExprKind::Block(ref else_, _) = else_.node {
84 if let Some(default) = check_assign(cx, canonical_id, else_) {
85 (else_.stmts.len() > 1, default)
86 } else if let Some(ref default) = decl.init {
94 } else if let Some(ref default) = decl.init {
100 let mutability = match mode {
101 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
105 // FIXME: this should not suggest `mut` if we can detect that the variable is not
106 // use mutably after the `if`
109 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
112 cond=snippet(cx, cond.span, "_"),
113 then=if then.stmts.len() > 1 { " ..;" } else { "" },
114 else=if default_multi_stmts { " ..;" } else { "" },
115 value=snippet(cx, value.span, "<value>"),
116 default=snippet(cx, default.span, "<default>"),
118 span_lint_and_then(cx,
121 "`if _ { .. } else { .. }` is an expression",
123 db.span_suggestion(span,
124 "it is more idiomatic to write",
126 if !mutability.is_empty() {
127 db.note("you might not need `mut` at all");
136 struct UsedVisitor<'a, 'tcx: 'a> {
137 cx: &'a LateContext<'a, 'tcx>,
142 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
143 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
145 if let hir::ExprKind::Path(ref qpath) = expr.node;
146 if let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id);
147 if self.id == local_id;
153 hir::intravisit::walk_expr(self, expr);
155 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
156 hir::intravisit::NestedVisitorMap::None
160 fn check_assign<'a, 'tcx>(
161 cx: &LateContext<'a, 'tcx>,
163 block: &'tcx hir::Block,
164 ) -> Option<&'tcx hir::Expr> {
166 if block.expr.is_none();
167 if let Some(expr) = block.stmts.iter().last();
168 if let hir::StmtKind::Semi(ref expr, _) = expr.node;
169 if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
170 if let hir::ExprKind::Path(ref qpath) = var.node;
171 if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);
174 let mut v = UsedVisitor {
180 for s in block.stmts.iter().take(block.stmts.len()-1) {
181 hir::intravisit::walk_stmt(&mut v, s);
195 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
196 let mut v = UsedVisitor {
201 hir::intravisit::walk_expr(&mut v, expr);