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

Add suggest Vec::push when the wrong method is used #121029

Closed
wants to merge 2 commits into from

Conversation

chenyukang
Copy link
Member

Fixes #87212

append is commonly used in Python, suggest push if possible.

@rustbot
Copy link
Collaborator

rustbot commented Feb 13, 2024

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 13, 2024
&& let Some(recv_ty) = finder.recv.and_then(|recv| typecheck.expr_ty_opt(recv))
&& let ty::Adt(def, _) = recv_ty.kind()
&& def.did() == vec_def_id
&& (recv_ty.contains(*found) || recv_ty.has_infer_types())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's better to check coerce between recv_ty and expected ty?

@oli-obk
Copy link
Contributor

oli-obk commented Feb 13, 2024

While this suggestion is really precise and targeted, would it be sufficient to use #[rustc_confusables(...)] on push?

cc #59450

@jieyouxu
Copy link
Member

While this suggestion is really precise and targeted, would it be sufficient to use #[rustc_confusables(...)] on push?

cc #59450

I think estebank is also adding/improving some infrastructure for this, cf. #120730?

@estebank
Copy link
Contributor

Indeed it will be better to leverage rustc_confusables for this, and #120730 expands its capabilities a bit to handle these cases:

error[E0308]: mismatched types
    --> f100.rs:5:18
     |
5    |     stuff.append(Thing); //~ ERROR mismatched types
     |           ------ ^^^^^ expected `&mut Vec<Thing>`, found `Thing`
     |           |
     |           arguments to this method are incorrect
     |
     = note: expected mutable reference `&mut Vec<Thing>`
                           found struct `Thing`
note: method defined here
    --> /home/gh-estebank/rust/library/alloc/src/vec/mod.rs:2025:12
     |
2025 |     pub fn append(&mut self, other: &mut Self) {
     |            ^^^^^^
help: you might have meant to use `push`
     |
5    |     stuff.push(Thing); //~ ERROR mismatched types
     |           ~~~~

error[E0308]: mismatched types
    --> f100.rs:11:18
     |
11   |     stuff.append(Thing); //~ ERROR mismatched types
     |           ------ ^^^^^ expected `&mut Vec<_>`, found `Thing`
     |           |
     |           arguments to this method are incorrect
     |
     = note: expected mutable reference `&mut Vec<_>`
                           found struct `Thing`
note: method defined here
    --> /home/gh-estebank/rust/library/alloc/src/vec/mod.rs:2025:12
     |
2025 |     pub fn append(&mut self, other: &mut Self) {
     |            ^^^^^^
help: you might have meant to use `push`
     |
11   |     stuff.push(Thing); //~ ERROR mismatched types
     |           ~~~~

error[E0308]: mismatched types
    --> f100.rs:17:18
     |
17   |     stuff.append(Thing); //~ ERROR mismatched types
     |           ------ ^^^^^ expected `&mut Vec<i32>`, found `Thing`
     |           |
     |           arguments to this method are incorrect
     |
     = note: expected mutable reference `&mut Vec<i32>`
                           found struct `Thing`
note: method defined here
    --> /home/gh-estebank/rust/library/alloc/src/vec/mod.rs:2025:12
     |
2025 |     pub fn append(&mut self, other: &mut Self) {
     |            ^^^^^^
note: you might have meant to use method `push`
    --> /home/gh-estebank/rust/library/alloc/src/vec/mod.rs:1920:5
     |
1920 |     pub fn push(&mut self, value: T) {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@chenyukang
Copy link
Member Author

emm, the issue was not linked with a PR 😂
closing it.

@chenyukang chenyukang closed this Feb 14, 2024
@estebank
Copy link
Contributor

@chenyukang yeah, it's a shame we have some overlapping tickets that are not linked to each other leading to situations like this one :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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