2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
|
2020-04-23 21:28:18 +00:00
|
|
|
use if_chain::if_chain;
|
2020-04-24 21:52:02 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-08-04 13:24:13 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
|
2020-04-23 21:28:18 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-02 16:32:55 +00:00
|
|
|
use rustc_span::sym;
|
2020-04-23 21:28:18 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for `match vec[idx]` or `match vec[n..m]`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** This can panic at runtime.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust, no_run
|
|
|
|
/// let arr = vec![0, 1, 2, 3];
|
|
|
|
/// let idx = 1;
|
|
|
|
///
|
|
|
|
/// // Bad
|
|
|
|
/// match arr[idx] {
|
|
|
|
/// 0 => println!("{}", 0),
|
|
|
|
/// 1 => println!("{}", 3),
|
|
|
|
/// _ => {},
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust, no_run
|
|
|
|
/// let arr = vec![0, 1, 2, 3];
|
|
|
|
/// let idx = 1;
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// match arr.get(idx) {
|
|
|
|
/// Some(0) => println!("{}", 0),
|
|
|
|
/// Some(1) => println!("{}", 3),
|
|
|
|
/// _ => {},
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-04-25 08:33:40 +00:00
|
|
|
pub MATCH_ON_VEC_ITEMS,
|
2020-05-11 18:23:47 +00:00
|
|
|
pedantic,
|
2020-04-25 08:33:40 +00:00
|
|
|
"matching on vector elements can panic"
|
2020-04-23 21:28:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 08:33:40 +00:00
|
|
|
declare_lint_pass!(MatchOnVecItems => [MATCH_ON_VEC_ITEMS]);
|
2020-04-23 21:28:18 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MatchOnVecItems {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
2020-04-23 21:28:18 +00:00
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), expr.span);
|
2021-04-02 21:35:32 +00:00
|
|
|
if let ExprKind::Match(match_expr, _, MatchSource::Normal) = expr.kind;
|
2020-04-24 21:52:02 +00:00
|
|
|
if let Some(idx_expr) = is_vec_indexing(cx, match_expr);
|
|
|
|
if let ExprKind::Index(vec, idx) = idx_expr.kind;
|
2020-04-23 21:28:18 +00:00
|
|
|
|
|
|
|
then {
|
2020-04-26 14:57:19 +00:00
|
|
|
// FIXME: could be improved to suggest surrounding every pattern with Some(_),
|
|
|
|
// but only when `or_patterns` are stabilized.
|
2020-04-24 21:52:02 +00:00
|
|
|
span_lint_and_sugg(
|
2020-04-23 21:28:18 +00:00
|
|
|
cx,
|
2020-04-25 08:33:40 +00:00
|
|
|
MATCH_ON_VEC_ITEMS,
|
2020-04-24 21:52:02 +00:00
|
|
|
match_expr.span,
|
2020-04-26 14:57:19 +00:00
|
|
|
"indexing into a vector may panic",
|
2020-04-24 21:52:02 +00:00
|
|
|
"try this",
|
|
|
|
format!(
|
|
|
|
"{}.get({})",
|
2020-04-26 14:57:19 +00:00
|
|
|
snippet(cx, vec.span, ".."),
|
|
|
|
snippet(cx, idx.span, "..")
|
2020-04-24 21:52:02 +00:00
|
|
|
),
|
2020-04-26 14:57:19 +00:00
|
|
|
Applicability::MaybeIncorrect
|
2020-04-23 21:28:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_vec_indexing<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
2020-04-23 21:28:18 +00:00
|
|
|
if_chain! {
|
2021-04-02 21:35:32 +00:00
|
|
|
if let ExprKind::Index(array, index) = expr.kind;
|
2020-05-11 18:23:47 +00:00
|
|
|
if is_vector(cx, array);
|
|
|
|
if !is_full_range(cx, index);
|
2020-04-23 21:28:18 +00:00
|
|
|
|
|
|
|
then {
|
2020-04-24 21:52:02 +00:00
|
|
|
return Some(expr);
|
2020-04-23 21:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:52:02 +00:00
|
|
|
None
|
2020-04-23 21:28:18 +00:00
|
|
|
}
|
2020-05-11 18:23:47 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_vector(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2020-09-11 03:06:49 +00:00
|
|
|
let ty = ty.peel_refs();
|
2020-11-02 16:32:55 +00:00
|
|
|
is_type_diagnostic_item(cx, ty, sym::vec_type)
|
2020-05-11 18:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_full_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2020-09-11 03:06:49 +00:00
|
|
|
let ty = ty.peel_refs();
|
2020-08-04 13:24:13 +00:00
|
|
|
is_type_lang_item(cx, ty, LangItem::RangeFull)
|
2020-05-11 18:23:47 +00:00
|
|
|
}
|