Skip to content

Conversation

@shreya-d2604
Copy link

@shreya-d2604 shreya-d2604 commented Dec 8, 2025

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):

  1. 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.

  2. manual_memcpy Bug Fix:The counter detection logic failed to properly handle counter variables that were used inside break statements, specifically labeled breaks (eg. break 'label;). This caused the main lint logic to sometimes miss the counter usage. This is now handled correctly.

  3. 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.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 8, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@Jarcho
Copy link
Contributor

Jarcho commented Dec 9, 2025

  1. 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.

This PR does the exact opposite. This case used to lint and now no longer does.

  1. manual_memcpy Bug Fix:The counter detection logic failed to properly handle counter variables that were used inside break statements, specifically labeled breaks (eg. break 'label;). This caused the main lint logic to sometimes miss the counter usage. This is now handled correctly.

Nothing in this PR touches how ExprKind::Break is handled, nor are there any tests for this.


If you're going to use an LLM you have to vet the output. It looks like it fixes #13572 (please link the issue as well), but it also breaks things in the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants