-
- Notifications
You must be signed in to change notification settings - Fork 14.2k
Open
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.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.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
fn foo(_f: impl Fn() -> &mut ()) {}Current output
error[E0106]: missing lifetime specifier --> bad.rs:1:25 | 1 | fn foo(_f: impl Fn() -> &mut ()) {} | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static` | 1 | fn foo(_f: impl Fn() -> &'static mut ()) {} | +++++++ help: instead, you are more likely to want to return an owned value | 1 - fn foo(_f: impl Fn() -> &mut ()) {} 1 + fn foo(_f: impl Fn() -> mut ()) {} |Desired output
error[E0106]: missing lifetime specifier --> bad.rs:1:25 | 1 | fn foo(_f: impl Fn() -> &mut ()) {} | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static` | 1 | fn foo(_f: impl Fn() -> &'static mut ()) {} | +++++++ help: instead, you are more likely to want to return an owned value | 1 - fn foo(_f: impl Fn() -> &mut ()) {} 1 + fn foo(_f: impl Fn() -> ()) {} |Rationale and extra context
In type annotations for closure return types, the help diagnostic suggests returning an owned value instead of a reference. However, it suggests the invalid syntax mut T, only removing the & instead of the full &mut. It should most likely suggest changing &mut T into T instead.
Other cases
Rust Version
rustc 1.92.0 (ded5c06cf 2025-12-08) binary: rustc commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234 commit-date: 2025-12-08 host: aarch64-pc-windows-msvc release: 1.92.0 LLVM version: 21.1.3Anything else?
No response
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.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.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.