loops: Fix multi-part counter detection in explicit_counter_loop and … #16203
+249
−125
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
changelog: [explicit_counter_loop], [manual_memcpy]: Fixed two bugs related to tracking counter variables in loops, specifically when initialization is split or when labeled breaks are used.
Detailed Summary
Issue#13572 fix.
This PR fixes two instances where loop counter detection failed, based on improved logic in the shared helper module (clippy_lints/src/loops/utils.rs):
explicit_counter_loop Bug Fix:The lint previously failed to warn when the counter variable's initialization was spread over two statements:
rust
let mut index = 1;
index = 0; // The lint incorrectly missed this
for v in &vec { index += 1 }
This logic has been corrected, and the lint now fires as expected.
manual_memcpy Bug Fix:The counter detection logic failed to properly handle counter variables that were used inside
breakstatements, specifically labeled breaks (eg. break 'label;). This caused the main lint logic to sometimes miss the counter usage. This is now handled correctly.Tests:All UI tests and cargo test now pass. The explicit_counter_loop.rs test file was updated to reflect the new, correct behavior.
No new lints were added; this is a bug fix for existing lint logic.