|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::visitors::for_each_expr; |
| 5 | +use clippy_utils::{SpanlessEq, get_enclosing_block, match_def_path, paths}; |
| 6 | +use core::ops::ControlFlow; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Block, Expr, ExprKind, PathSegment}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_middle::ty; |
| 11 | +use rustc_session::impl_lint_pass; |
| 12 | +use rustc_span::sym; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// |
| 17 | + /// This lint checks for a call to `reserve` before `extend` on a `Vec` or `VecDeque`. |
| 18 | + /// ### Why is this bad? |
| 19 | + /// Since Rust 1.62, `extend` implicitly calls `reserve` |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// let mut vec: Vec<usize> = vec![]; |
| 24 | + /// let array: &[usize] = &[1, 2]; |
| 25 | + /// vec.reserve(array.len()); |
| 26 | + /// vec.extend(array); |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust |
| 30 | + /// let mut vec: Vec<usize> = vec![]; |
| 31 | + /// let array: &[usize] = &[1, 2]; |
| 32 | + /// vec.extend(array); |
| 33 | + /// ``` |
| 34 | + #[clippy::version = "1.64.0"] |
| 35 | + pub UNNECESSARY_RESERVE, |
| 36 | + pedantic, |
| 37 | + "calling `reserve` before `extend` on a `Vec` or `VecDeque`, when it will be called implicitly" |
| 38 | +} |
| 39 | + |
| 40 | +impl_lint_pass!(UnnecessaryReserve => [UNNECESSARY_RESERVE]); |
| 41 | + |
| 42 | +pub struct UnnecessaryReserve { |
| 43 | + msrv: Msrv, |
| 44 | +} |
| 45 | +impl UnnecessaryReserve { |
| 46 | + pub fn new(conf: &'static Conf) -> Self { |
| 47 | + Self { |
| 48 | + msrv: conf.msrv.clone(), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<'tcx> LateLintPass<'tcx> for UnnecessaryReserve { |
| 54 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 55 | + if !self.msrv.meets(msrvs::EXTEND_IMPLICIT_RESERVE) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if let ExprKind::MethodCall(PathSegment { ident: method, .. }, struct_calling_on, args_a, _) = expr.kind |
| 60 | + && method.name.as_str() == "reserve" |
| 61 | + && acceptable_type(cx, struct_calling_on) |
| 62 | + && let Some(block) = get_enclosing_block(cx, expr.hir_id) |
| 63 | + && let Some(next_stmt_span) = check_extend_method(cx, block, struct_calling_on, &args_a[0]) |
| 64 | + && !next_stmt_span.from_expansion() |
| 65 | + { |
| 66 | + span_lint_and_then( |
| 67 | + cx, |
| 68 | + UNNECESSARY_RESERVE, |
| 69 | + next_stmt_span, |
| 70 | + "unnecessary call to `reserve`", |
| 71 | + |diag| { |
| 72 | + diag.span_suggestion( |
| 73 | + expr.span, |
| 74 | + "remove this line", |
| 75 | + String::new(), |
| 76 | + Applicability::MaybeIncorrect, |
| 77 | + ); |
| 78 | + }, |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + extract_msrv_attr!(LateContext); |
| 84 | +} |
| 85 | + |
| 86 | +#[must_use] |
| 87 | +fn acceptable_type<'tcx>(cx: &LateContext<'tcx>, struct_calling_on: &Expr<'_>) -> bool { |
| 88 | + let acceptable_types = [sym::Vec, sym::VecDeque]; |
| 89 | + acceptable_types.iter().any(|&acceptable_ty| { |
| 90 | + match cx.typeck_results().expr_ty(struct_calling_on).peel_refs().kind() { |
| 91 | + ty::Adt(def, _) => cx.tcx.is_diagnostic_item(acceptable_ty, def.did()), |
| 92 | + _ => false, |
| 93 | + } |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +#[must_use] |
| 98 | +fn check_extend_method<'tcx>( |
| 99 | + cx: &LateContext<'tcx>, |
| 100 | + block: &'tcx Block<'tcx>, |
| 101 | + struct_expr: &Expr<'tcx>, |
| 102 | + args_a: &Expr<'tcx>, |
| 103 | +) -> Option<rustc_span::Span> { |
| 104 | + let args_a_kind = &args_a.kind; |
| 105 | + let mut read_found = false; |
| 106 | + let mut spanless_eq = SpanlessEq::new(cx); |
| 107 | + |
| 108 | + let _: Option<!> = for_each_expr(cx, block, |expr: &Expr<'tcx>| { |
| 109 | + if let ExprKind::MethodCall(_, struct_calling_on, _, _) = expr.kind |
| 110 | + && let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) |
| 111 | + && let ExprKind::MethodCall( |
| 112 | + PathSegment { |
| 113 | + ident: method_call_a, .. |
| 114 | + }, |
| 115 | + .., |
| 116 | + ) = args_a_kind |
| 117 | + && method_call_a.name == sym::len |
| 118 | + && match_def_path(cx, expr_def_id, &paths::ITER_EXTEND) |
| 119 | + && acceptable_type(cx, struct_calling_on) |
| 120 | + && spanless_eq.eq_expr(struct_calling_on, struct_expr) |
| 121 | + { |
| 122 | + read_found = true; |
| 123 | + } |
| 124 | + let _: bool = !read_found; |
| 125 | + ControlFlow::Continue(()) |
| 126 | + }); |
| 127 | + |
| 128 | + if read_found { Some(block.span) } else { None } |
| 129 | +} |
0 commit comments