Skip to content

Commit 77ac3f6

Browse files
committed
Add repeat_n if MSRV is at least 1.83
1 parent 8a852b7 commit 77ac3f6

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ macro_rules! msrv_aliases {
1717

1818
// names may refer to stabilized feature flags or library items
1919
msrv_aliases! {
20-
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY }
20+
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, REPEAT_N }
2121
1,82,0 { IS_NONE_OR }
2222
1,81,0 { LINT_REASONS_STABILIZATION }
2323
1,80,0 { BOX_INTO_ITER}

clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,24 @@ pub(super) fn check(
7878
{
7979
let method_to_use_name;
8080
let new_span;
81+
let use_take;
8182

8283
if eager_or_lazy::switch_to_eager_eval(cx, body_expr) {
83-
method_to_use_name = "repeat";
84-
let body_snippet = snippet_with_applicability(cx, body_expr.span, "..", &mut applicability);
85-
new_span = (arg.span, body_snippet.to_string());
84+
if msrv.meets(msrvs::REPEAT_N) {
85+
method_to_use_name = "repeat_n";
86+
let body_snippet = snippet_with_applicability(cx, body_expr.span, "..", &mut applicability);
87+
new_span = (arg.span, format!("{body_snippet}, {count}"));
88+
use_take = false;
89+
} else {
90+
method_to_use_name = "repeat";
91+
let body_snippet = snippet_with_applicability(cx, body_expr.span, "..", &mut applicability);
92+
new_span = (arg.span, body_snippet.to_string());
93+
use_take = true;
94+
}
8695
} else if msrv.meets(msrvs::REPEAT_WITH) {
8796
method_to_use_name = "repeat_with";
8897
new_span = (param.span, String::new());
98+
use_take = true;
8999
} else {
90100
return;
91101
}
@@ -97,14 +107,25 @@ pub(super) fn check(
97107
"map of a closure that does not depend on its parameter over a range",
98108
|diag| {
99109
diag.multipart_suggestion(
100-
format!("remove the explicit range and use `{method_to_use_name}` and `take`"),
110+
if use_take {
111+
format!("remove the explicit range and use `{method_to_use_name}` and `take`")
112+
} else {
113+
format!("remove the explicit range and use `{method_to_use_name}`")
114+
},
101115
vec![
102116
(
103117
receiver.span.to(method_call_span),
104118
format!("std::iter::{method_to_use_name}"),
105119
),
106120
new_span,
107-
(ex.span.shrink_to_hi(), format!(".take({count})")),
121+
(
122+
ex.span.shrink_to_hi(),
123+
if use_take {
124+
format!(".take({count})")
125+
} else {
126+
String::new()
127+
},
128+
),
108129
],
109130
applicability,
110131
);

tests/ui/map_with_unused_argument_over_ranges.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() {
2727
std::iter::repeat_with(|| do_something()).take(11);
2828
std::iter::repeat_with(|| do_something()).take(7);
2929
std::iter::repeat_with(|| do_something()).take(8);
30-
std::iter::repeat(3).take(10);
30+
std::iter::repeat_n(3, 10);
3131
std::iter::repeat_with(|| {
3232
let x = 3;
3333
x + 2
@@ -66,3 +66,8 @@ fn msrv_1_27() {
6666
fn msrv_1_28() {
6767
std::iter::repeat_with(|| do_something()).take(10);
6868
}
69+
70+
#[clippy::msrv = "1.82"]
71+
fn msrv_1_82() {
72+
std::iter::repeat(3).take(10);
73+
}

tests/ui/map_with_unused_argument_over_ranges.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ fn msrv_1_27() {
6666
fn msrv_1_28() {
6767
(0..10).map(|_| do_something());
6868
}
69+
70+
#[clippy::msrv = "1.82"]
71+
fn msrv_1_82() {
72+
(0..10).map(|_| 3);
73+
}

tests/ui/map_with_unused_argument_over_ranges.stderr

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ error: map of a closure that does not depend on its parameter over a range
6666
LL | (0..10).map(|_| 3);
6767
| ^^^^^^^^^^^^^^^^^^
6868
|
69-
help: remove the explicit range and use `repeat` and `take`
69+
help: remove the explicit range and use `repeat_n`
7070
|
71-
LL | std::iter::repeat(3).take(10);
72-
| ~~~~~~~~~~~~~~~~~ ~ +++++++++
71+
LL | std::iter::repeat_n(3, 10);
72+
| ~~~~~~~~~~~~~~~~~~~ ~~~~~
7373

7474
error: map of a closure that does not depend on its parameter over a range
7575
--> tests/ui/map_with_unused_argument_over_ranges.rs:31:5
@@ -208,5 +208,16 @@ LL - (0..10).map(|_| do_something());
208208
LL + std::iter::repeat_with(|| do_something()).take(10);
209209
|
210210

211-
error: aborting due to 17 previous errors
211+
error: map of a closure that does not depend on its parameter over a range
212+
--> tests/ui/map_with_unused_argument_over_ranges.rs:72:5
213+
|
214+
LL | (0..10).map(|_| 3);
215+
| ^^^^^^^^^^^^^^^^^^
216+
|
217+
help: remove the explicit range and use `repeat` and `take`
218+
|
219+
LL | std::iter::repeat(3).take(10);
220+
| ~~~~~~~~~~~~~~~~~ ~ +++++++++
221+
222+
error: aborting due to 18 previous errors
212223

0 commit comments

Comments
 (0)