1 use crate::utils::{snippet, span_lint_and_then};
2 use if_chain::if_chain;
4 use rustc::hir::def::Def;
5 use rustc::hir::BindingAnnotation;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use rustc_errors::Applicability;
11 /// **What it does:** Checks for variable declarations immediately followed by a
12 /// conditional affectation.
14 /// **Why is this bad?** This is not idiomatic Rust.
16 /// **Known problems:** None.
28 /// let mut baz = None;
38 /// let foo = if bar() {
44 /// let baz = if bar() {
50 declare_clippy_lint! {
51 pub USELESS_LET_IF_SEQ,
53 "unidiomatic `let mut` declaration followed by initialization in `if`"
56 #[derive(Copy, Clone)]
59 impl LintPass for LetIfSeq {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(USELESS_LET_IF_SEQ)
64 fn name(&self) -> &'static str {
69 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
70 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
71 let mut it = block.stmts.iter().peekable();
72 while let Some(stmt) = it.next() {
74 if let Some(expr) = it.peek();
75 if let hir::StmtKind::Local(ref local) = stmt.node;
76 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.node;
77 if let hir::StmtKind::Expr(ref if_) = expr.node;
78 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node;
79 if !used_in_expr(cx, canonical_id, cond);
80 if let hir::ExprKind::Block(ref then, _) = then.node;
81 if let Some(value) = check_assign(cx, canonical_id, &*then);
82 if !used_in_expr(cx, canonical_id, value);
84 let span = stmt.span.to(if_.span);
86 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
87 if let hir::ExprKind::Block(ref else_, _) = else_.node {
88 if let Some(default) = check_assign(cx, canonical_id, else_) {
89 (else_.stmts.len() > 1, default)
90 } else if let Some(ref default) = local.init {
98 } else if let Some(ref default) = local.init {
104 let mutability = match mode {
105 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
109 // FIXME: this should not suggest `mut` if we can detect that the variable is not
110 // use mutably after the `if`
113 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
116 cond=snippet(cx, cond.span, "_"),
117 then=if then.stmts.len() > 1 { " ..;" } else { "" },
118 else=if default_multi_stmts { " ..;" } else { "" },
119 value=snippet(cx, value.span, "<value>"),
120 default=snippet(cx, default.span, "<default>"),
122 span_lint_and_then(cx,
125 "`if _ { .. } else { .. }` is an expression",
129 "it is more idiomatic to write",
131 Applicability::HasPlaceholders,
133 if !mutability.is_empty() {
134 db.note("you might not need `mut` at all");
143 struct UsedVisitor<'a, 'tcx: 'a> {
144 cx: &'a LateContext<'a, 'tcx>,
149 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
150 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
152 if let hir::ExprKind::Path(ref qpath) = expr.node;
153 if let Def::Local(local_id) = self.cx.tables.qpath_def(qpath, expr.hir_id);
154 if self.id == local_id;
160 hir::intravisit::walk_expr(self, expr);
162 fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'tcx> {
163 hir::intravisit::NestedVisitorMap::None
167 fn check_assign<'a, 'tcx>(
168 cx: &LateContext<'a, 'tcx>,
170 block: &'tcx hir::Block,
171 ) -> Option<&'tcx hir::Expr> {
173 if block.expr.is_none();
174 if let Some(expr) = block.stmts.iter().last();
175 if let hir::StmtKind::Semi(ref expr) = expr.node;
176 if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
177 if let hir::ExprKind::Path(ref qpath) = var.node;
178 if let Def::Local(local_id) = cx.tables.qpath_def(qpath, var.hir_id);
181 let mut v = UsedVisitor {
187 for s in block.stmts.iter().take(block.stmts.len()-1) {
188 hir::intravisit::walk_stmt(&mut v, s);
202 fn used_in_expr<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, id: ast::NodeId, expr: &'tcx hir::Expr) -> bool {
203 let mut v = UsedVisitor { cx, id, used: false };
204 hir::intravisit::walk_expr(&mut v, expr);