Skip to content

Commit 409fc81

Browse files
fix:(chunks_exact_to_as_chunks): Pick iter method depending on mut-ness (#17316)
`chunks_exact_to_as_chunks` always suggested using `.iter()` to turn a slice into an iterator even if the slice is `&mut [T]`. This loses the mut-ness of the slice, making the suggestion wrong. This PR fixes this by suggesting `.iter()` if the slice is immutable and `.iter_mut()` if it is mutable. I added a new line to the existing UI test that the suggested snippet uses `iter_mut` (when correct). --- changelog: [`chunks_exact_to_as_chunks`]: Suggest `iter`/`iter_mut` depending on mut-ness
2 parents ffd8256 + b63e08a commit 409fc81

5 files changed

Lines changed: 59 additions & 8 deletions

clippy_lints/src/methods/chunks_exact_to_as_chunks.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ pub(super) fn check<'tcx>(
3636
return;
3737
}
3838

39-
let suggestion_method = if method_name == sym::chunks_exact_mut {
40-
"as_chunks_mut"
41-
} else {
42-
"as_chunks"
43-
};
39+
let is_mut = method_name == sym::chunks_exact_mut;
40+
let suggestion_method = if is_mut { "as_chunks_mut" } else { "as_chunks" };
41+
let iter_method = if is_mut { "iter_mut" } else { "iter" };
4442

4543
let mut applicability = Applicability::MachineApplicable;
4644
let arg_str = snippet_with_context(cx, arg.span, expr.span.ctxt(), "_", &mut applicability).0;
@@ -80,7 +78,7 @@ pub(super) fn check<'tcx>(
8078
diag.span_suggestion(
8179
call_span,
8280
"consider using `as_chunks` instead",
83-
format!("{as_chunks}.0.iter()"),
81+
format!("{as_chunks}.0.{iter_method}()"),
8482
applicability,
8583
);
8684
return;
@@ -95,7 +93,7 @@ pub(super) fn check<'tcx>(
9593
&& let PatKind::Binding(_, _, ident, _) = let_stmt.pat.kind
9694
{
9795
diag.note(format!(
98-
"you can access the chunks using `{ident}.0.iter()`, and the remainder using `{ident}.1`"
96+
"you can access the chunks using `{ident}.0.{iter_method}()`, and the remainder using `{ident}.1`"
9997
));
10098
}
10199
},

tests/ui/chunks_exact_to_as_chunks.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ help: consider using `as_chunks_mut::<4>()` instead
5050
|
5151
LL | let mut it = arr.chunks_exact_mut(4);
5252
| ^^^^^^^^^^^^^^^^^^^
53-
= note: you can access the chunks using `it.0.iter()`, and the remainder using `it.1`
53+
= note: you can access the chunks using `it.0.iter_mut()`, and the remainder using `it.1`
5454

5555
error: aborting due to 4 previous errors
5656

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::chunks_exact_to_as_chunks)]
2+
#![allow(unused)]
3+
4+
fn main() {
5+
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
6+
7+
for _ in arr.as_chunks::<4>().0 {}
8+
//~^ chunks_exact_to_as_chunks
9+
for _ in arr.as_chunks_mut::<4>().0 {}
10+
//~^ chunks_exact_to_as_chunks
11+
for chunk in arr.as_chunks_mut::<4>().0.iter_mut().take(2) {
12+
//~^ chunks_exact_to_as_chunks
13+
chunk[0] += 1; // mutate chunks
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::chunks_exact_to_as_chunks)]
2+
#![allow(unused)]
3+
4+
fn main() {
5+
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
6+
7+
for _ in arr.chunks_exact(4) {}
8+
//~^ chunks_exact_to_as_chunks
9+
for _ in arr.chunks_exact_mut(4) {}
10+
//~^ chunks_exact_to_as_chunks
11+
for chunk in arr.chunks_exact_mut(4).take(2) {
12+
//~^ chunks_exact_to_as_chunks
13+
chunk[0] += 1; // mutate chunks
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: using `chunks_exact` with a constant chunk size
2+
--> tests/ui/chunks_exact_to_as_chunks_fixable.rs:7:18
3+
|
4+
LL | for _ in arr.chunks_exact(4) {}
5+
| ^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks::<4>().0`
6+
|
7+
= note: `-D clippy::chunks-exact-to-as-chunks` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_to_as_chunks)]`
9+
10+
error: using `chunks_exact_mut` with a constant chunk size
11+
--> tests/ui/chunks_exact_to_as_chunks_fixable.rs:9:18
12+
|
13+
LL | for _ in arr.chunks_exact_mut(4) {}
14+
| ^^^^^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks_mut::<4>().0`
15+
16+
error: using `chunks_exact_mut` with a constant chunk size
17+
--> tests/ui/chunks_exact_to_as_chunks_fixable.rs:11:22
18+
|
19+
LL | for chunk in arr.chunks_exact_mut(4).take(2) {
20+
| ^^^^^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks_mut::<4>().0.iter_mut()`
21+
22+
error: aborting due to 3 previous errors
23+

0 commit comments

Comments
 (0)