|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::source::snippet_opt; |
| 3 | +use rustc_ast::ast::LitKind; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir as hir; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::declare_lint_pass; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// replace accessing array with an `idx`` by a `get(idx)`. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// it's easier to locate error where you access array when |
| 15 | + /// out of boundary; and you can write your error handle to |
| 16 | + /// solve this problem. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// let numbers = &[4, 7, 3]; |
| 21 | + /// let my_number = numbers[5]; |
| 22 | + /// ``` |
| 23 | + /// Use instead: |
| 24 | + /// ```no_run |
| 25 | + /// let numbers = &[4, 7, 3]; |
| 26 | + /// let Some(my_number) = numbers.get(5) else { |
| 27 | + /// // handle |
| 28 | + /// }; |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "1.76.0"] |
| 31 | + pub MIGHT_PANIC, |
| 32 | + style, |
| 33 | + "use `get()` function if you're not sure whether index is legal" |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint_pass!(MightPanic => [MIGHT_PANIC]); |
| 37 | + |
| 38 | +impl LateLintPass<'_> for MightPanic { |
| 39 | + fn check_stmt<'tcx>(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt<'tcx>) { |
| 40 | + // pattern matching an `let` stmt satisfy form like `let x: ty = arr[idx];` |
| 41 | + if let hir::StmtKind::Local(hir::Local { |
| 42 | + pat, |
| 43 | + ty, |
| 44 | + init, |
| 45 | + els: _, |
| 46 | + hir_id: _, |
| 47 | + span, |
| 48 | + source: _, |
| 49 | + }) = &stmt.kind |
| 50 | + && let hir::PatKind::Binding(_, _, ident, _) = pat.kind |
| 51 | + && let Some(expr) = init |
| 52 | + && let hir::ExprKind::Index(arr, idx, _) = &expr.kind |
| 53 | + && let hir::ExprKind::Path(_) = arr.kind |
| 54 | + && let hir::ExprKind::Lit(spanned) = idx.kind |
| 55 | + && let LitKind::Int(v, _) = spanned.node |
| 56 | + { |
| 57 | + // get type info string by stmt.local.ty.span |
| 58 | + // may not have an explict type, it doesn't matter |
| 59 | + let mut ty_str = String::new(); |
| 60 | + if let Some(hir::Ty { |
| 61 | + hir_id: _, |
| 62 | + kind: _, |
| 63 | + span: ty_span, |
| 64 | + }) = ty |
| 65 | + && let Some(snip) = snippet_opt(cx, *ty_span) |
| 66 | + { |
| 67 | + ty_str = snip; |
| 68 | + } |
| 69 | + let span = *span; |
| 70 | + span_lint_and_then( |
| 71 | + cx, |
| 72 | + MIGHT_PANIC, |
| 73 | + span, |
| 74 | + "use `get()` function if you're not sure whether index is legal", |
| 75 | + |diag| { |
| 76 | + if let Some(snip) = snippet_opt(cx, arr.span) { |
| 77 | + // format sugg string |
| 78 | + let sugg = if ty_str.is_empty() { |
| 79 | + format!("let Some({ident}) = {snip}.get({v}) else {{ panic!() }};") |
| 80 | + } else { |
| 81 | + format!("let Some({ident}): Option<&{ty_str}> = {snip}.get({v}) else {{ panic!() }};") |
| 82 | + }; |
| 83 | + diag.span_suggestion(span, "Try", sugg, Applicability::MachineApplicable); |
| 84 | + } |
| 85 | + }, |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments