Improvable error when putting `mut` in front of argument in trait method
Open
@mostafarezaei is already working on this.
Since Jan 6, 2025.
A-diagnostics
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
trait Foo {
fn foo(mut a: String);
}
Current output
error: patterns aren't allowed in functions without bodies
--> src/lib.rs:3:12
|
3 | fn foo(mut a: String);
| ^^^^^ help: remove `mut` from the parameter: `a`
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #35203 <https://github.com/rust-lang/rust/issues/35203>
= note: `#[deny(patterns_in_fns_without_body)]` on by default
Desired output
error: patterns aren't allowed in functions without bodies
--> src/lib.rs:3:12
|
3 | fn foo(mut a: String);
| ^^^^^ help: remove `mut` from the parameter: `a`
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: mutability of arguments cannot be declared in trait functions, as they are not part of the actual function signature.
= note: for more information, see issue #35203 <https://github.com/rust-lang/rust/issues/35203>
= note: `#[deny(patterns_in_fns_without_body)]` on by default
Rationale and extra context
Adding mut in front of an argument makes an owned value be mutable inside a function. This is a form of a pattern, so the current error message is not wrong. However, most people, especially beginners, don't intuitively understand this as pattern syntax, and might not understand that it does not at all affect the effective function signature.
Thus, adding a specific hint or maybe even a completely different, more specific error message, for this case could help avoid confusion, and might serve as a learning opportunity to beginners.
Other cases
Rust Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.