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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
self.suggest_function_pointers(cause, span, &exp_found, diag);
self.suggest_push_for_append(cause, span, &exp_found, diag);
}
}

Expand Down
59 changes: 55 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,61 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}

pub(super) fn suggest_push_for_append(
&self,
cause: &ObligationCause<'tcx>,
span: Span,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
) {
let ty::error::ExpectedFound { expected: _, found } = exp_found;
let body = if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(cause.body_id) {
self.tcx.hir().body(body_id)
} else {
return;
};

struct MethodCallVisitor<'tcx> {
pub method: Option<Span>,
pub recv: Option<&'tcx hir::Expr<'tcx>>,
pub err_span: Span,
}

impl<'v> Visitor<'v> for MethodCallVisitor<'v> {
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
if let hir::ExprKind::MethodCall(method, recv, args, _) = ex.kind {
if args.len() == 1
&& args[0].span.eq(&self.err_span)
&& method.ident.name.as_str() == "append"
{
self.method = Some(method.ident.span);
self.recv = Some(recv);
}
}
walk_expr(self, ex);
}
}

let mut finder = MethodCallVisitor { method: None, err_span: span, recv: None };
finder.visit_body(body);

if let Some(typecheck) = self.typeck_results.as_ref()
&& let Some(method_call) = finder.method
&& let Some(vec_def_id) = self.tcx.get_diagnostic_item(sym::Vec)
&& 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?

{
diag.span_suggestion_verbose(
method_call,
"you might want to use `push` to add new element to `Vec`",
"push",
Applicability::MaybeIncorrect,
);
}
}

pub(super) fn suggest_function_pointers(
&self,
cause: &ObligationCause<'tcx>,
Expand Down Expand Up @@ -504,10 +559,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
walk_stmt(self, ex);
}

fn visit_body(&mut self, body: &'v hir::Body<'v>) {
hir::intravisit::walk_body(self, body);
}
}

let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/inference/issue-87212-suggest-push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
struct Thing;
fn t1() {
let mut stuff: Vec<Thing> = Vec::new();
// suggest push
stuff.append(Thing); //~ ERROR mismatched types
}

fn t2() {
let mut stuff = vec![];
// suggest push
stuff.append(Thing); //~ ERROR mismatched types
}

fn t3() {
let mut stuff: Vec<i32> = Vec::new();
// don't suggest push
stuff.append(Thing); //~ ERROR mismatched types
}

fn main() {}
50 changes: 50 additions & 0 deletions tests/ui/inference/issue-87212-suggest-push.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error[E0308]: mismatched types
--> $DIR/issue-87212-suggest-push.rs:5:18
|
LL | stuff.append(Thing);
| ------ ^^^^^ 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
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
help: you might want to use `push` to add new element to `Vec`
|
LL | stuff.push(Thing);
| ~~~~

error[E0308]: mismatched types
--> $DIR/issue-87212-suggest-push.rs:11:18
|
LL | stuff.append(Thing);
| ------ ^^^^^ expected `&mut Vec<_>`, found `Thing`
| |
| arguments to this method are incorrect
|
= note: expected mutable reference `&mut Vec<_>`
found struct `Thing`
note: method defined here
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
help: you might want to use `push` to add new element to `Vec`
|
LL | stuff.push(Thing);
| ~~~~

error[E0308]: mismatched types
--> $DIR/issue-87212-suggest-push.rs:17:18
|
LL | stuff.append(Thing);
| ------ ^^^^^ 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
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 3 previous errors

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