|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::visitors::for_each_expr; |
| 3 | +use clippy_utils::{get_parent_expr, peel_blocks}; |
| 4 | +use core::ops::ControlFlow; |
| 5 | +use rustc_ast::ast::LitKind; |
| 6 | +use rustc_ast::BinOpKind; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Detects `if`-then-`else` that can be replaced with `&&`. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// `&&` is simpler than `if`-then-`else`. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```no_run |
| 21 | + /// if a { |
| 22 | + /// b |
| 23 | + /// } else { |
| 24 | + /// false |
| 25 | + /// } |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```no_run |
| 29 | + /// a && b |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.80.0"] |
| 32 | + pub MANUAL_AND, |
| 33 | + complexity, |
| 34 | + "this `if`-then-`else` that can be replaced with `&&`." |
| 35 | +} |
| 36 | + |
| 37 | +declare_lint_pass!(ManualAnd => [MANUAL_AND]); |
| 38 | + |
| 39 | +fn extract_final_expression_snippet<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<String> { |
| 40 | + if let ExprKind::Block(block, _) = expr.kind { |
| 41 | + if let Some(final_expr) = block.expr { |
| 42 | + return cx.sess().source_map().span_to_snippet(final_expr.span).ok(); |
| 43 | + } |
| 44 | + } |
| 45 | + cx.sess().source_map().span_to_snippet(expr.span).ok() |
| 46 | +} |
| 47 | + |
| 48 | +fn fetch_bool_expr(expr: &Expr<'_>) -> Option<bool> { |
| 49 | + if let ExprKind::Lit(lit_ptr) = peel_blocks(expr).kind { |
| 50 | + if let LitKind::Bool(value) = lit_ptr.node { |
| 51 | + return Some(value); |
| 52 | + } |
| 53 | + } |
| 54 | + None |
| 55 | +} |
| 56 | + |
| 57 | +fn contains_let(cond: &Expr<'_>) -> bool { |
| 58 | + for_each_expr(cond, |e| { |
| 59 | + if matches!(e.kind, ExprKind::Let(_)) { |
| 60 | + ControlFlow::Break(()) |
| 61 | + } else { |
| 62 | + ControlFlow::Continue(()) |
| 63 | + } |
| 64 | + }) |
| 65 | + .is_some() |
| 66 | +} |
| 67 | + |
| 68 | +fn contains_or(cond: &Expr<'_>) -> bool { |
| 69 | + for_each_expr(cond, |e| { |
| 70 | + if let ExprKind::Binary(ref n, _, _) = e.kind { |
| 71 | + if n.node == BinOpKind::Or { |
| 72 | + ControlFlow::Break(()) |
| 73 | + } else { |
| 74 | + ControlFlow::Continue(()) |
| 75 | + } |
| 76 | + } else { |
| 77 | + ControlFlow::Continue(()) |
| 78 | + } |
| 79 | + }) |
| 80 | + .is_some() |
| 81 | +} |
| 82 | + |
| 83 | +impl<'tcx> LateLintPass<'tcx> for ManualAnd { |
| 84 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 85 | + if let Some(parent) = get_parent_expr(cx, expr) { |
| 86 | + if let ExprKind::If(_, _, _) = parent.kind { |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + if let ExprKind::If(cond, then, Some(else_expr)) = expr.kind { |
| 91 | + if contains_let(cond) || contains_or(cond) || contains_or(then) { |
| 92 | + return; |
| 93 | + } |
| 94 | + if fetch_bool_expr(then).is_some() |
| 95 | + || match then.kind { |
| 96 | + ExprKind::Block(block, _) => !block.stmts.is_empty(), |
| 97 | + _ => false, |
| 98 | + } |
| 99 | + { |
| 100 | + return; |
| 101 | + } |
| 102 | + if let Some(false) = fetch_bool_expr(else_expr) { |
| 103 | + let applicability = Applicability::MachineApplicable; |
| 104 | + let cond_snippet = cx |
| 105 | + .sess() |
| 106 | + .source_map() |
| 107 | + .span_to_snippet(cond.span) |
| 108 | + .unwrap_or_else(|_| "..".to_string()); |
| 109 | + |
| 110 | + let then_snippet = extract_final_expression_snippet(cx, then).unwrap_or_else(|| "..".to_string()); |
| 111 | + span_lint_and_then( |
| 112 | + cx, |
| 113 | + MANUAL_AND, |
| 114 | + expr.span, |
| 115 | + "this `if`-then-`else` expression can be simplified with `&&`", |
| 116 | + |diag| { |
| 117 | + diag.span_suggestion( |
| 118 | + expr.span, |
| 119 | + "try", |
| 120 | + format!("{cond_snippet} && {then_snippet}"), |
| 121 | + applicability, |
| 122 | + ); |
| 123 | + }, |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments