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

Detect AString.clone() when &str is needed and emit appropriate suggestion #61106

Closed
estebank opened this issue May 24, 2019 · 0 comments · Fixed by #61143
Closed

Detect AString.clone() when &str is needed and emit appropriate suggestion #61106

estebank opened this issue May 24, 2019 · 0 comments · Fixed by #61143
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@estebank
Copy link
Contributor

estebank commented May 24, 2019

On type mismatch with a suggestion to borrow in order to dereference a String into a &str, we should notice unnecessary .clone() calls:

error[E0308]: mismatched types
 --> src/main.rs:7:38
  |
7 |     assert_eq!(make_lipographic('e', passage.clone()), passage);
  |                                      ^^^^^^^^^^^^^^^
  |                                      |
  |                                      expected &str, found struct `std::string::String`
  |                                      help: consider borrowing here: `&passage.clone()`
  |
  = note: expected type `&str`
             found type `std::string::String`

and suggest their removal:

error[E0308]: mismatched types
 --> src/main.rs:7:38
  |
7 |     assert_eq!(make_lipographic('e', passage.clone()), passage);
  |                                      ^^^^^^^^^^^^^^^
  |                                      |
  |                                      expected &str, found struct `std::string::String`
  |                                      help: consider borrowing and avoiding the clone here: `&passage`
  |
  = note: expected type `&str`
             found type `std::string::String`

From https://thenewwazoo.github.io/clone.html

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels May 24, 2019
Lonami referenced this issue in thenewwazoo/thenewwazoo.github.io May 24, 2019
Centril added a commit to Centril/rust that referenced this issue Jun 14, 2019
When suggesting borrow, remove useless clones

Fix rust-lang#61106.
Centril added a commit to Centril/rust that referenced this issue Jun 14, 2019
When suggesting borrow, remove useless clones

Fix rust-lang#61106.
bors added a commit that referenced this issue Jun 15, 2019
When suggesting borrow, remove useless clones

Fix #61106.
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-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant