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 removing the borrow" suggests to only remove & and not mut #85933

Closed
m-ou-se opened this issue Jun 2, 2021 · 1 comment · Fixed by #85939
Closed

"consider removing the borrow" suggests to only remove & and not mut #85933

m-ou-se opened this issue Jun 2, 2021 · 1 comment · Fixed by #85939
Assignees
Labels
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

@m-ou-se
Copy link
Member

m-ou-se commented Jun 2, 2021

In the following snippet, rustc suggests to remove just the & instead of removing the &mut, resulting in an invalid suggestion: mut format!("").

fn main() {
    let _: String = &mut format!("");
}
error[E0308]: mismatched types
 --> src/main.rs:2:21
  |
2 |     let _: String = &mut format!("");
  |            ------   ^^^^^^^^^^^^^^^^
  |            |        |
  |            |        expected struct `String`, found `&mut String`
  |            |        help: consider removing the borrow: `mut format!("")`
  |            expected due to this
@m-ou-se m-ou-se added 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 Jun 2, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Jun 2, 2021

The issue lies in the strip_prefix('&') here:

if sm.is_imported(expr.span) {
if let Ok(src) = sm.span_to_snippet(sp) {
if let Some(src) = src.strip_prefix('&') {
return Some((
sp,
"consider removing the borrow",

Not sure how to easily fix it though, because & and mut are separate tokens (and unlike & is usually followed by whitespace), so strip_prefix("&mut") won't do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

1 participant