Skip to content

from_iter_instead_of_collect help output invalid rust code #14581

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

Closed
A4-Tacks opened this issue Apr 10, 2025 · 0 comments · Fixed by #14586
Closed

from_iter_instead_of_collect help output invalid rust code #14581

A4-Tacks opened this issue Apr 10, 2025 · 0 comments · Fixed by #14586
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

@A4-Tacks
Copy link

Summary

clippy version: clippy 0.1.87 (e2014e876e 2025-04-01)

Invalid Turbofish of .collect()?

Reproducer

I tried this code:

#[warn(clippy::from_iter_instead_of_collect)]
fn main() {
    let nums = [0, 1, 2];
    let _ = &String::from_iter(
        nums.iter().map(|&num| char::from_u32(num).unwrap()),
    );
}

I expected to see this happen:

warning: usage of `FromIterator::from_iter`
 --> src/main.rs:4:14
  |
4 |       let _ = &String::from_iter(
  |  ______________^
5 | |         nums.iter().map(|&num| char::from_u32(num).unwrap()),
6 | |     );
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_iter_instead_of_collect
note: the lint level is defined here
 --> src/main.rs:1:8
  |
1 | #[warn(clippy::from_iter_instead_of_collect)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.collect()` instead of `::from_iter()`
  |
4 ~     let _ = &nums.iter().map(|&num| char::from_u32(num).unwrap()).collect::<String>();
  |

warning: `test_` (bin "test_") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

Instead, this happened:

warning: usage of `FromIterator::from_iter`
 --> src/main.rs:4:14
  |
4 |       let _ = &String::from_iter(
  |  ______________^
5 | |         nums.iter().map(|&num| char::from_u32(num).unwrap()),
6 | |     );
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_iter_instead_of_collect
note: the lint level is defined here
 --> src/main.rs:1:8
  |
1 | #[warn(clippy::from_iter_instead_of_collect)]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.collect()` instead of `::from_iter()`
  |
4 ~     let _ = &nums.iter().map(|&num| char::from_u32(num).unwrap()).collect::<String::from_iter(
5 ~         nums.iter().map(|&num| char<_>>();
  |

warning: `test_` (bin "test_") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

Version

rustc 1.88.0-nightly (e2014e876 2025-04-01)
binary: rustc
commit-hash: e2014e876e3efaa69bf51c19579adb16c3df5f81
commit-date: 2025-04-01
host: aarch64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.1

Additional Labels

No response

@A4-Tacks A4-Tacks added the C-bug Category: Clippy is not doing the correct thing label Apr 10, 2025
@samueltardieu samueltardieu added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 10, 2025
@samueltardieu samueltardieu self-assigned this Apr 10, 2025
github-merge-queue bot pushed a commit that referenced this issue Apr 17, 2025
Instead of looking for angle brackets in the source code, use the HIR
and Ty interfaces to either copy the original type, or complete it with
`_` placeholders if all type and const generic arguments are inferred.

Fixes #14581

changelog: [`from_iter_instead_of_collect`]: show correct type in
suggestion
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
Development

Successfully merging a pull request may close this issue.

2 participants