|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::{match_def_path, meets_msrv, msrvs, paths, visitors::expr_visitor_no_bodies}; |
| 3 | +use rustc_hir::{intravisit::Visitor, Block, ExprKind, QPath, StmtKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_middle::ty; |
| 6 | +use rustc_semver::RustcVersion; |
| 7 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 8 | +use rustc_span::sym; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// This lint checks for `reserve` before calling the `extend` method. |
| 13 | + /// ### Why is this bad? |
| 14 | + /// vec::reserve method before vec::extend is no longer makes sense in rustc version >= 1.62 |
| 15 | + /// ### Example |
| 16 | + /// ```rust |
| 17 | + /// let mut vec: Vec<usize> = vec![]; |
| 18 | + /// let b: &[usize] = [1]; |
| 19 | + /// vec.reserve(b.len()); |
| 20 | + /// vec.extend(b); |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// let mut vec: Vec<usize> = vec![]; |
| 25 | + /// let b: &[usize] = [1]; |
| 26 | + /// vec.extend(b); |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.64.0"] |
| 29 | + pub UNNECESSARY_RESERVE, |
| 30 | + pedantic, |
| 31 | + "`reserve` method before `extend` is no longer makes sense in rustc version >= 1.62" |
| 32 | +} |
| 33 | + |
| 34 | +pub struct UnnecessaryReserve { |
| 35 | + msrv: Option<RustcVersion>, |
| 36 | +} |
| 37 | + |
| 38 | +impl UnnecessaryReserve { |
| 39 | + #[must_use] |
| 40 | + pub fn new(msrv: Option<RustcVersion>) -> Self { |
| 41 | + Self { msrv } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl_lint_pass!(UnnecessaryReserve => [UNNECESSARY_RESERVE]); |
| 46 | + |
| 47 | +impl<'tcx> LateLintPass<'tcx> for UnnecessaryReserve { |
| 48 | + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &Block<'tcx>) { |
| 49 | + if !meets_msrv(self.msrv, msrvs::UNNECESSARY_RESERVE) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + for (idx, stmt) in block.stmts.iter().enumerate() { |
| 54 | + if let StmtKind::Semi(semi_expr) = stmt.kind |
| 55 | + && let ExprKind::MethodCall(_, [struct_calling_on, _], _) = semi_expr.kind |
| 56 | + && let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(semi_expr.hir_id) |
| 57 | + && (match_def_path(cx, expr_def_id, &paths::VEC_RESERVE) || |
| 58 | + match_def_path(cx, expr_def_id, &paths::VEC_DEQUE_RESERVE)) |
| 59 | + && acceptable_type(cx, struct_calling_on) |
| 60 | + && let Some(next_stmt_span) = check_extend_method(cx, block, idx, struct_calling_on) |
| 61 | + && !next_stmt_span.from_expansion() |
| 62 | + { |
| 63 | + span_lint( |
| 64 | + cx, |
| 65 | + UNNECESSARY_RESERVE, |
| 66 | + next_stmt_span, |
| 67 | + "this `reserve` no longer makes sense in rustc version >= 1.62", |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + extract_msrv_attr!(LateContext); |
| 74 | +} |
| 75 | + |
| 76 | +#[must_use] |
| 77 | +fn acceptable_type(cx: &LateContext<'_>, struct_calling_on: &rustc_hir::Expr<'_>) -> bool { |
| 78 | + let acceptable_types = [sym::Vec, sym::VecDeque]; |
| 79 | + acceptable_types.iter().any(|&acceptable_ty| { |
| 80 | + match cx.typeck_results().expr_ty(struct_calling_on).peel_refs().kind() { |
| 81 | + ty::Adt(def, _) => cx.tcx.is_diagnostic_item(acceptable_ty, def.did()), |
| 82 | + _ => false, |
| 83 | + } |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +#[must_use] |
| 88 | +fn check_extend_method( |
| 89 | + cx: &LateContext<'_>, |
| 90 | + block: &Block<'_>, |
| 91 | + idx: usize, |
| 92 | + struct_expr: &rustc_hir::Expr<'_>, |
| 93 | +) -> Option<rustc_span::Span> { |
| 94 | + let mut read_found = false; |
| 95 | + let next_stmt_span; |
| 96 | + |
| 97 | + let mut visitor = expr_visitor_no_bodies(|expr| { |
| 98 | + if let ExprKind::MethodCall(_, [struct_calling_on, _], _) = expr.kind |
| 99 | + && let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) |
| 100 | + && match_def_path(cx, expr_def_id, &paths::ITER_EXTEND) |
| 101 | + && acceptable_type(cx, struct_calling_on) |
| 102 | + && equal_ident(struct_calling_on, struct_expr) |
| 103 | + { |
| 104 | + read_found = true; |
| 105 | + } |
| 106 | + !read_found |
| 107 | + }); |
| 108 | + |
| 109 | + if idx == block.stmts.len() - 1 { |
| 110 | + if let Some(e) = block.expr { |
| 111 | + visitor.visit_expr(e); |
| 112 | + next_stmt_span = e.span; |
| 113 | + } else { |
| 114 | + return None; |
| 115 | + } |
| 116 | + } else { |
| 117 | + let next_stmt = &block.stmts[idx + 1]; |
| 118 | + visitor.visit_stmt(next_stmt); |
| 119 | + next_stmt_span = next_stmt.span; |
| 120 | + } |
| 121 | + drop(visitor); |
| 122 | + |
| 123 | + if read_found { |
| 124 | + return Some(next_stmt_span); |
| 125 | + } |
| 126 | + |
| 127 | + None |
| 128 | +} |
| 129 | + |
| 130 | +#[must_use] |
| 131 | +fn equal_ident(left: &rustc_hir::Expr<'_>, right: &rustc_hir::Expr<'_>) -> bool { |
| 132 | + fn ident_name(expr: &rustc_hir::Expr<'_>) -> Option<rustc_span::Symbol> { |
| 133 | + if let ExprKind::Path(QPath::Resolved(None, inner_path)) = expr.kind |
| 134 | + && let [inner_seg] = inner_path.segments { |
| 135 | + return Some(inner_seg.ident.name); |
| 136 | + } |
| 137 | + None |
| 138 | + } |
| 139 | + |
| 140 | + ident_name(left) == ident_name(right) |
| 141 | +} |
0 commit comments