about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2025-02-23 15:50:44 +0000
committerGitHub <noreply@github.com>2025-02-23 15:50:44 +0000
commit35d5ee0e41a3eee80f4bc009529ed486cf427dfc (patch)
treeaf1e47b1cfaca77532f5efd8076f48a7b6bdf8dd
parentd92da0fb322e833e481a4c50f0f73b7b27829546 (diff)
parent2a52fbe5daaa0f9e583b6f75067c0ef16bf1be9c (diff)
downloadrust-35d5ee0e41a3eee80f4bc009529ed486cf427dfc.tar.gz
rust-35d5ee0e41a3eee80f4bc009529ed486cf427dfc.zip
add MSRV check for `repeat_vec_with_capacity` (#14126)
changelog: [`repeat_vec_with_capacity`]: add MSRV check
-rw-r--r--book/src/lint_configuration.md1
-rw-r--r--clippy_config/src/conf.rs1
-rw-r--r--clippy_lints/src/lib.rs2
-rw-r--r--clippy_lints/src/repeat_vec_with_capacity.rs24
-rw-r--r--tests/ui/repeat_vec_with_capacity.fixed5
-rw-r--r--tests/ui/repeat_vec_with_capacity.rs5
6 files changed, 34 insertions, 4 deletions
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index 74c2be3479e..7a70c6c40ac 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -797,6 +797,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
 * [`ptr_as_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr)
 * [`redundant_field_names`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names)
 * [`redundant_static_lifetimes`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes)
+* [`repeat_vec_with_capacity`](https://rust-lang.github.io/rust-clippy/master/index.html#repeat_vec_with_capacity)
 * [`same_item_push`](https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push)
 * [`seek_from_current`](https://rust-lang.github.io/rust-clippy/master/index.html#seek_from_current)
 * [`seek_rewind`](https://rust-lang.github.io/rust-clippy/master/index.html#seek_rewind)
diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs
index ff2a8618240..23d585a6c15 100644
--- a/clippy_config/src/conf.rs
+++ b/clippy_config/src/conf.rs
@@ -652,6 +652,7 @@ define_Conf! {
         ptr_as_ptr,
         redundant_field_names,
         redundant_static_lifetimes,
+        repeat_vec_with_capacity,
         same_item_push,
         seek_from_current,
         seek_rewind,
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index c9e9a444864..f00ae5a9731 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -947,7 +947,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
     store.register_late_pass(|_| Box::<pathbuf_init_then_push::PathbufThenPush<'_>>::default());
     store.register_late_pass(|_| Box::new(iter_over_hash_type::IterOverHashType));
     store.register_late_pass(|_| Box::new(impl_hash_with_borrow_str_and_bytes::ImplHashWithBorrowStrBytes));
-    store.register_late_pass(|_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity));
+    store.register_late_pass(move |_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity::new(conf)));
     store.register_late_pass(|_| Box::new(uninhabited_references::UninhabitedReferences));
     store.register_late_pass(|_| Box::new(ineffective_open_options::IneffectiveOpenOptions));
     store.register_late_pass(|_| Box::<unconditional_recursion::UnconditionalRecursion>::default());
diff --git a/clippy_lints/src/repeat_vec_with_capacity.rs b/clippy_lints/src/repeat_vec_with_capacity.rs
index 5dddf9263a3..40263afd28f 100644
--- a/clippy_lints/src/repeat_vec_with_capacity.rs
+++ b/clippy_lints/src/repeat_vec_with_capacity.rs
@@ -1,15 +1,29 @@
+use clippy_config::Conf;
 use clippy_utils::consts::{ConstEvalCtxt, Constant};
 use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::higher::VecArgs;
 use clippy_utils::macros::matching_root_macro_call;
+use clippy_utils::msrvs::{self, Msrv};
 use clippy_utils::source::snippet;
 use clippy_utils::{expr_or_init, fn_def_id, std_or_core};
 use rustc_errors::Applicability;
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
-use rustc_session::declare_lint_pass;
+use rustc_session::impl_lint_pass;
 use rustc_span::{Span, sym};
 
+pub struct RepeatVecWithCapacity {
+    msrv: Msrv,
+}
+
+impl RepeatVecWithCapacity {
+    pub fn new(conf: &'static Conf) -> Self {
+        Self {
+            msrv: conf.msrv.clone(),
+        }
+    }
+}
+
 declare_clippy_lint! {
     /// ### What it does
     /// 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! {
     "repeating a `Vec::with_capacity` expression which does not retain capacity"
 }
 
-declare_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]);
+impl_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]);
 
 fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, sugg_msg: &'static str, sugg: String) {
     span_lint_and_then(
@@ -112,6 +126,10 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
 impl LateLintPass<'_> for RepeatVecWithCapacity {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
         check_vec_macro(cx, expr);
-        check_repeat_fn(cx, expr);
+        if self.msrv.meets(msrvs::REPEAT_WITH) {
+            check_repeat_fn(cx, expr);
+        }
     }
+
+    extract_msrv_attr!(LateContext);
 }
diff --git a/tests/ui/repeat_vec_with_capacity.fixed b/tests/ui/repeat_vec_with_capacity.fixed
index 5c6736a9a4e..8a3b0b4f193 100644
--- a/tests/ui/repeat_vec_with_capacity.fixed
+++ b/tests/ui/repeat_vec_with_capacity.fixed
@@ -37,3 +37,8 @@ fn main() {
         from_macro!(Vec::<()>::with_capacity(42));
     }
 }
+
+#[clippy::msrv = "1.27.0"]
+fn msrv_check() {
+    std::iter::repeat(Vec::<()>::with_capacity(42));
+}
diff --git a/tests/ui/repeat_vec_with_capacity.rs b/tests/ui/repeat_vec_with_capacity.rs
index 2646794822c..011202d8475 100644
--- a/tests/ui/repeat_vec_with_capacity.rs
+++ b/tests/ui/repeat_vec_with_capacity.rs
@@ -37,3 +37,8 @@ fn main() {
         from_macro!(Vec::<()>::with_capacity(42));
     }
 }
+
+#[clippy::msrv = "1.27.0"]
+fn msrv_check() {
+    std::iter::repeat(Vec::<()>::with_capacity(42));
+}