Skip to content

Commit 21f02ba

Browse files
committed
Add tests for direct iteration and for array coercing to slice reference
1 parent c970717 commit 21f02ba

5 files changed

+176
-87
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![warn(clippy::chunks_exact_with_const_size)]
2+
#![allow(unused)]
3+
4+
fn main() {
5+
let slice = [1, 2, 3, 4, 5, 6, 7, 8];
6+
7+
// Should NOT trigger - runtime value
8+
let size = 4;
9+
let mut it = slice.chunks_exact(size);
10+
for chunk in it {}
11+
12+
// Should trigger - direct iteration without binding (gets suggestion)
13+
for chunk in slice.as_chunks::<4>().0.iter() {
14+
//~^ chunks_exact_with_const_size
15+
let _ = chunk;
16+
}
17+
18+
// Should trigger - direct iteration with const
19+
const CHUNK_SIZE: usize = 4;
20+
for chunk in slice.as_chunks::<CHUNK_SIZE>().0.iter() {
21+
//~^ chunks_exact_with_const_size
22+
let _ = chunk;
23+
}
24+
25+
// Should trigger - chunks_exact_mut with direct iteration (gets suggestion)
26+
// Note: The suggestion uses .iter() but should use .iter_mut() - this is a known limitation
27+
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
28+
for chunk in arr.as_chunks_mut::<4>().0.iter() {
29+
//~^ chunks_exact_with_const_size
30+
let _ = chunk;
31+
}
32+
}
Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
#![warn(clippy::chunks_exact_with_const_size)]
22
#![allow(unused)]
3-
#![allow(clippy::iter_cloned_collect)]
43

