|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, source::snippet_with_applicability}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{ |
| 4 | + intravisit::{walk_expr, Visitor}, |
| 5 | + Expr, ExprKind, Node, |
| 6 | +}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 8 | +use rustc_middle::lint::in_external_macro; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for empty `if` branches with no else branch. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// It can be entirely omitted, and often the condition too. |
| 17 | + /// |
| 18 | + /// ### Known issues |
| 19 | + /// This will usually only suggest to remove the `if` statement, not the condition. Other lints |
| 20 | + /// such as `no_effect` will take care of removing the condition if it's unnecessary. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust,ignore |
| 24 | + /// if really_expensive_condition(&i) {} |
| 25 | + /// if really_expensive_condition_with_side_effects(&mut i) {} |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```rust,ignore |
| 29 | + /// // <omitted> |
| 30 | + /// really_expensive_condition_with_side_effects(&mut i); |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.72.0"] |
| 33 | + pub NEEDLESS_IF, |
| 34 | + complexity, |
| 35 | + "checks for empty if branches" |
| 36 | +} |
| 37 | +declare_lint_pass!(NeedlessIf => [NEEDLESS_IF]); |
| 38 | + |
| 39 | +impl LateLintPass<'_> for NeedlessIf { |
| 40 | + fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 41 | + if let ExprKind::If(if_expr, block, else_expr) = &expr.kind |
| 42 | + && let ExprKind::Block(block, ..) = block.kind |
| 43 | + && block.stmts.is_empty() |
| 44 | + && block.expr.is_none() |
| 45 | + && else_expr.is_none() |
| 46 | + && !in_external_macro(cx.sess(), expr.span) |
| 47 | + { |
| 48 | + // Ignore `else if` |
| 49 | + if let Some(parent_id) = cx.tcx.hir().opt_parent_id(expr.hir_id) |
| 50 | + && let Some(Node::Expr(Expr { |
| 51 | + kind: ExprKind::If(_, _, Some(else_expr)), |
| 52 | + .. |
| 53 | + })) = cx.tcx.hir().find(parent_id) |
| 54 | + && else_expr.hir_id == expr.hir_id |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if is_any_if_let(if_expr) || is_from_proc_macro(cx, expr) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + let mut app = Applicability::MachineApplicable; |
| 64 | + let snippet = snippet_with_applicability(cx, if_expr.span, "{ ... }", &mut app); |
| 65 | + |
| 66 | + span_lint_and_sugg( |
| 67 | + cx, |
| 68 | + NEEDLESS_IF, |
| 69 | + expr.span, |
| 70 | + "this `if` branch is empty", |
| 71 | + "you can remove it", |
| 72 | + if if_expr.can_have_side_effects() { |
| 73 | + format!("{snippet};") |
| 74 | + } else { |
| 75 | + String::new() |
| 76 | + }, |
| 77 | + app, |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Returns true if any `Expr` contained within this `Expr` is a `Let`, else false. |
| 84 | +/// |
| 85 | +/// Really wish `Expr` had a `walk` method... |
| 86 | +fn is_any_if_let(expr: &Expr<'_>) -> bool { |
| 87 | + let mut v = IsAnyLetVisitor(false); |
| 88 | + |
| 89 | + v.visit_expr(expr); |
| 90 | + v.0 |
| 91 | +} |
| 92 | + |
| 93 | +struct IsAnyLetVisitor(bool); |
| 94 | + |
| 95 | +impl Visitor<'_> for IsAnyLetVisitor { |
| 96 | + fn visit_expr(&mut self, expr: &Expr<'_>) { |
| 97 | + if matches!(expr.kind, ExprKind::Let(..)) { |
| 98 | + self.0 = true; |
| 99 | + } else { |
| 100 | + walk_expr(self, expr); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments