|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::higher::PanicExpn; |
| 3 | +use clippy_utils::is_expn_of; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{Block, Expr, ExprKind, StmtKind, UnOp}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Detects `if`-then-`panic!` that can be replaced with `assert!`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// `assert!` is simpler than `if`-then-`panic!`. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// let sad_people: Vec<&str> = vec![]; |
| 20 | + /// if !sad_people.is_empty() { |
| 21 | + /// panic!("there are sad people: {:?}", sad_people); |
| 22 | + /// } |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```rust |
| 26 | + /// let sad_people: Vec<&str> = vec![]; |
| 27 | + /// assert!(sad_people.is_empty(), "there are sad people: {:?}", sad_people); |
| 28 | + /// ``` |
| 29 | + pub IF_THEN_PANIC, |
| 30 | + style, |
| 31 | + "`panic!` and only a `panic!` in `if`-then statement" |
| 32 | +} |
| 33 | + |
| 34 | +declare_lint_pass!(IfThenPanic => [IF_THEN_PANIC]); |
| 35 | + |
| 36 | +impl LateLintPass<'_> for IfThenPanic { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 38 | + if_chain! { |
| 39 | + if let Expr { |
| 40 | + kind: ExprKind:: If(cond, Expr { |
| 41 | + kind: ExprKind::Block( |
| 42 | + Block { |
| 43 | + stmts: [stmt], |
| 44 | + .. |
| 45 | + }, |
| 46 | + _), |
| 47 | + .. |
| 48 | + }, None), |
| 49 | + .. |
| 50 | + } = &expr; |
| 51 | + if is_expn_of(stmt.span, "panic").is_some(); |
| 52 | + if !matches!(cond.kind, ExprKind::Let(_, _, _)); |
| 53 | + if let StmtKind::Semi(semi) = stmt.kind; |
| 54 | + if !cx.tcx.sess.source_map().is_multiline(cond.span); |
| 55 | + |
| 56 | + then { |
| 57 | + let span = if let Some(panic_expn) = PanicExpn::parse(semi) { |
| 58 | + match *panic_expn.format_args.value_args { |
| 59 | + [] => panic_expn.format_args.format_string_span, |
| 60 | + [.., last] => panic_expn.format_args.format_string_span.to(last.span), |
| 61 | + } |
| 62 | + } else { |
| 63 | + if_chain! { |
| 64 | + if let ExprKind::Block(block, _) = semi.kind; |
| 65 | + if let Some(init) = block.expr; |
| 66 | + if let ExprKind::Call(_, [format_args]) = init.kind; |
| 67 | + |
| 68 | + then { |
| 69 | + format_args.span |
| 70 | + } else { |
| 71 | + return |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + let mut applicability = Applicability::MachineApplicable; |
| 76 | + let sugg = snippet_with_applicability(cx, span, "..", &mut applicability); |
| 77 | + |
| 78 | + let cond_sugg = |
| 79 | + if let ExprKind::DropTemps(Expr{kind: ExprKind::Unary(UnOp::Not, not_expr), ..}) = cond.kind { |
| 80 | + snippet_with_applicability(cx, not_expr.span, "..", &mut applicability).to_string() |
| 81 | + } else { |
| 82 | + format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability)) |
| 83 | + }; |
| 84 | + |
| 85 | + span_lint_and_help( |
| 86 | + cx, |
| 87 | + IF_THEN_PANIC, |
| 88 | + expr.span, |
| 89 | + "only a `panic!` in `if`-then statement", |
| 90 | + None, |
| 91 | + &format!("considering use `assert!({}, {})` instead", cond_sugg, sugg) |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments