]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/match_on_vec_items.rs
Rollup merge of #88860 - nbdd0121:panic, r=m-ou-se
[rust.git] / clippy_lints / src / match_on_vec_items.rs
index 167b4abd93d1baa37fb1dd5094b8be53c743e937..552c9a588977fc564d0a409a568839d50d299a56 100644 (file)
@@ -1,19 +1,22 @@
-use crate::utils::{is_type_diagnostic_item, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty};
+use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::source::snippet;
+use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
 use if_chain::if_chain;
 use rustc_errors::Applicability;
-use rustc_hir::{Expr, ExprKind, MatchSource};
+use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::lint::in_external_macro;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::sym;
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for `match vec[idx]` or `match vec[n..m]`.
+    /// ### What it does
+    /// Checks for `match vec[idx]` or `match vec[n..m]`.
     ///
-    /// **Why is this bad?** This can panic at runtime.
+    /// ### Why is this bad?
+    /// This can panic at runtime.
     ///
-    /// **Known problems:** None.
-    ///
-    /// **Example:**
+    /// ### Example
     /// ```rust, no_run
     /// let arr = vec![0, 1, 2, 3];
     /// let idx = 1;
     /// }
     /// ```
     pub MATCH_ON_VEC_ITEMS,
-    style,
+    pedantic,
     "matching on vector elements can panic"
 }
 
 declare_lint_pass!(MatchOnVecItems => [MATCH_ON_VEC_ITEMS]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchOnVecItems {
-    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) {
+impl<'tcx> LateLintPass<'tcx> for MatchOnVecItems {
+    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
         if_chain! {
             if !in_external_macro(cx.sess(), expr.span);
-            if let ExprKind::Match(ref match_expr, _, MatchSource::Normal) = expr.kind;
+            if let ExprKind::Match(match_expr, _, MatchSource::Normal) = expr.kind;
             if let Some(idx_expr) = is_vec_indexing(cx, match_expr);
             if let ExprKind::Index(vec, idx) = idx_expr.kind;
 
             then {
-                let mut applicability = Applicability::MaybeIncorrect;
+                // FIXME: could be improved to suggest surrounding every pattern with Some(_),
+                // but only when `or_patterns` are stabilized.
                 span_lint_and_sugg(
                     cx,
                     MATCH_ON_VEC_ITEMS,
                     match_expr.span,
-                    "indexing vector may panic. Consider using `get`",
+                    "indexing into a vector may panic",
                     "try this",
                     format!(
                         "{}.get({})",
-                        snippet_with_applicability(cx, vec.span, "..", &mut applicability),
-                        snippet_with_applicability(cx, idx.span, "..", &mut applicability)
+                        snippet(cx, vec.span, ".."),
+                        snippet(cx, idx.span, "..")
                     ),
-                    applicability
+                    Applicability::MaybeIncorrect
                 );
             }
         }
     }
 }
 
-fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
+fn is_vec_indexing<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
     if_chain! {
-        if let ExprKind::Index(ref array, _) = expr.kind;
-        let ty = cx.tables.expr_ty(array);
-        let ty = walk_ptrs_ty(ty);
-        if is_type_diagnostic_item(cx, ty, sym!(vec_type));
+        if let ExprKind::Index(array, index) = expr.kind;
+        if is_vector(cx, array);
+        if !is_full_range(cx, index);
 
         then {
             return Some(expr);
@@ -86,3 +89,15 @@ fn is_vec_indexing<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'tcx>)
 
     None
 }
+
+fn is_vector(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
+    let ty = cx.typeck_results().expr_ty(expr);
+    let ty = ty.peel_refs();
+    is_type_diagnostic_item(cx, ty, sym::Vec)
+}
+
+fn is_full_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
+    let ty = cx.typeck_results().expr_ty(expr);
+    let ty = ty.peel_refs();
+    is_type_lang_item(cx, ty, LangItem::RangeFull)
+}