1 use crate::utils::{path_to_local_id, snippet, span_lint_and_then, visitors::LocalUsedVisitor};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
5 use rustc_hir::BindingAnnotation;
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
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 pub USELESS_LET_IF_SEQ,
51 "unidiomatic `let mut` declaration followed by initialization in `if`"
54 declare_lint_pass!(LetIfSeq => [USELESS_LET_IF_SEQ]);
56 impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
57 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
58 let mut it = block.stmts.iter().peekable();
59 while let Some(stmt) = it.next() {
61 if let Some(expr) = it.peek();
62 if let hir::StmtKind::Local(ref local) = stmt.kind;
63 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
64 if let hir::StmtKind::Expr(ref if_) = expr.kind;
65 if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.kind;
66 if !LocalUsedVisitor::new(canonical_id).check_expr(cond);
67 if let hir::ExprKind::Block(ref then, _) = then.kind;
68 if let Some(value) = check_assign(canonical_id, &*then);
69 if !LocalUsedVisitor::new(canonical_id).check_expr(value);
71 let span = stmt.span.to(if_.span);
73 let has_interior_mutability = !cx.typeck_results().node_type(canonical_id).is_freeze(
77 if has_interior_mutability { return; }
79 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
80 if let hir::ExprKind::Block(ref else_, _) = else_.kind {
81 if let Some(default) = check_assign(canonical_id, else_) {
82 (else_.stmts.len() > 1, default)
83 } else if let Some(ref default) = local.init {
91 } else if let Some(ref default) = local.init {
97 let mutability = match mode {
98 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
102 // FIXME: this should not suggest `mut` if we can detect that the variable is not
103 // use mutably after the `if`
106 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
109 cond=snippet(cx, cond.span, "_"),
110 then=if then.stmts.len() > 1 { " ..;" } else { "" },
111 else=if default_multi_stmts { " ..;" } else { "" },
112 value=snippet(cx, value.span, "<value>"),
113 default=snippet(cx, default.span, "<default>"),
115 span_lint_and_then(cx,
118 "`if _ { .. } else { .. }` is an expression",
120 diag.span_suggestion(
122 "it is more idiomatic to write",
124 Applicability::HasPlaceholders,
126 if !mutability.is_empty() {
127 diag.note("you might not need `mut` at all");
136 fn check_assign<'tcx>(decl: hir::HirId, block: &'tcx hir::Block<'_>) -> Option<&'tcx hir::Expr<'tcx>> {
138 if block.expr.is_none();
139 if let Some(expr) = block.stmts.iter().last();
140 if let hir::StmtKind::Semi(ref expr) = expr.kind;
141 if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind;
142 if path_to_local_id(var, decl);
144 let mut v = LocalUsedVisitor::new(decl);
146 if block.stmts.iter().take(block.stmts.len()-1).any(|stmt| v.check_stmt(stmt)) {