Skip to content

when a fn is missing a parameter type, the help advice could be improved #144968

@ericseppanen

Description

@ericseppanen

Code

fn is_even(value) -> bool {
    value % 2 == 0
}

Current output

error: expected one of `:`, `@`, or `|`, found `)`
 --> src/lib.rs:1:17
  |
1 | fn is_even(value) -> bool {
  |                 ^ expected one of `:`, `@`, or `|`
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a `self` type, give it a parameter name
  |
1 | fn is_even(self: value) -> bool {
  |            +++++
help: if this is a parameter name, give it a type
  |
1 | fn is_even(value: TypeName) -> bool {
  |                 ++++++++++
help: if this is a type, explicitly ignore the parameter name
  |
1 | fn is_even(_: value) -> bool {
  |            ++

Desired output

error: expected one of `:`, `@`, or `|`, found `)`
 --> src/lib.rs:1:17
  |
1 | fn is_even(value) -> bool {
  |                 ^ expected one of `:`, `@`, or `|`
  |
help: if this is a parameter name, give it a type
  |
1 | fn is_even(value: TypeName) -> bool {
  |                 ++++++++++
help: if this is a type, explicitly ignore the parameter name
  |
1 | fn is_even(_: value) -> bool {
  |            ++

Rationale and extra context

While teaching Rust to a 14-year-old, this was one of the first errors we encountered.

The first thing I notice is that the first help advice is kind of inappropriate: this is not a member function so there is no self. Additionally, if this error is expected among beginners (e.g. those coming from Python), it seems unwise to guide them to using (unstable) arbitrary self types.

I think the first help suggestion could probably be deleted, promoting if this is a parameter name, give it a type into the first spot.

Additionally, the note anonymous parameters are removed in the 2018 edition (see RFC 1685) seems like a distraction. 2018 was a long time ago, and I don't think people should be nagged about this any longer. It especially should not appear in an error message that beginners are likely to encounter on their first day writing Rust code.

Rust Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

Anything else?

Thanks @djc @estebank for the nudge to file this issue.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions