Skip to content

Misleading suggestion for builders #91525

Description

@terrarier2111

When trying to return Self instead of self from a method with a self or mut self parameter the suggestion is most likely misleading. I expected the compiler to tell me to change Self to self because in my eyes its more likely that the user wants to return the same instance that they took instead of allocating a new one but instead i get this:

error: the `Self` constructor can only be used with tuple or unit structs
  --> builders.rs:13:7
   |
13 |       Self
   |       ^^^^ help: use curly brackets: `Self { /* fields */ }`

error: aborting due to previous error

here is a reproducer:

fn main() {
}

struct Builder {
   setting: bool,
}

impl Builder {
   fn setting(mut self)	-> Self	{
      self.setting = true;
      Self
   }
}

here is the expected output:

error: `Self` is a constructor and thus can't be returned itself
  --> builders.rs:13:7
   |
13 |       Self
   |       ^^^^ help: try replacing `Self` with `self`

error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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