54
fn main() {
65
let slice = [1, 2, 3, 4, 5, 6, 7, 8];
76

8-
// Should trigger lint - literal constant
9-
let result = slice.chunks_exact(4);
10-
//~^ chunks_exact_with_const_size
11-
12-
// Should trigger lint - const value
13-
const CHUNK_SIZE: usize = 4;
14-
let result = slice.chunks_exact(CHUNK_SIZE);
15-
//~^ chunks_exact_with_const_size
16-
177
// Should NOT trigger - runtime value
188
let size = 4;
199
let mut it = slice.chunks_exact(size);
2010
for chunk in it {}
2111

22-
// Should trigger lint - simple iteration
23-
let result = slice.chunks_exact(3);
24-
//~^ chunks_exact_with_const_size
12+
// Should trigger - direct iteration without binding (gets suggestion)
13+
for chunk in slice.chunks_exact(4) {
14+
//~^ chunks_exact_with_const_size
15+
let _ = chunk;
16+
}
2517

26-
// Should trigger - mutable variant
27-
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
28-
let result = arr.chunks_exact_mut(4);
29-
//~^ chunks_exact_with_const_size
18+
// Should trigger - direct iteration with const
19+
const CHUNK_SIZE: usize = 4;
20+
for chunk in slice.chunks_exact(CHUNK_SIZE) {
21+
//~^ chunks_exact_with_const_size
22+
let _ = chunk;
23+
}
3024

31-
// Should trigger - multiline expression
32-
#[rustfmt::skip]
33-
let result = slice
34-
.iter()
35-
.copied()
36-
.collect::<Vec<_>>()
37-
.chunks_exact(2);
38-
//~^ chunks_exact_with_const_size
25+
// Should trigger - chunks_exact_mut with direct iteration (gets suggestion)
26+
// Note: The suggestion uses .iter() but should use .iter_mut() - this is a known limitation
27+
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
28+
for chunk in arr.chunks_exact_mut(4) {
29+
//~^ chunks_exact_with_const_size
30+
let _ = chunk;
31+
}
3932
}
Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,23 @@
11
error: using `chunks_exact` with a constant chunk size
2-
--> tests/ui/chunks_exact_with_const_size.rs:9:24
2+
--> tests/ui/chunks_exact_with_const_size.rs:13:24
33
|
4-
LL | let result = slice.chunks_exact(4);
5-
| ^^^^^^^^^^^^^^^
4+
LL | for chunk in slice.chunks_exact(4) {
5+
| ^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks::<4>().0.iter()`
66
|
7-
help: consider using `as_chunks::<4>()` instead
8-
--> tests/ui/chunks_exact_with_const_size.rs:9:24
9-
|
10-
LL | let result = slice.chunks_exact(4);
11-
| ^^^^^^^^^^^^^^^
12-
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
137
= note: `-D clippy::chunks-exact-with-const-size` implied by `-D warnings`
148
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_with_const_size)]`
159

1610
error: using `chunks_exact` with a constant chunk size
17-
--> tests/ui/chunks_exact_with_const_size.rs:14:24
18-
|
19-
LL | let result = slice.chunks_exact(CHUNK_SIZE);
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^
21-
|
22-
help: consider using `as_chunks::<CHUNK_SIZE>()` instead
23-
--> tests/ui/chunks_exact_with_const_size.rs:14:24
24-
|
25-
LL | let result = slice.chunks_exact(CHUNK_SIZE);
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^
27-
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
28-
29-
error: using `chunks_exact` with a constant chunk size
30-
--> tests/ui/chunks_exact_with_const_size.rs:23:24
31-
|
32-
LL | let result = slice.chunks_exact(3);
33-
| ^^^^^^^^^^^^^^^
34-
|
35-
help: consider using `as_chunks::<3>()` instead
36-
--> tests/ui/chunks_exact_with_const_size.rs:23:24
11+
--> tests/ui/chunks_exact_with_const_size.rs:20:24
3712
|
38-
LL | let result = slice.chunks_exact(3);
39-
| ^^^^^^^^^^^^^^^
40-
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
13+
LL | for chunk in slice.chunks_exact(CHUNK_SIZE) {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks::<CHUNK_SIZE>().0.iter()`
4115

4216
error: using `chunks_exact_mut` with a constant chunk size
4317
--> tests/ui/chunks_exact_with_const_size.rs:28:22
4418
|
45-
LL | let result = arr.chunks_exact_mut(4);
46-
| ^^^^^^^^^^^^^^^^^^^
47-
|
48-
help: consider using `as_chunks_mut::<4>()` instead
49-
--> tests/ui/chunks_exact_with_const_size.rs:28:22
50-
|
51-
LL | let result = arr.chunks_exact_mut(4);
52-
| ^^^^^^^^^^^^^^^^^^^
53-
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
54-
55-
error: using `chunks_exact` with a constant chunk size
56-
--> tests/ui/chunks_exact_with_const_size.rs:37:10
57-
|
58-
LL | .chunks_exact(2);
59-
| ^^^^^^^^^^^^^^^
60-
|
61-
help: consider using `as_chunks::<2>()` instead
62-
--> tests/ui/chunks_exact_with_const_size.rs:37:10
63-
|
64-
LL | .chunks_exact(2);
65-
| ^^^^^^^^^^^^^^^
66-
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
19+
LL | for chunk in arr.chunks_exact_mut(4) {
20+
| ^^^^^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks_mut::<4>().0.iter()`
6721

68-
error: aborting due to 5 previous errors
22+
error: aborting due to 3 previous errors
6923

tests/ui/chunks_exact_with_const_size_unfixable.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
#![warn(clippy::chunks_exact_with_const_size)]
22
#![allow(unused)]
3+
#![allow(clippy::iter_cloned_collect)]
34

45
fn main() {
56
let slice = [1, 2, 3, 4, 5, 6, 7, 8];
67
const CHUNK_SIZE: usize = 4;
78

9+
// Should trigger lint - literal constant (stored in let binding, so help not suggestion)
10+
let result = slice.chunks_exact(4);
11+
//~^ chunks_exact_with_const_size
12+
13+
// Should trigger lint - const value
14+
let result = slice.chunks_exact(CHUNK_SIZE);
15+
//~^ chunks_exact_with_const_size
16+
17+
// Should trigger lint - simple iteration
18+
let result = slice.chunks_exact(3);
19+
//~^ chunks_exact_with_const_size
20+
21+
// Should trigger - mutable variant
22+
let mut arr = [1, 2, 3, 4, 5, 6, 7, 8];
23+
let result = arr.chunks_exact_mut(4);
24+
//~^ chunks_exact_with_const_size
25+
26+
// Should trigger - multiline expression
27+
#[rustfmt::skip]
28+
let result = slice
29+
.iter()
30+
.copied()
31+
.collect::<Vec<_>>()
32+
.chunks_exact(2);
33+
//~^ chunks_exact_with_const_size
34+
35+
// Should trigger - array coerces to slice reference
36+
let array = [1, 2, 3, 4, 5, 6, 7, 8];
37+
let result = array.chunks_exact(4);
38+
//~^ chunks_exact_with_const_size
39+
840
// Should trigger lint with help message only (not suggestion) - stored in variable
941
let mut chunk_iter = slice.chunks_exact(CHUNK_SIZE);
1042
//~^ chunks_exact_with_const_size
Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,108 @@
11
error: using `chunks_exact` with a constant chunk size
2-
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:9:32
2+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:10:24
3+
|
4+
LL | let result = slice.chunks_exact(4);
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
help: consider using `as_chunks::<4>()` instead
8+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:10:24
9+
|
10+
LL | let result = slice.chunks_exact(4);
11+
| ^^^^^^^^^^^^^^^
12+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
13+
= note: `-D clippy::chunks-exact-with-const-size` implied by `-D warnings`
14+
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_with_const_size)]`
15+
16+
error: using `chunks_exact` with a constant chunk size
17+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:14:24
18+
|
19+
LL | let result = slice.chunks_exact(CHUNK_SIZE);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^
21+
|
22+
help: consider using `as_chunks::<CHUNK_SIZE>()` instead
23+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:14:24
24+
|
25+
LL | let result = slice.chunks_exact(CHUNK_SIZE);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^
27+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
28+
29+
error: using `chunks_exact` with a constant chunk size
30+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:18:24
31+
|
32+
LL | let result = slice.chunks_exact(3);
33+
| ^^^^^^^^^^^^^^^
34+
|
35+
help: consider using `as_chunks::<3>()` instead
36+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:18:24
37+
|
38+
LL | let result = slice.chunks_exact(3);
39+
| ^^^^^^^^^^^^^^^
40+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
41+
42+
error: using `chunks_exact_mut` with a constant chunk size
43+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:23:22
44+
|
45+
LL | let result = arr.chunks_exact_mut(4);
46+
| ^^^^^^^^^^^^^^^^^^^
47+
|
48+
help: consider using `as_chunks_mut::<4>()` instead
49+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:23:22
50+
|
51+
LL | let result = arr.chunks_exact_mut(4);
52+
| ^^^^^^^^^^^^^^^^^^^
53+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
54+
55+
error: using `chunks_exact` with a constant chunk size
56+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:32:10
57+
|
58+
LL | .chunks_exact(2);
59+
| ^^^^^^^^^^^^^^^
60+
|
61+
help: consider using `as_chunks::<2>()` instead
62+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:32:10
63+
|
64+
LL | .chunks_exact(2);
65+
| ^^^^^^^^^^^^^^^
66+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
67+
68+
error: using `chunks_exact` with a constant chunk size
69+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:37:24
70+
|
71+
LL | let result = array.chunks_exact(4);
72+
| ^^^^^^^^^^^^^^^
73+
|
74+
help: consider using `as_chunks::<4>()` instead
75+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:37:24
76+
|
77+
LL | let result = array.chunks_exact(4);
78+
| ^^^^^^^^^^^^^^^
79+
= note: you can access the chunks using `result.0.iter()`, and the remainder using `result.1`
80+
81+
error: using `chunks_exact` with a constant chunk size
82+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:41:32
383
|
484
LL | let mut chunk_iter = slice.chunks_exact(CHUNK_SIZE);
585
| ^^^^^^^^^^^^^^^^^^^^^^^^
686
|
787
help: consider using `as_chunks::<CHUNK_SIZE>()` instead
8-
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:9:32
88+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:41:32
989
|
1090
LL | let mut chunk_iter = slice.chunks_exact(CHUNK_SIZE);
1191
| ^^^^^^^^^^^^^^^^^^^^^^^^
1292
= note: you can access the chunks using `chunk_iter.0.iter()`, and the remainder using `chunk_iter.1`
13-
= note: `-D clippy::chunks-exact-with-const-size` implied by `-D warnings`
14-
= help: to override `-D warnings` add `#[allow(clippy::chunks_exact_with_const_size)]`
1593

1694
error: using `chunks_exact_mut` with a constant chunk size
17-
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:16:31
95+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:48:31
1896
|
1997
LL | let mut chunk_iter = arr2.chunks_exact_mut(CHUNK_SIZE);
2098
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2199
|
22100
help: consider using `as_chunks_mut::<CHUNK_SIZE>()` instead
23-
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:16:31
101+
--> tests/ui/chunks_exact_with_const_size_unfixable.rs:48:31
24102
|
25103
LL | let mut chunk_iter = arr2.chunks_exact_mut(CHUNK_SIZE);
26104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27105
= note: you can access the chunks using `chunk_iter.0.iter()`, and the remainder using `chunk_iter.1`
28106

29-
error: aborting due to 2 previous errors
107+
error: aborting due to 8 previous errors
30108

0 commit comments

Comments
 (0)