Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

&&self suggestion from compiler #131084

Open
jpmelos opened this issue Oct 1, 2024 · 0 comments
Open

&&self suggestion from compiler #131084

jpmelos opened this issue Oct 1, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jpmelos
Copy link

jpmelos commented Oct 1, 2024

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

@jpmelos jpmelos added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant