- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
struct S;
trait Trait {
    fn foo() {}
}
impl Trait for &S {}
impl Trait for &mut S {}
fn main() {
    let _ = S::foo();
}Current output
error[E0277]: the trait bound `S: Trait` is not satisfied
  --> $DIR/dont-suggest-borrowing-existing-borrow.rs:17:13
   |
LL |     let _ = S::foo();
   |             ^ the trait `Trait` is not implemented for `S`
   |
help: consider borrowing here
   |
LL |     let _ = &S::foo();
   |             +
LL |     let _ = &mut S::foo();
   |             ++++Desired output
error[E0277]: the trait bound `S: Trait` is not satisfied
  --> $DIR/dont-suggest-borrowing-existing-borrow.rs:17:13
   |
LL |     let _ = S::foo();
   |             ^ the trait `Trait` is not implemented for `S`
   |
help: consider borrowing here
   |
LL |     let _ = <&S>::foo();
   |             ++ +
LL |     let _ = <&mut S>::foo();
   |             +++++  +Rationale and extra context
No response
Other cases
Rust Version
1.90-devAnything else?
This happens because the suggestion code assumes that the obligation always comes from an expression.
Found while working on #132469
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.