Skip to content

Commit 1402d8a

Browse files
committed
fix a FN where incr exprs with no semicolon at ends
1 parent 94d7b82 commit 1402d8a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

clippy_lints/src/loops.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,14 +1014,20 @@ fn get_assignments<'a: 'c, 'tcx: 'c, 'c>(
10141014
.iter()
10151015
.filter_map(move |stmt| match stmt.kind {
10161016
StmtKind::Local(..) | StmtKind::Item(..) => None,
1017-
StmtKind::Expr(e) | StmtKind::Semi(e) => if_chain! {
1018-
if let ExprKind::AssignOp(_, var, _) = e.kind;
1019-
// skip StartKind::Range
1020-
if loop_counters.iter().skip(1).any(|counter| Some(counter.id) == var_def_id(cx, var));
1021-
then { None } else { Some(e) }
1022-
},
1017+
StmtKind::Expr(e) | StmtKind::Semi(e) => Some(e),
10231018
})
10241019
.chain((*expr).into_iter())
1020+
.filter(move |e| {
1021+
if let ExprKind::AssignOp(_, place, _) = e.kind {
1022+
!loop_counters
1023+
.iter()
1024+
// skip StartKind::Range
1025+
.skip(1)
1026+
.any(|counter| same_var(cx, place, counter.id))
1027+
} else {
1028+
true
1029+
}
1030+
})
10251031
.map(get_assignment)
10261032
}
10271033

tests/ui/manual_memcpy/with_loop_counters.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32])
5959
dst[count] = src[i + 2];
6060
count += 1;
6161
}
62+
63+
// make sure incrementing expressions without semicolons at the end of loops are handled correctly.
64+
let mut count = 0;
65+
for i in 3..src.len() {
66+
dst[i] = src[count];
67+
count += 1
68+
}
6269
}
6370

6471
fn main() {}

tests/ui/manual_memcpy/with_loop_counters.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,14 @@ LL | | count += 1;
8989
LL | | }
9090
| |_____^ help: try replacing the loop by: `dst[(0 << 1)..((1 << 1) + (0 << 1))].clone_from_slice(&src[2..((1 << 1) + 2)]);`
9191

92-
error: aborting due to 9 previous errors
92+
error: it looks like you're manually copying between slices
93+
--> $DIR/with_loop_counters.rs:65:5
94+
|
95+
LL | / for i in 3..src.len() {
96+
LL | | dst[i] = src[count];
97+
LL | | count += 1
98+
LL | | }
99+
| |_____^ help: try replacing the loop by: `dst[3..src.len()].clone_from_slice(&src[..(src.len() - 3)]);`
100+
101+
error: aborting due to 10 previous errors
93102

0 commit comments

Comments
 (0)