add MSRV check for repeat_vec_with_capacity (#14126)

changelog: [`repeat_vec_with_capacity`]: add MSRV check
This commit is contained in:
Manish Goregaokar 2025-02-23 15:50:44 +00:00 committed by GitHub
commit 35d5ee0e41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 4 deletions

View file

@ -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)

View file

@ -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,

View file

@ -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());

View file

@ -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);
}

View file

@ -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));
}

View file

@ -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));
}