Skip to content

Commit

Permalink
add MSRV check for repeat_vec_with_capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Feb 1, 2025
1 parent 88a00a8 commit 2a52fbe
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,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());
Expand Down
24 changes: 21 additions & 3 deletions clippy_lints/src/repeat_vec_with_capacity.rs
Original file line number Diff line number Diff line change
@@ -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))`.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions tests/ui/repeat_vec_with_capacity.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
5 changes: 5 additions & 0 deletions tests/ui/repeat_vec_with_capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 2a52fbe

Please sign in to comment.