Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manual_flatten suggests uncompilable code on slice of references #6893

Closed
Cxarli opened this issue Mar 12, 2021 · 1 comment · Fixed by #6962
Closed

manual_flatten suggests uncompilable code on slice of references #6893

Cxarli opened this issue Mar 12, 2021 · 1 comment · Fixed by #6962
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Cxarli
Copy link

Cxarli commented Mar 12, 2021

Lint name: manual_flatten

For the following code:

let x = &[ &Some(1) ];
    
// warning: unnecessary `if let` since only the `Some` variant of the iterator element is used
for n in x {
    if let Some(n) = n {
        println!("{:?}", n);
    }
}

The lint suggests replacing the loop with:

for n in x.into_iter().flatten() {
    println!("{:?}", n);
}

However, this doesn't compile, because &&Option<{integer}> is not an iterator.

What does compile is

for n in x.into_iter().copied().flatten() {
    println!("{:?}", n);
}

Meta

$ cargo clippy -V
clippy 0.1.52 (a15f484 2021-02-22)

$ rustc -Vv
rustc 1.52.0-nightly (a15f484b9 2021-02-22)
binary: rustc
commit-hash: a15f484b918a4533ad633ea903ccce82910af342
commit-date: 2021-02-22
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1
@Cxarli Cxarli added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 12, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label Mar 15, 2021
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Mar 24, 2021

@rustbot claim

@bors bors closed this as completed in 917b538 Mar 24, 2021
dburgener added a commit to dburgener/cascade that referenced this issue Apr 7, 2023
1. IMHO this suggestion typically makes the code less readable.  I find
the interior if logic clearer than locating a flatten() in the middle of
a chained collection of iterator calls and recalling the semantics of
flatten() on an iterator.
2. The code suggestions provided by clippy to fix this issue do not
always seem to compile.  I got #159 to compile and make clippy happen,
and the flattening feels readable enough there, but the finally
compiling code does not match clippy's suggestion, which was broken.

(In #159 I appeared to be running into something essentially similar to
rust-lang/rust-clippy#6893, but according to
that issue, it was fixes a few years ago, so I'm not sure what's going
on.  I might dig into it more, but as mentioned in point 1, I'm
skeptical of the value of this check anyways)
dburgener added a commit to dburgener/cascade that referenced this issue Apr 10, 2023
1. IMHO this suggestion typically makes the code less readable.  I find
the interior if logic clearer than locating a flatten() in the middle of
a chained collection of iterator calls and recalling the semantics of
flatten() on an iterator.
2. The code suggestions provided by clippy to fix this issue do not
always seem to compile.  I got #159 to compile and make clippy happen,
and the flattening feels readable enough there, but the finally
compiling code does not match clippy's suggestion, which was broken.

(In #159 I appeared to be running into something essentially similar to
rust-lang/rust-clippy#6893, but according to
that issue, it was fixes a few years ago, so I'm not sure what's going
on.  I might dig into it more, but as mentioned in point 1, I'm
skeptical of the value of this check anyways)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants