Skip to content

&&self suggestion from compiler #131084

Description

@jpmelos

Code

struct SomeStruct;

impl SomeStruct {
    fn some_fn(&some_name) {
        self
    }
}

fn main() {}

Current output

error[E0424]: expected value, found module `self`
 --> src/main.rs:5:9
  |
4 |     fn some_fn(&some_name) {
  |        ------- this function doesn't have a `self` parameter
5 |         self
  |         ^^^^ `self` value is a keyword only available in methods with a `self` parameter
  |
help: add a `self` receiver parameter to make the associated `fn` a method
  |
4 |     fn some_fn(&&self, some_name) {
  |                 ++++++

For more information about this error, try `rustc --explain E0424`.

Desired output

error[E0424]: expected value, found module `self`
 --> src/main.rs:5:9
  |
4 |     fn some_fn(&some_name) {
  |        ------- this function doesn't have a `self` parameter
5 |         self
  |         ^^^^ `self` value is a keyword only available in methods with a `self` parameter
  |
help: add a `self` receiver parameter to make the associated `fn` a method
  |
4 |     fn some_fn(self, &some_name) {
  |                +++++

For more information about this error, try `rustc --explain E0424`.

Rationale and extra context

Maybe the user forgot to add the self parameter in the code. Or maybe they forgot to rename some_name to self while refactoring the code. Either way, for beginners, the compiler suggesting to use &&self instead of self or &self may be confusing.

Rust Version

rustc 1.81.0 (eeb90cd 2024-09-04)
binary: rustc
commit-hash: eeb90cd
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTT-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