1 use crate::utils::{higher, qpath_res, snippet, span_lint_and_then};
2 use if_chain::if_chain;
3 use rustc::declare_lint_pass;
4 use rustc::hir::map::Map;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_errors::Applicability;
8 use rustc_hir::def::Res;
9 use rustc_hir::intravisit;
10 use rustc_hir::BindingAnnotation;
11 use rustc_session::declare_tool_lint;
13 declare_clippy_lint! {
14 /// **What it does:** Checks for variable declarations immediately followed by a
15 /// conditional affectation.
17 /// **Why is this bad?** This is not idiomatic Rust.
19 /// **Known problems:** None.
31 /// let mut baz = None;
41 /// let foo = if bar() {
47 /// let baz = if bar() {
53 pub USELESS_LET_IF_SEQ,
55 "unidiomatic `let mut` declaration followed by initialization in `if`"
58 declare_lint_pass!(LetIfSeq => [USELESS_LET_IF_SEQ]);
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
61 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block<'_>) {
62 let mut it = block.stmts.iter().peekable();
63 while let Some(stmt) = it.next() {
65 if let Some(expr) = it.peek();
66 if let hir::StmtKind::Local(ref local) = stmt.kind;
67 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
68 if let hir::StmtKind::Expr(ref if_) = expr.kind;
69 if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_);
70 if !used_in_expr(cx, canonical_id, cond);
71 if let hir::ExprKind::Block(ref then, _) = then.kind;
72 if let Some(value) = check_assign(cx, canonical_id, &*then);
73 if !used_in_expr(cx, canonical_id, value);
75 let span = stmt.span.to(if_.span);
77 let has_interior_mutability = !cx.tables.node_type(canonical_id).is_freeze(
82 if has_interior_mutability { return; }
84 let (default_multi_stmts, default) = if let Some(ref else_) = *else_ {
85 if let hir::ExprKind::Block(ref else_, _) = else_.kind {
86 if let Some(default) = check_assign(cx, canonical_id, else_) {
87 (else_.stmts.len() > 1, default)
88 } else if let Some(ref default) = local.init {
96 } else if let Some(ref default) = local.init {
102 let mutability = match mode {
103 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
107 // FIXME: this should not suggest `mut` if we can detect that the variable is not
108 // use mutably after the `if`
111 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
114 cond=snippet(cx, cond.span, "_"),
115 then=if then.stmts.len() > 1 { " ..;" } else { "" },
116 else=if default_multi_stmts { " ..;" } else { "" },
117 value=snippet(cx, value.span, "<value>"),
118 default=snippet(cx, default.span, "<default>"),
120 span_lint_and_then(cx,
123 "`if _ { .. } else { .. }` is an expression",
127 "it is more idiomatic to write",
129 Applicability::HasPlaceholders,
131 if !mutability.is_empty() {
132 db.note("you might not need `mut` at all");
141 struct UsedVisitor<'a, 'tcx> {
142 cx: &'a LateContext<'a, 'tcx>,
147 impl<'a, 'tcx> intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
148 type Map = Map<'tcx>;
150 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
152 if let hir::ExprKind::Path(ref qpath) = expr.kind;
153 if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id);
154 if self.id == local_id;
160 intravisit::walk_expr(self, expr);
162 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<'_, Self::Map> {
163 intravisit::NestedVisitorMap::None
167 fn check_assign<'a, 'tcx>(
168 cx: &LateContext<'a, 'tcx>,
170 block: &'tcx hir::Block<'_>,
171 ) -> Option<&'tcx hir::Expr<'tcx>> {
173 if block.expr.is_none();
174 if let Some(expr) = block.stmts.iter().last();
175 if let hir::StmtKind::Semi(ref expr) = expr.kind;
176 if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind;
177 if let hir::ExprKind::Path(ref qpath) = var.kind;
178 if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
181 let mut v = UsedVisitor {
187 for s in block.stmts.iter().take(block.stmts.len()-1) {
188 intravisit::walk_stmt(&mut v, s);
202 fn used_in_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> bool {
203 let mut v = UsedVisitor { cx, id, used: false };
204 intravisit::walk_expr(&mut v, expr);