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

Compiler suggestion "try using a conversion method: .to_string" #105494

Closed
Deleplace opened this issue Dec 9, 2022 · 5 comments · Fixed by #105872
Closed

Compiler suggestion "try using a conversion method: .to_string" #105494

Deleplace opened this issue Dec 9, 2022 · 5 comments · Fixed by #105872
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Deleplace
Copy link

Deleplace commented Dec 9, 2022

I tried this code (playground):

fn main() {
    let mut path: String = "/usr".to_string();
    let folder: String = "lib".to_string();
    
    path = format!("{}/{}", path, folder).as_str();
    
    println!("{}", &path);
}

I expected to see this happen:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:12
  |
2 |     let mut path: String = "/usr".to_string();
  |                   ------ expected due to this type
...
5 |     path = format!("{}/{}", path, folder).as_str();
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try removing the call to method: `.as_str()`
  |            |
  |            expected struct `String`, found `&str`

Instead, this happened:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:12
  |
2 |     let mut path: String = "/usr".to_string();
  |                   ------ expected due to this type
...
5 |     path = format!("{}/{}", path, folder).as_str();
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
  |            |
  |            expected struct `String`, found `&str`

This is just a very specific instance where the suggestion to add some code, while "technically correct", is less useful than the better suggestion to remove some code.

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: aarch64-apple-darwin
release: 1.65.0
LLVM version: 15.0.0
Backtrace

<backtrace>

@Deleplace Deleplace added the C-bug Category: This is a bug. label Dec 9, 2022
@chenyukang
Copy link
Member

@rustbot claim

@jruderman
Copy link
Contributor

@rustbot label +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 12, 2022
@estebank
Copy link
Contributor

@chenyukang, it seems that given an expression, you can generically check if removing the last method call if present would give you the right type, so this could be implemented in a very generic way instead of special casing &str/String.

@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Dec 13, 2022
chenyukang added a commit to chenyukang/rust that referenced this issue Dec 23, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 24, 2022
…e-method-call, r=eholk

Suggest remove last method call when type coerce with expected  type

Fixes rust-lang#105494
@bors bors closed this as completed in 459b234 Dec 24, 2022
@Deleplace
Copy link
Author

This looks awesome, thank you so much!

@Deleplace
Copy link
Author

This is how it looks now in the nightly playground. Very cool.

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:5:12
  |
2 |     let mut path: String = "/usr".to_string();
  |                   ------ expected due to this type
...
5 |     path = format!("{}/{}", path, folder).as_str();
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `String`, found `&str`
  |
help: try removing the method call
  |
5 -     path = format!("{}/{}", path, folder).as_str();
5 +     path = format!("{}/{}", path, folder);
  |

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants