Skip to content

manual_flatten suggests code that does not compile #7538

Closed
@xobs

Description

@xobs

Lint name:

manual_flatten

I tried this code:

for server in self.servers.iter_mut() {
    if let Some(server_inner) = server {
        if server_inner.pid == target_pid {
            *server = None;
        }
    }
}

I expected to see this happen: The lint should not have been emitted. Alternately, the suggestion should have resulted in code that compiled.

Instead, this happened: The Quick fix resulted in code that did not compile.

Meta

  • cargo clippy -V: clippy 0.1.54 (a178d032 2021-07-26)
  • rustc -Vv:
    rustc 1.54.0 (a178d0322 2021-07-26)
    binary: rustc
    commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
    commit-date: 2021-07-26
    host: x86_64-pc-windows-msvc
    release: 1.54.0
    LLVM version: 12.0.1
    

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Aug 6, 2021
dswij

dswij commented on Aug 9, 2021

@dswij
Member

Depending on what servers is, this might be related to #7514.

@xobs can you help clarify what servers is, or better provide a minimal example that we can run/test on?

xobs

xobs commented on Aug 9, 2021

@xobs
Author

Sure, here's a minimal example:

#[derive(Debug)]
struct Server {
    pid: u32,
}

fn main() {
    let target_pid = 1u32;
    let mut servers = [
        None,
        None,
        Some(Server { pid: 0u32 }),
        Some(Server { pid: 1u32 }),
    ];
    for server in servers.iter_mut() {
        if let Some(server_inner) = server {
            if server_inner.pid == target_pid {
                *server = None;
            }
        }
    }
    println!("Servers: {:x?}", servers);
}

Here's a link to the Playground, which also gives this recommendation: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=265c9480c7cbb542e9dd26aca6562c2b

dswij

dswij commented on Aug 14, 2021

@dswij
Member

Thanks for the minimal example!

This seems to be similar to #6784, I think we can close one in favor of the other.

added a commit that references this issue on Aug 16, 2021

Auto merge of #7566 - dswij:manual-flatten-use, r=xFrednet

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

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

      Participants

      @xobs@dswij

      Issue actions

        `manual_flatten` suggests code that does not compile · Issue #7538 · rust-lang/rust-clippy