diff options
| author | rail <12975677+rail-rain@users.noreply.github.com> | 2020-10-02 21:19:14 +1300 |
|---|---|---|
| committer | rail <12975677+rail-rain@users.noreply.github.com> | 2020-10-02 21:38:50 +1300 |
| commit | 41a0ccbc57902e488e62ed8ca9a1ebb565129c09 (patch) | |
| tree | e794bb810a8d69bf36248e5551457c2c2be08b01 | |
| parent | 1402d8ae4f0c07ac48bd8297ec07901346c32d32 (diff) | |
| download | rust-41a0ccbc57902e488e62ed8ca9a1ebb565129c09.tar.gz rust-41a0ccbc57902e488e62ed8ca9a1ebb565129c09.zip | |
add comments around `loop_counters`
| -rw-r--r-- | clippy_lints/src/loops.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index ea40c2af4f7..4f279cc5ef7 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1005,6 +1005,10 @@ fn get_assignment<'tcx>(e: &'tcx Expr<'tcx>) -> Option<(&'tcx Expr<'tcx>, &'tcx } } +/// Get assignments from the given block. +/// The returned iterator yields `None` if no assignment expressions are there, +/// filtering out the increments of the given whitelisted loop counters; +/// because its job is to make sure there's nothing other than assignments and the increments. fn get_assignments<'a: 'c, 'tcx: 'c, 'c>( cx: &'a LateContext<'tcx>, Block { stmts, expr, .. }: &'tcx Block<'tcx>, @@ -1021,7 +1025,8 @@ fn get_assignments<'a: 'c, 'tcx: 'c, 'c>( if let ExprKind::AssignOp(_, place, _) = e.kind { !loop_counters .iter() - // skip StartKind::Range + // skip the first item which should be `StartKind::Range` + // this makes it possible to use the slice with `StartKind::Range` in the same iterator loop. .skip(1) .any(|counter| same_var(cx, place, counter.id)) } else { @@ -1191,11 +1196,11 @@ fn detect_manual_memcpy<'tcx>( iter_b = Some(get_assignment(body)); } - // The only statements in the for loops can be indexed assignments from - // indexed retrievals. let assignments = iter_a.into_iter().flatten().chain(iter_b.into_iter()); let big_sugg = assignments + // The only statements in the for loops can be indexed assignments from + // indexed retrievals (except increments of loop counters). .map(|o| { o.and_then(|(lhs, rhs)| { let rhs = fetch_cloned_expr(rhs); |
