Skip to content

Add hint to correct Vec::append into Vec::push when the wrong method is used. #87212

Closed
@John2143

Description

@John2143

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a6f4c87ca812c201bfb57ef861c4d919

struct Thing;

fn main() {
    let mut stuff: Vec<Thing> = Vec::new();

    stuff.append(Thing);
}

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:6:18
  |
6 |     stuff.append(Thing);
  |                  ^^^^^ expected `&mut Vec<Thing>`, found struct `Thing`
  |
  = note: expected mutable reference `&mut Vec<Thing>`
                        found struct `Thing`

Ideally the output should look like:

error[E0308]: mismatched types
 --> src/main.rs:6:18
  |
6 |     stuff.append(Thing);
  |                  ^^^^^
  |                  |
  |                  expected `&mut Vec<Thing>`, found struct `Thing`
  |                  help: use `push` to add a single element
  |
  = note: expected mutable reference `&mut Vec<Thing>`
                        found struct `Thing`

There should be some kind of suggestion to use push when a value T is incorrectly append-ed onto a Vec<T>. This likely only trips up newcomers to rust, but I know a fair number of python users who had this as their first error message. There are likely some some other collections this could apply to, but Vec would definitely constitute most of these mistakes.

If nobody is immediately opposed to this change, I would like to try to implement this. I would need some help with where to make this change, but this seems like a fair introduction into editing compiler output.

It may also be helpful to just add a section mentioning push to the append function docs.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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