1 use crate::utils::{higher, qpath_res, snippet, span_lint_and_then};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
5 use rustc_hir::def::Res;
6 use rustc_hir::intravisit;
7 use rustc_hir::BindingAnnotation;
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::hir::map::Map;
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 declare_clippy_lint! {
13 /// **What it does:** Checks for variable declarations immediately followed by a
14 /// conditional affectation.
16 /// **Why is this bad?** This is not idiomatic Rust.
18 /// **Known problems:** None.
30 /// let mut baz = None;
40 /// let foo = if bar() {
46 /// let baz = if bar() {
52 pub USELESS_LET_IF_SEQ,
54 "unidiomatic `let mut` declaration followed by initialization in `if`"
57 declare_lint_pass!(LetIfSeq => [USELESS_LET_IF_SEQ]);
59 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
60 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block<'_>) {
61 let mut it = block.stmts.iter().peekable();
62 while let Some(stmt) = it.next() {
64 if let Some(expr) = it.peek();
65 if let hir::StmtKind::Local(ref local) = stmt.kind;
66 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
67 if let hir::StmtKind::Expr(ref if_) = expr.kind;
68 if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_);
69 if !used_in_expr(cx, canonical_id, cond);
70 if let hir::ExprKind::Block(ref then, _) = then.kind;
71 if let Some(value) = check_assign(cx, canonical_id, &*then);
72 if !used_in_expr(cx, canonical_id, value);
74 let span = stmt.span.to(if_.span);
76 let has_interior_mutability = !cx.tables.node_type(canonical_id).is_freeze(
81 if has_interior_mutability { return; }
83 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
84 if let hir::ExprKind::Block(ref else_, _) = else_.kind {
85 if let Some(default) = check_assign(cx, canonical_id, else_) {
86 (else_.stmts.len() > 1, default)
87 } else if let Some(ref default) = local.init {
95 } else if let Some(ref default) = local.init {
101 let mutability = match mode {
102 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
106 // FIXME: this should not suggest `mut` if we can detect that the variable is not
107 // use mutably after the `if`
110 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
113 cond=snippet(cx, cond.span, "_"),
114 then=if then.stmts.len() > 1 { " ..;" } else { "" },
115 else=if default_multi_stmts { " ..;" } else { "" },
116 value=snippet(cx, value.span, "<value>"),
117 default=snippet(cx, default.span, "<default>"),
119 span_lint_and_then(cx,
122 "`if _ { .. } else { .. }` is an expression",
124 diag.span_suggestion(
126 "it is more idiomatic to write",
128 Applicability::HasPlaceholders,
130 if !mutability.is_empty() {
131 diag.note("you might not need `mut` at all");
140 struct UsedVisitor<'a, 'tcx> {
141 cx: &'a LateContext<'a, 'tcx>,
146 impl<'a, 'tcx> intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
147 type Map = Map<'tcx>;
149 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
151 if let hir::ExprKind::Path(ref qpath) = expr.kind;
152 if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id);
153 if self.id == local_id;
159 intravisit::walk_expr(self, expr);
161 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
162 intravisit::NestedVisitorMap::None
166 fn check_assign<'a, 'tcx>(
167 cx: &LateContext<'a, 'tcx>,
169 block: &'tcx hir::Block<'_>,
170 ) -> Option<&'tcx hir::Expr<'tcx>> {
172 if block.expr.is_none();
173 if let Some(expr) = block.stmts.iter().last();
174 if let hir::StmtKind::Semi(ref expr) = expr.kind;
175 if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind;
176 if let hir::ExprKind::Path(ref qpath) = var.kind;
177 if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
180 let mut v = UsedVisitor {
186 for s in block.stmts.iter().take(block.stmts.len()-1) {
187 intravisit::walk_stmt(&mut v, s);
201 fn used_in_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> bool {
202 let mut v = UsedVisitor { cx, id, used: false };
203 intravisit::walk_expr(&mut v, expr);