1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::{path_to_local_id, visitors::is_local_used};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
7 use rustc_hir::BindingAnnotation;
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
13 /// Checks for variable declarations immediately followed by a
14 /// conditional affectation.
16 /// ### Why is this bad?
17 /// This is not idiomatic Rust.
29 /// let mut baz = None;
39 /// let foo = if bar() {
45 /// let baz = if bar() {
51 #[clippy::version = "pre 1.29.0"]
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<'tcx> LateLintPass<'tcx> for LetIfSeq {
60 fn check_block(&mut self, cx: &LateContext<'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(local) = stmt.kind;
66 if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind;
67 if let hir::StmtKind::Expr(if_) = expr.kind;
68 if let hir::ExprKind::If(hir::Expr { kind: hir::ExprKind::DropTemps(cond), ..}, then, else_) = if_.kind;
69 if !is_local_used(cx, *cond, canonical_id);
70 if let hir::ExprKind::Block(then, _) = then.kind;
71 if let Some(value) = check_assign(cx, canonical_id, &*then);
72 if !is_local_used(cx, value, canonical_id);
74 let span = stmt.span.to(if_.span);
76 let has_interior_mutability = !cx.typeck_results().node_type(canonical_id).is_freeze(
80 if has_interior_mutability { return; }
82 let (default_multi_stmts, default) = if let Some(else_) = else_ {
83 if let hir::ExprKind::Block(else_, _) = else_.kind {
84 if let Some(default) = check_assign(cx, canonical_id, else_) {
85 (else_.stmts.len() > 1, default)
86 } else if let Some(default) = local.init {
94 } else if let Some(default) = local.init {
100 let mutability = match mode {
101 BindingAnnotation::RefMut | BindingAnnotation::Mutable => "<mut> ",
105 // FIXME: this should not suggest `mut` if we can detect that the variable is not
106 // use mutably after the `if`
109 "let {mut}{name} = if {cond} {{{then} {value} }} else {{{else} {default} }};",
112 cond=snippet(cx, cond.span, "_"),
113 then=if then.stmts.len() > 1 { " ..;" } else { "" },
114 else=if default_multi_stmts { " ..;" } else { "" },
115 value=snippet(cx, value.span, "<value>"),
116 default=snippet(cx, default.span, "<default>"),
118 span_lint_and_then(cx,
121 "`if _ { .. } else { .. }` is an expression",
123 diag.span_suggestion(
125 "it is more idiomatic to write",
127 Applicability::HasPlaceholders,
129 if !mutability.is_empty() {
130 diag.note("you might not need `mut` at all");
139 fn check_assign<'tcx>(
140 cx: &LateContext<'tcx>,
142 block: &'tcx hir::Block<'_>,
143 ) -> Option<&'tcx hir::Expr<'tcx>> {
145 if block.expr.is_none();
146 if let Some(expr) = block.stmts.iter().last();
147 if let hir::StmtKind::Semi(expr) = expr.kind;
148 if let hir::ExprKind::Assign(var, value, _) = expr.kind;
149 if path_to_local_id(var, decl);
151 if block.stmts.iter().take(block.stmts.len()-1).any(|stmt| is_local_used(cx, stmt, decl)) {