Skip to content

Commit 2a52fbe

Browse files
committed
add MSRV check for repeat_vec_with_capacity
1 parent 88a00a8 commit 2a52fbe

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
@@ -769,6 +769,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
769769
* [`ptr_as_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr)
770770
* [`redundant_field_names`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names)
771771
* [`redundant_static_lifetimes`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes)
772+
* [`repeat_vec_with_capacity`](https://rust-lang.github.io/rust-clippy/master/index.html#repeat_vec_with_capacity)
772773
* [`same_item_push`](https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push)
773774
* [`seek_from_current`](https://rust-lang.github.io/rust-clippy/master/index.html#seek_from_current)
774775
* [`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
@@ -638,6 +638,7 @@ define_Conf! {
638638
ptr_as_ptr,
639639
redundant_field_names,
640640
redundant_static_lifetimes,
641+
repeat_vec_with_capacity,
641642
same_item_push,
642643
seek_from_current,
643644
seek_rewind,

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
944944
store.register_late_pass(|_| Box::<pathbuf_init_then_push::PathbufThenPush<'_>>::default());
945945
store.register_late_pass(|_| Box::new(iter_over_hash_type::IterOverHashType));
946946
store.register_late_pass(|_| Box::new(impl_hash_with_borrow_str_and_bytes::ImplHashWithBorrowStrBytes));
947-
store.register_late_pass(|_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity));
947+
store.register_late_pass(move |_| Box::new(repeat_vec_with_capacity::RepeatVecWithCapacity::new(conf)));
948948
store.register_late_pass(|_| Box::new(uninhabited_references::UninhabitedReferences));
949949
store.register_late_pass(|_| Box::new(ineffective_open_options::IneffectiveOpenOptions));
950950
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)