Skip to content

Commit 35d5ee0

Browse files
authored
add MSRV check for repeat_vec_with_capacity (#14126)
changelog: [`repeat_vec_with_capacity`]: add MSRV check
2 parents d92da0f + 2a52fbe commit 35d5ee0

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

book/src/lint_configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
797797
* [`ptr_as_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr)
798798
* [`redundant_field_names`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names)
799799
* [`redundant_static_lifetimes`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes)
800+
* [`repeat_vec_with_capacity`](https://rust-lang.github.io/rust-clippy/master/index.html#repeat_vec_with_capacity)
800801
* [`same_item_push`](https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push)
801802
* [`seek_from_current`](https://rust-lang.github.io/rust-clippy/master/index.html#seek_from_current)
802803
* [`seek_rewind`](https://rust-lang.github.io/rust-clippy/master/index.html#seek_rewind)

clippy_config/src/conf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ define_Conf! {
652652
ptr_as_ptr,
653653
redundant_field_names,
654654
redundant_static_lifetimes,
655+
repeat_vec_with_capacity,
655656
same_item_push,
656657
seek_from_current,
657658
seek_rewind,

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
947947
store.register_late_pass(|_| Box::<pathbuf_init_then_push::PathbufThenPush<'_>>::default());
948948
store.register_late_pass(|_| Box::new(iter_over_hash_type::IterOverHashType));
949949
store.register_late_pass(|_| Box::new(impl_hash_with_borrow_str_and_bytes::ImplHashWithBorrowStrBytes));
950-
store.register_late_pass(|_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity));
950+
store.register_late_pass(move |_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity::new(conf)));
951951
store.register_late_pass(|_| Box::new(uninhabited_references::UninhabitedReferences));
952952
store.register_late_pass(|_| Box::new(ineffective_open_options::IneffectiveOpenOptions));
953953
store.register_late_pass(|_| Box::<unconditional_recursion::UnconditionalRecursion>::default());

clippy_lints/src/repeat_vec_with_capacity.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
use clippy_config::Conf;
12
use clippy_utils::consts::{ConstEvalCtxt, Constant};
23
use clippy_utils::diagnostics::span_lint_and_then;
34
use clippy_utils::higher::VecArgs;
45
use clippy_utils::macros::matching_root_macro_call;
6+
use clippy_utils::msrvs::{self, Msrv};
57
use clippy_utils::source::snippet;
68
use clippy_utils::{expr_or_init, fn_def_id, std_or_core};
79
use rustc_errors::Applicability;
810
use rustc_hir::{Expr, ExprKind};
911
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_session::declare_lint_pass;
12+
use rustc_session::impl_lint_pass;
1113
use rustc_span::{Span, sym};
1214

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+
1327
declare_clippy_lint! {
1428
/// ### What it does
1529
/// 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! {
4862
"repeating a `Vec::with_capacity` expression which does not retain capacity"
4963
}
5064

51-
declare_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]);
65+
impl_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]);
5266

5367
fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, sugg_msg: &'static str, sugg: String) {
5468
span_lint_and_then(
@@ -112,6 +126,10 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
112126
impl LateLintPass<'_> for RepeatVecWithCapacity {
113127
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
114128
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+
}
116132
}
133+
134+
extract_msrv_attr!(LateContext);
117135
}

tests/ui/repeat_vec_with_capacity.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ fn main() {
3737
from_macro!(Vec::<()>::with_capacity(42));
3838
}
3939
}
40+
41+
#[clippy::msrv = "1.27.0"]
42+
fn msrv_check() {
43+
std::iter::repeat(Vec::<()>::with_capacity(42));
44+
}

tests/ui/repeat_vec_with_capacity.rs

+5
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ fn main() {
3737
from_macro!(Vec::<()>::with_capacity(42));
3838
}
3939
}
40+
41+
#[clippy::msrv = "1.27.0"]
42+
fn msrv_check() {
43+
std::iter::repeat(Vec::<()>::with_capacity(42));
44+
}

0 commit comments

Comments
 (0)