]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/match_on_vec_items.rs
Merge commit '4911ab124c481430672a3833b37075e6435ec34d' into clippyup
[rust.git] / clippy_lints / src / match_on_vec_items.rs
1 use crate::utils::{is_type_diagnostic_item, is_type_lang_item, snippet, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
5 use rustc_lint::{LateContext, LateLintPass, LintContext};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::sym;
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for `match vec[idx]` or `match vec[n..m]`.
12     ///
13     /// **Why is this bad?** This can panic at runtime.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     /// ```rust, no_run
19     /// let arr = vec![0, 1, 2, 3];
20     /// let idx = 1;
21     ///
22     /// // Bad
23     /// match arr[idx] {
24     ///     0 => println!("{}", 0),
25     ///     1 => println!("{}", 3),
26     ///     _ => {},
27     /// }
28     /// ```
29     /// Use instead:
30     /// ```rust, no_run
31     /// let arr = vec![0, 1, 2, 3];
32     /// let idx = 1;
33     ///
34     /// // Good
35     /// match arr.get(idx) {
36     ///     Some(0) => println!("{}", 0),
37     ///     Some(1) => println!("{}", 3),
38     ///     _ => {},
39     /// }
40     /// ```
41     pub MATCH_ON_VEC_ITEMS,
42     pedantic,
43     "matching on vector elements can panic"
44 }
45
46 declare_lint_pass!(MatchOnVecItems => [MATCH_ON_VEC_ITEMS]);
47
48 impl<'tcx> LateLintPass<'tcx> for MatchOnVecItems {
49     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
50         if_chain! {
51             if !in_external_macro(cx.sess(), expr.span);
52             if let ExprKind::Match(ref match_expr, _, MatchSource::Normal) = expr.kind;
53             if let Some(idx_expr) = is_vec_indexing(cx, match_expr);
54             if let ExprKind::Index(vec, idx) = idx_expr.kind;
55
56             then {
57                 // FIXME: could be improved to suggest surrounding every pattern with Some(_),
58                 // but only when `or_patterns` are stabilized.
59                 span_lint_and_sugg(
60                     cx,
61                     MATCH_ON_VEC_ITEMS,
62                     match_expr.span,
63                     "indexing into a vector may panic",
64                     "try this",
65                     format!(
66                         "{}.get({})",
67                         snippet(cx, vec.span, ".."),
68                         snippet(cx, idx.span, "..")
69                     ),
70                     Applicability::MaybeIncorrect
71                 );
72             }
73         }
74     }
75 }
76
77 fn is_vec_indexing<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
78     if_chain! {
79         if let ExprKind::Index(ref array, ref index) = expr.kind;
80         if is_vector(cx, array);
81         if !is_full_range(cx, index);
82
83         then {
84             return Some(expr);
85         }
86     }
87
88     None
89 }
90
91 fn is_vector(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
92     let ty = cx.typeck_results().expr_ty(expr);
93     let ty = ty.peel_refs();
94     is_type_diagnostic_item(cx, ty, sym::vec_type)
95 }
96
97 fn is_full_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
98     let ty = cx.typeck_results().expr_ty(expr);
99     let ty = ty.peel_refs();
100     is_type_lang_item(cx, ty, LangItem::RangeFull)
101 }