Skip to content

Commit 5c71352

Browse files
committed
Prevent unnecessary lints from triggering
1 parent 4918e7a commit 5c71352

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

clippy_lints/src/loops.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,14 @@ fn check_for_loop<'tcx>(
768768
body: &'tcx Expr<'_>,
769769
expr: &'tcx Expr<'_>,
770770
) {
771-
check_for_loop_range(cx, pat, arg, body, expr);
771+
let is_manual_memcpy_triggered = detect_manual_memcpy(cx, pat, arg, body, expr);
772+
if !is_manual_memcpy_triggered {
773+
check_for_loop_range(cx, pat, arg, body, expr);
774+
check_for_loop_explicit_counter(cx, pat, arg, body, expr);
775+
}
772776
check_for_loop_arg(cx, pat, arg, expr);
773-
check_for_loop_explicit_counter(cx, pat, arg, body, expr);
774777
check_for_loop_over_map_kv(cx, pat, arg, body, expr);
775778
check_for_mut_range_bound(cx, arg, body);
776-
detect_manual_memcpy(cx, pat, arg, body, expr);
777779
detect_same_item_push(cx, pat, arg, body, expr);
778780
}
779781

@@ -1152,7 +1154,7 @@ fn detect_manual_memcpy<'tcx>(
11521154
arg: &'tcx Expr<'_>,
11531155
body: &'tcx Expr<'_>,
11541156
expr: &'tcx Expr<'_>,
1155-
) {
1157+
) -> bool {
11561158
if let Some(higher::Range {
11571159
start: Some(start),
11581160
end: Some(end),
@@ -1222,9 +1224,11 @@ fn detect_manual_memcpy<'tcx>(
12221224
big_sugg,
12231225
Applicability::Unspecified,
12241226
);
1227+
return true;
12251228
}
12261229
}
12271230
}
1231+
false
12281232
}
12291233

12301234
// Scans the body of the for loop and determines whether lint should be given

tests/ui/manual_memcpy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
115115
}
116116
}
117117

118-
#[allow(clippy::needless_range_loop, clippy::explicit_counter_loop)]
119118
pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
120119
let mut count = 0;
121120
for i in 3..src.len() {

tests/ui/manual_memcpy.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,49 +79,49 @@ LL | for i in 0..0 {
7979
| ^^^^ help: try replacing the loop by: `dst[..0].clone_from_slice(&src[..0])`
8080

8181
error: it looks like you're manually copying between slices
82-
--> $DIR/manual_memcpy.rs:121:14
82+
--> $DIR/manual_memcpy.rs:120:14
8383
|
8484
LL | for i in 3..src.len() {
8585
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[3..src.len()].clone_from_slice(&src[..(src.len() - 3)])`
8686

8787
error: it looks like you're manually copying between slices
88-
--> $DIR/manual_memcpy.rs:127:14
88+
--> $DIR/manual_memcpy.rs:126:14
8989
|
9090
LL | for i in 3..src.len() {
9191
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..(src.len() - 3)].clone_from_slice(&src[3..])`
9292

9393
error: it looks like you're manually copying between slices
94-
--> $DIR/manual_memcpy.rs:133:14
94+
--> $DIR/manual_memcpy.rs:132:14
9595
|
9696
LL | for i in 0..src.len() {
9797
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[3..(src.len() + 3)].clone_from_slice(&src[..])`
9898

9999
error: it looks like you're manually copying between slices
100-
--> $DIR/manual_memcpy.rs:139:14
100+
--> $DIR/manual_memcpy.rs:138:14
101101
|
102102
LL | for i in 0..src.len() {
103103
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[3..(src.len() + 3)])`
104104

105105
error: it looks like you're manually copying between slices
106-
--> $DIR/manual_memcpy.rs:145:14
106+
--> $DIR/manual_memcpy.rs:144:14
107107
|
108108
LL | for i in 3..(3 + src.len()) {
109109
| ^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[3..((3 + src.len()))].clone_from_slice(&src[..((3 + src.len()) - 3)])`
110110

111111
error: it looks like you're manually copying between slices
112-
--> $DIR/manual_memcpy.rs:151:14
112+
--> $DIR/manual_memcpy.rs:150:14
113113
|
114114
LL | for i in 5..src.len() {
115115
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[5..src.len()].clone_from_slice(&src[(3 - 2)..((src.len() - 2) + 3 - 5)])`
116116

117117
error: it looks like you're manually copying between slices
118-
--> $DIR/manual_memcpy.rs:157:14
118+
--> $DIR/manual_memcpy.rs:156:14
119119
|
120120
LL | for i in 3..10 {
121121
| ^^^^^ help: try replacing the loop by: `dst[3..10].clone_from_slice(&src[5..(10 + 5 - 3)])`
122122

123123
error: it looks like you're manually copying between slices
124-
--> $DIR/manual_memcpy.rs:164:14
124+
--> $DIR/manual_memcpy.rs:163:14
125125
|
126126
LL | for i in 0..src.len() {
127127
| ^^^^^^^^^^^^
@@ -133,13 +133,13 @@ LL | dst2[30..(src.len() + 30)].clone_from_slice(&src[..]) {
133133
|
134134

135135
error: it looks like you're manually copying between slices
136-
--> $DIR/manual_memcpy.rs:174:14
136+
--> $DIR/manual_memcpy.rs:173:14
137137
|
138138
LL | for i in 0..1 << 1 {
139139
| ^^^^^^^^^ help: try replacing the loop by: `dst[(0 << 1)..((1 << 1) + (0 << 1))].clone_from_slice(&src[2..((1 << 1) + 2)])`
140140

141141
error: it looks like you're manually copying between slices
142-
--> $DIR/manual_memcpy.rs:182:14
142+
--> $DIR/manual_memcpy.rs:181:14
143143
|
144144
LL | for i in 0..src.len() {
145145
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`

0 commit comments

Comments
 (0)