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.
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use if_chain::if_chain;
14 use crate::rustc::hir;
15 use crate::rustc::hir::BindingAnnotation;
16 use crate::rustc::hir::def::Def;
17 use crate::syntax::ast;
18 use crate::utils::{snippet, span_lint_and_then};
19 use crate::rustc_errors::Applicability;
21 /// **What it does:** Checks for variable declarations immediately followed by a
22 /// conditional affectation.
24 /// **Why is this bad?** This is not idiomatic Rust.
26 /// **Known problems:** None.
38 /// let mut baz = None;
48 /// let foo = if bar() {
54 /// let baz = if bar() {
60 declare_clippy_lint! {
61 pub USELESS_LET_IF_SEQ,
63 "unidiomatic `let mut` declaration followed by initialization in `if`"
66 #[derive(Copy, Clone)]
69 impl LintPass for LetIfSeq {
70 fn get_lints(&self) -> LintArray {
71 lint_array!(USELESS_LET_IF_SEQ)
75 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
76 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
77 let mut it = block.stmts.iter().peekable();
78 while let Some(stmt) = it.next() {
80 if let Some(expr) = it.peek();
81 if let hir::StmtKind::Decl(ref decl, _) = stmt.node;
82 if let hir::DeclKind::Local(ref decl) = decl.node;
83 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = decl.pat.node;
84 if let hir::StmtKind::Expr(ref if_, _) = expr.node;
85 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
86 if !used_in_expr(cx, canonical_id, cond);
87 if let hir::ExprKind::Block(ref then, _) = then.node;
88 if let Some(value) = check_assign(cx, canonical_id, &*then);
89 if !used_in_expr(cx, canonical_id, value);
91 let span = stmt.span.to(if_.span);
93 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
94 if let hir::ExprKind::Block(ref else_, _) = else_.node {
95 if let Some(default) = check_assign(cx, canonical_id, else_) {
96 (else_.stmts.len() > 1, default)
97 } else if let Some(ref default) = decl.init {
105 } else if let Some(ref default) = decl.init {
111 let mutability = match mode {
112 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
116 // FIXME: this should not suggest `mut` if we can detect that the variable is not
117 // use mutably after the `if`
120 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
123 cond=snippet(cx, cond.span, "_"),
124 then=if then.stmts.len() > 1 { " ..;" } else { "" },
125 else=if default_multi_stmts { " ..;" } else { "" },
126 value=snippet(cx, value.span, "<value>"),
127 default=snippet(cx, default.span, "<default>"),
129 span_lint_and_then(cx,
132 "`if _ { .. } else { .. }` is an expression",
134 db.span_suggestion_with_applicability(
136 "it is more idiomatic to write",
138 Applicability::HasPlaceholders,
140 if !mutability.is_empty() {
141 db.note("you might not need `mut` at all");
150 struct UsedVisitor<'a, 'tcx: 'a> {
151 cx: &'a LateContext<'a, 'tcx>,
156 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
157 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
159 if let hir::ExprKind::Path(ref qpath) = expr.node;
160 if let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id);
161 if self.id == local_id;
167 hir::intravisit::walk_expr(self, expr);
169 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
170 hir::intravisit::NestedVisitorMap::None
174 fn check_assign<'a, 'tcx>(
175 cx: &LateContext<'a, 'tcx>,
177 block: &'tcx hir::Block,
178 ) -> Option<&'tcx hir::Expr> {
180 if block.expr.is_none();
181 if let Some(expr) = block.stmts.iter().last();
182 if let hir::StmtKind::Semi(ref expr, _) = expr.node;
183 if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
184 if let hir::ExprKind::Path(ref qpath) = var.node;
185 if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);
188 let mut v = UsedVisitor {
194 for s in block.stmts.iter().take(block.stmts.len()-1) {
195 hir::intravisit::walk_stmt(&mut v, s);
209 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
210 let mut v = UsedVisitor {
215 hir::intravisit::walk_expr(&mut v, expr);