From 2a52fbe5daaa0f9e583b6f75067c0ef16bf1be9c Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Sat, 1 Feb 2025 12:42:39 +0900 Subject: [PATCH] add MSRV check for `repeat_vec_with_capacity` --- book/src/lint_configuration.md | 1 + clippy_config/src/conf.rs | 1 + clippy_lints/src/lib.rs | 2 +- clippy_lints/src/repeat_vec_with_capacity.rs | 24 +++++++++++++++++--- tests/ui/repeat_vec_with_capacity.fixed | 5 ++++ tests/ui/repeat_vec_with_capacity.rs | 5 ++++ 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index b8f9fff9c613..dfce429a8eeb 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -769,6 +769,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 552141476f3a..c2e97498f4df 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -638,6 +638,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 4b700673d0f8..7f7755015ffc 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -944,7 +944,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { store.register_late_pass(|_| Box::>::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::::default()); diff --git a/clippy_lints/src/repeat_vec_with_capacity.rs b/clippy_lints/src/repeat_vec_with_capacity.rs index 5dddf9263a35..40263afd28f1 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 f72b61b5f6cd..34d9d7c3d71d 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 c0cc81f78436..6c7740b198aa 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)); +}