|
| 1 | +use crate::utils::{is_wild, span_lint_and_help}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_hir::{Arm, Expr, ExprKind, MatchSource}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 5 | +use rustc_middle::lint::in_external_macro; |
| 6 | +use rustc_middle::ty::{self, AdtDef}; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// **What it does:** Checks for `match vec[idx]` or `match vec[n..m]`. |
| 11 | + /// |
| 12 | + /// **Why is this bad?** This can panic at runtime. |
| 13 | + /// |
| 14 | + /// **Known problems:** None. |
| 15 | + /// |
| 16 | + /// **Example:** |
| 17 | + /// ```rust, no_run |
| 18 | + /// let arr = vec![0, 1, 2, 3]; |
| 19 | + /// let idx = 1; |
| 20 | + /// |
| 21 | + /// // Bad |
| 22 | + /// match arr[idx] { |
| 23 | + /// 0 => println!("{}", 0), |
| 24 | + /// 1 => println!("{}", 3), |
| 25 | + /// _ => {}, |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust, no_run |
| 30 | + /// let arr = vec![0, 1, 2, 3]; |
| 31 | + /// let idx = 1; |
| 32 | + /// |
| 33 | + /// // Good |
| 34 | + /// match arr.get(idx) { |
| 35 | + /// Some(0) => println!("{}", 0), |
| 36 | + /// Some(1) => println!("{}", 3), |
| 37 | + /// _ => {}, |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + pub MATCH_VEC_ITEM, |
| 41 | + style, |
| 42 | + "match vector by indexing can panic" |
| 43 | +} |
| 44 | + |
| 45 | +declare_lint_pass!(MatchVecItem => [MATCH_VEC_ITEM]); |
| 46 | + |
| 47 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchVecItem { |
| 48 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) { |
| 49 | + if_chain! { |
| 50 | + if !in_external_macro(cx.sess(), expr.span); |
| 51 | + if let ExprKind::Match(ref ex, ref arms, MatchSource::Normal) = expr.kind; |
| 52 | + if contains_wild_arm(arms); |
| 53 | + if is_vec_indexing(cx, ex); |
| 54 | + |
| 55 | + then { |
| 56 | + span_lint_and_help( |
| 57 | + cx, |
| 58 | + MATCH_VEC_ITEM, |
| 59 | + expr.span, |
| 60 | + "indexing vector may panic", |
| 61 | + None, |
| 62 | + "consider using `get(..)` instead.", |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn contains_wild_arm(arms: &[Arm<'_>]) -> bool { |
| 70 | + arms.iter().any(|arm| is_wild(&arm.pat) && is_unit_expr(arm.body)) |
| 71 | +} |
| 72 | + |
| 73 | +fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> bool { |
| 74 | + if_chain! { |
| 75 | + if let ExprKind::Index(ref array, _) = expr.kind; |
| 76 | + let ty = cx.tables.expr_ty(array); |
| 77 | + if let ty::Adt(def, _) = ty.kind; |
| 78 | + if is_vec(cx, def); |
| 79 | + |
| 80 | + then { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + false |
| 86 | +} |
| 87 | + |
| 88 | +fn is_vec<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, def: &'a AdtDef) -> bool { |
| 89 | + if_chain! { |
| 90 | + let def_path = cx.tcx.def_path(def.did); |
| 91 | + if def_path.data.len() == 2; |
| 92 | + if let Some(module) = def_path.data.get(0); |
| 93 | + if module.data.as_symbol() == sym!(vec); |
| 94 | + if let Some(name) = def_path.data.get(1); |
| 95 | + if name.data.as_symbol() == sym!(Vec); |
| 96 | + |
| 97 | + then { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + false |
| 103 | +} |
| 104 | + |
| 105 | +fn is_unit_expr(expr: &Expr<'_>) -> bool { |
| 106 | + match expr.kind { |
| 107 | + ExprKind::Tup(ref v) if v.is_empty() => true, |
| 108 | + ExprKind::Block(ref b, _) if b.stmts.is_empty() && b.expr.is_none() => true, |
| 109 | + _ => false, |
| 110 | + } |
| 111 | +} |
0 commit comments