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

Invalid method call removal suggested when collecting &strs into Strings #133380

Open
cyrgani opened this issue Nov 23, 2024 · 0 comments
Open

Invalid method call removal suggested when collecting &strs into Strings #133380

cyrgani opened this issue Nov 23, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cyrgani
Copy link
Contributor

cyrgani commented Nov 23, 2024

Code

fn main() {
    String::new().lines().collect::<Vec<String>>();
}

Current output

error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `&str`
    --> src/main.rs:2:37
     |
2    |     String::new().lines().collect::<Vec<String>>();
     |                           -------   ^^^^^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&str>`
     |                           |
     |                           required by a bound introduced by this call
     |
     = help: the trait `FromIterator<&str>` is not implemented for `Vec<String>`
             but trait `FromIterator<String>` is implemented for it
     = help: for that trait implementation, expected `String`, found `&str`
note: the method call chain might not have had the expected associated types
    --> src/main.rs:2:19
     |
2    |     String::new().lines().collect::<Vec<String>>();
     |     ------------- ^^^^^^^ `Iterator::Item` is `&str` here
     |     |
     |     this expression has type `String`
note: required by a bound in `collect`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1967:19
     |
1967 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`
help: consider removing this method call, as the receiver has type `String` and `String: FromIterator<&str>` trivially holds
     |
2    -     String::new().lines().collect::<Vec<String>>();
2    +     String::new().collect::<Vec<String>>();
     |

Desired output

error[E0277]: a value of type `Vec<String>` cannot be built from an iterator over elements of type `&str`
    --> src/main.rs:2:37
     |
2    |     String::new().lines().collect::<Vec<String>>();
     |                           -------   ^^^^^^^^^^^ value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&str>`
     |                           |
     |                           required by a bound introduced by this call
     |
     = help: the trait `FromIterator<&str>` is not implemented for `Vec<String>`
             but trait `FromIterator<String>` is implemented for it
     = help: for that trait implementation, expected `String`, found `&str`
note: the method call chain might not have had the expected associated types
    --> src/main.rs:2:19
     |
2    |     String::new().lines().collect::<Vec<String>>();
     |     ------------- ^^^^^^^ `Iterator::Item` is `&str` here
     |     |
     |     this expression has type `String`
note: required by a bound in `collect`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1967:19
     |
1967 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`
help: consider changing this type to `&str`
     |
2    -     String::new().lines().collect::<Vec<String>>();
2    +     String::new().lines().collect::<Vec<&str>>();
     |
help: consider converting `&str` to `String` with `ToString::to_string`
``

Rationale and extra context

Current suggestion produces invalid code and changes the semantics of the program.

Other cases

Rust Version

1.84.0-nightly (2024-11-21 b19329a37cedf2027517)

Anything else?

No response

@cyrgani cyrgani added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant