Skip to content

False positive explicit_counter_loop when the counter is used in a closure after the loop #4732

@madadam

Description

@madadam

The explicit_counter_loop lint is incorrectly triggered in this case:

let vec = vec![1,2,3]; let mut index = 0; for _v in &vec { index += 1 } run(|| { println!("index: {}", index); })

This is incorrect because the counter is used after the loop. Note that if the counter were used directly and not in a closure, the lint would not get triggered.

Rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3f1f1216aa7a4344d6891fc9e4ccdd5c

> cargo clippy -V clippy 0.0.212 (3aea860 2019-09-03) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions