Skip to content

"consider borrowing here" suggestion is missing parentheses #104961

Closed
@jyn514

Description

@jyn514

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c03c22d0ff9f0d88bb0cacb6edb95501

fn foo(x: &str) -> bool {
    x.starts_with("hi".to_string() + " you")
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): expected a `FnMut<(char,)>` closure, found `String`
 --> src/lib.rs:2:19
  |
2 |     x.starts_with("hi".to_string() + " you")
  |       ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Pattern<'_>` is not implemented for `String`
  |       |
  |       required by a bound introduced by this call
  |
  = note: the trait bound `String: Pattern<'_>` is not satisfied
  = note: required for `String` to implement `Pattern<'_>`
note: required by a bound in `core::str::<impl str>::starts_with`
help: consider borrowing here
  |
2 |     x.starts_with(&"hi".to_string() + " you")
  |                   +

Ideally the output should look like:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): expected a `FnMut<(char,)>` closure, found `String`
 --> src/lib.rs:2:19
  |
2 |     x.starts_with("hi".to_string() + " you")
  |       ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Pattern<'_>` is not implemented for `String`
  |       |
  |       required by a bound introduced by this call
  |
  = note: the trait bound `String: Pattern<'_>` is not satisfied
  = note: required for `String` to implement `Pattern<'_>`
note: required by a bound in `core::str::<impl str>::starts_with`
help: consider borrowing here
  |
2 |     x.starts_with(&("hi".to_string() + " you"))
  |                   ++

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`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions