Skip to content

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

Closed
@estebank

Description

@estebank
Contributor

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

Activity

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-diagnosticsArea: Messages for errors, warnings, and lints
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on May 24, 2019
added a commit that references this issue on May 24, 2019
added 2 commits that reference this issue on Jun 14, 2019

Rollup merge of rust-lang#61143 - estebank:issue-61106, r=eddyb

98b11b0

Rollup merge of rust-lang#61143 - estebank:issue-61106, r=eddyb

58a6259
added a commit that references this issue on Jun 15, 2019

Auto merge of #61143 - estebank:issue-61106, r=eddyb

9f06855
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @estebank

      Issue actions

        Detect `AString.clone()` when `&str` is needed and emit appropriate suggestion · Issue #61106 · rust-lang/rust