Skip to content

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

Closed
@estebank

Description

@estebank

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions