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

"consider borrowing here" suggestion is missing parentheses #104961

Closed
jyn514 opened this issue Nov 26, 2022 · 4 comments · Fixed by #105019
Closed

"consider borrowing here" suggestion is missing parentheses #104961

jyn514 opened this issue Nov 26, 2022 · 4 comments · Fixed by #105019
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Nov 26, 2022

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"))
  |                   ++
@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints 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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Nov 26, 2022
@chenyukang
Copy link
Member

@rustbot claim

@chenyukang chenyukang removed their assignment Nov 27, 2022
@chenyukang
Copy link
Member

chenyukang commented Nov 27, 2022

@compiler-errors
The code here:

Seems we can only get more context from obligation, with only span we can not know whether the current code snippet is an ident or a more complicated expression.

The current snippet is "hi".to_string() + " you".

An completed solution should make sure we get something like expr in obligation?
A hack I'm thinking is checking whether snippet contains spaces, if so we suggest it like:

2 |     x.starts_with(&("hi".to_string() + " you"))
  |                   ++

@chenyukang
Copy link
Member

I guess we have more similar issues for other suggestions come from obligation.

@jyn514 jyn514 changed the title "consider parenthesizing expression" suggestion is missing parentheses "consider borrowing here" suggestion is missing parentheses Nov 27, 2022
@chenyukang
Copy link
Member

This is an similar case, but we handle it well:

fn bar(val: &str) {
    todo!()
}

fn main() {
    bar("hello".to_string() + "world");
}

Output is:

error[E0308]: mismatched types
 --> ./p/n.rs:7:9
  |
7 |     bar("hello".to_string() + "world");
  |     --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |     |   |
  |     |   expected `&str`, found struct `String`
  |     |   help: consider borrowing here: `&("hello".to_string() + "world")`
  |     arguments to this function are incorrect
  |
note: function defined here
 --> ./p/n.rs:2:4
  |
2 | fn bar(val: &str) {
  |    ^^^ ---------

error: aborting due to previous error

The codepath is different, we take care parenthesis here, with expr:

let needs_parens = match expr.kind {

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 11, 2023
…w, r=cjgillot

Add parentheses properly for borrowing suggestion

Fixes rust-lang#104961
@bors bors closed this as completed in 8f736a4 Feb 11, 2023
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` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

2 participants