1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::utils::{snippet, span_lint_and_then};
11 use if_chain::if_chain;
13 use rustc::hir::def::Def;
14 use rustc::hir::BindingAnnotation;
15 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use rustc::{declare_tool_lint, lint_array};
17 use rustc_errors::Applicability;
20 /// **What it does:** Checks for variable declarations immediately followed by a
21 /// conditional affectation.
23 /// **Why is this bad?** This is not idiomatic Rust.
25 /// **Known problems:** None.
37 /// let mut baz = None;
47 /// let foo = if bar() {
53 /// let baz = if bar() {
59 declare_clippy_lint! {
60 pub USELESS_LET_IF_SEQ,
62 "unidiomatic `let mut` declaration followed by initialization in `if`"
65 #[derive(Copy, Clone)]
68 impl LintPass for LetIfSeq {
69 fn get_lints(&self) -> LintArray {
70 lint_array!(USELESS_LET_IF_SEQ)
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
75 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
76 let mut it = block.stmts.iter().peekable();
77 while let Some(stmt) = it.next() {
79 if let Some(expr) = it.peek();
80 if let hir::StmtKind::Decl(ref decl, _) = stmt.node;
81 if let hir::DeclKind::Local(ref decl) = decl.node;
82 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = decl.pat.node;
83 if let hir::StmtKind::Expr(ref if_, _) = expr.node;
84 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
85 if !used_in_expr(cx, canonical_id, cond);
86 if let hir::ExprKind::Block(ref then, _) = then.node;
87 if let Some(value) = check_assign(cx, canonical_id, &*then);
88 if !used_in_expr(cx, canonical_id, value);
90 let span = stmt.span.to(if_.span);
92 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
93 if let hir::ExprKind::Block(ref else_, _) = else_.node {
94 if let Some(default) = check_assign(cx, canonical_id, else_) {
95 (else_.stmts.len() > 1, default)
96 } else if let Some(ref default) = decl.init {
104 } else if let Some(ref default) = decl.init {
110 let mutability = match mode {
111 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
115 // FIXME: this should not suggest `mut` if we can detect that the variable is not
116 // use mutably after the `if`
119 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
122 cond=snippet(cx, cond.span, "_"),
123 then=if then.stmts.len() > 1 { " ..;" } else { "" },
124 else=if default_multi_stmts { " ..;" } else { "" },
125 value=snippet(cx, value.span, "<value>"),
126 default=snippet(cx, default.span, "<default>"),
128 span_lint_and_then(cx,
131 "`if _ { .. } else { .. }` is an expression",
133 db.span_suggestion_with_applicability(
135 "it is more idiomatic to write",
137 Applicability::HasPlaceholders,
139 if !mutability.is_empty() {
140 db.note("you might not need `mut` at all");
149 struct UsedVisitor<'a, 'tcx: 'a> {
150 cx: &'a LateContext<'a, 'tcx>,
155 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
156 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
158 if let hir::ExprKind::Path(ref qpath) = expr.node;
159 if let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id);
160 if self.id == local_id;
166 hir::intravisit::walk_expr(self, expr);
168 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
169 hir::intravisit::NestedVisitorMap::None
173 fn check_assign<'a, 'tcx>(
174 cx: &LateContext<'a, 'tcx>,
176 block: &'tcx hir::Block,
177 ) -> Option<&'tcx hir::Expr> {
179 if block.expr.is_none();
180 if let Some(expr) = block.stmts.iter().last();
181 if let hir::StmtKind::Semi(ref expr, _) = expr.node;
182 if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
183 if let hir::ExprKind::Path(ref qpath) = var.node;
184 if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);
187 let mut v = UsedVisitor {
193 for s in block.stmts.iter().take(block.stmts.len()-1) {
194 hir::intravisit::walk_stmt(&mut v, s);
208 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
209 let mut v = UsedVisitor { cx, id, used: false };
210 hir::intravisit::walk_expr(&mut v, expr);