|
| 1 | +use clippy_config::Conf; |
1 | 2 | use clippy_utils::consts::{ConstEvalCtxt, Constant}; |
2 | 3 | use clippy_utils::diagnostics::span_lint_and_then; |
3 | 4 | use clippy_utils::higher::VecArgs; |
4 | 5 | use clippy_utils::macros::matching_root_macro_call; |
| 6 | +use clippy_utils::msrvs::{self, Msrv}; |
5 | 7 | use clippy_utils::source::snippet; |
6 | 8 | use clippy_utils::{expr_or_init, fn_def_id, std_or_core}; |
7 | 9 | use rustc_errors::Applicability; |
8 | 10 | use rustc_hir::{Expr, ExprKind}; |
9 | 11 | use rustc_lint::{LateContext, LateLintPass}; |
10 | | -use rustc_session::declare_lint_pass; |
| 12 | +use rustc_session::impl_lint_pass; |
11 | 13 | use rustc_span::{Span, sym}; |
12 | 14 |
|
| 15 | +pub struct RepeatVecWithCapacity { |
| 16 | + msrv: Msrv, |
| 17 | +} |
| 18 | + |
| 19 | +impl RepeatVecWithCapacity { |
| 20 | + pub fn new(conf: &'static Conf) -> Self { |
| 21 | + Self { |
| 22 | + msrv: conf.msrv.clone(), |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
13 | 27 | declare_clippy_lint! { |
14 | 28 | /// ### What it does |
15 | 29 | /// Looks for patterns such as `vec![Vec::with_capacity(x); n]` or `iter::repeat(Vec::with_capacity(x))`. |
@@ -48,7 +62,7 @@ declare_clippy_lint! { |
48 | 62 | "repeating a `Vec::with_capacity` expression which does not retain capacity" |
49 | 63 | } |
50 | 64 |
|
51 | | -declare_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]); |
| 65 | +impl_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]); |
52 | 66 |
|
53 | 67 | fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, sugg_msg: &'static str, sugg: String) { |
54 | 68 | span_lint_and_then( |
@@ -112,6 +126,10 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) { |
112 | 126 | impl LateLintPass<'_> for RepeatVecWithCapacity { |
113 | 127 | fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
114 | 128 | check_vec_macro(cx, expr); |
115 | | - check_repeat_fn(cx, expr); |
| 129 | + if self.msrv.meets(msrvs::REPEAT_WITH) { |
| 130 | + check_repeat_fn(cx, expr); |
| 131 | + } |
116 | 132 | } |
| 133 | + |
| 134 | + extract_msrv_attr!(LateContext); |
117 | 135 | } |
0 commit comments