Skip to content

branches_sharing_code incorrectly parsing for loop  #7628

@CeleritasCelery

Description

@CeleritasCelery

Lint name:
branches_sharing_code

I tried this code:

fn main() {
    let mut vec: Vec<usize> = Vec::new();

    if vec.is_empty() {
        vec.len();
    } else {
        vec.push(3);
        vec.len();
        for x in vec {
            println!("{:?}", x);
        }
    }
}

I expected to see this happen: The lint should not flag the code, since the branches do not share code at the start or end

Instead, this happened: The lint reports: "all if blocks contain the same code at the end"

help: consider moving the end statements out like this
   |
10 |     }
11 |     vec.len();
12 |     for x in vec
   |

Notice that it takes the first line of the for loop as part of the suggestion. It looks like clippy is not properly parsing the loop as code and so it is getting ignored. If I change the for loop to any other statement, I don't see this issue.

Meta

Rust version (rustc -Vv):

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions