Skip to content

manual_flatten doesn't follow its suggestion to remove if let statement leading to broken code #14692

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

Open
ulrichstark opened this issue Apr 25, 2025 · 7 comments · May be fixed by #14861
Open
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ulrichstark
Copy link

ulrichstark commented Apr 25, 2025

Summary

Clippy lint manual_flatten doesn't follow its own suggestion to remove if let statement leading to broken code.
Similar to #7514, but without references or slices.

Reproducer

I tried this code:

fn main() {
    let results: Vec<Result<String, ()>> = vec![];

    for result in results {
        if let Ok(value) = result {
            println!("{value}");
        }
    }
}

I expected to see this happen:

for result in results.into_iter().flatten() {
    println!("{result}");
}

Instead, this happened:

for result in results.into_iter().flatten() {
    if let Ok(value) = result { // broken because result is now of type String
        println!("{value}");
    }
}

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-pc-windows-msvc
release: 1.86.0
LLVM version: 19.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

@ulrichstark ulrichstark added the C-bug Category: Clippy is not doing the correct thing label Apr 25, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 25, 2025
@donkomura
Copy link
Contributor

@rustbot claim

@donkomura
Copy link
Contributor

donkomura commented May 14, 2025

Hi, @ulrichstark .
Could you tell me the clippy suggestions?
I tried running Clippy, and the suggestion can be compiled.

Rust Playground

@ulrichstark
Copy link
Author

I'm using rust-analyzer's quick fix "try: results.into_iter().flatten()" in VS Code.
It's only replacing results with results.into_iter().flatten(), but not removing the if.
Is this instead just an issue with rust-analyzer only applying the first suggestion of clippy?

Clippy suggestion:

warning: unnecessary `if let` since only the `Ok` variant of the iterator element is used
 --> src\main.rs:4:5
  |
4 |       for result in results {
  |       ^             ------- help: try: `results.into_iter().flatten()`
  |  _____|
  | |
5 | |         if let Ok(value) = result {
6 | |             println!("{value}");
7 | |         }
8 | |     }
  | |_____^
  |
help: ...and remove the `if let` statement in the for loop
 --> src\main.rs:5:9
  |
5 | /         if let Ok(value) = result {
6 | |             println!("{value}");
7 | |         }
  | |_________^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
  = note: `#[warn(clippy::manual_flatten)]` on by default

@donkomura
Copy link
Contributor

donkomura commented May 15, 2025

Thank you for the additional information!
I think this problem comes from clippy because if let part is the help message which does not fix the original code.

// If suggestion is not a one-liner, it won't be shown inline within the error message. In that
// case, it will be shown in the extra `help` message at the end, which is why the first
// `help_msg` needs to refer to the correct relative position of the suggestion.
let help_msg = if sugg.contains('\n') {
"remove the `if let` statement in the for loop and then..."
} else {
"...and remove the `if let` statement in the for loop"
};
span_lint_and_then(cx, MANUAL_FLATTEN, span, msg, |diag| {
diag.span_suggestion(arg.span, "try", sugg, applicability);
diag.span_help(inner_expr.span, help_msg);
});
}
}

@samueltardieu
Copy link
Contributor

If the suggestion is incomplete and cannot be completed, it should probably be MachineApplicable::HasPlaceholders.

@Alexendoo
Copy link
Member

While that would stop RA from suggesting it as a code action it's more of a workaround, it doesn't actually contain any placeholders

The core issue being that it emits a suggestion and a help message where it should be a single multipart suggestion

@samueltardieu
Copy link
Contributor

samueltardieu commented May 17, 2025

Of course, emitting a correct and complete suggestion would be even better (that's what I wrote above in "if the suggestion is incomplete and cannot be completed").

@donkomura donkomura linked a pull request May 21, 2025 that will close this issue
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
5 participants