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

diagnostics: consider parameter count when suggesting smart pointers #98509

Merged
merged 1 commit into from
Jun 26, 2022
Merged
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
5 changes: 4 additions & 1 deletion compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span,
rcvr_ty,
item_name,
args.map(|args| args.len()),
source,
out_of_scope_traits,
&unsatisfied_predicates,
Expand Down Expand Up @@ -1732,6 +1733,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
rcvr_ty: Ty<'tcx>,
item_name: Ident,
inputs_len: Option<usize>,
source: SelfSource<'tcx>,
valid_out_of_scope_traits: Vec<DefId>,
unsatisfied_predicates: &[(
Expand Down Expand Up @@ -1808,7 +1810,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Explicitly ignore the `Pin::as_ref()` method as `Pin` does not
// implement the `AsRef` trait.
let skip = skippable.contains(&did)
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name));
|| (("Pin::new" == *pre) && (sym::as_ref == item_name.name))
|| inputs_len.map_or(false, |inputs_len| pick.item.kind == ty::AssocKind::Fn && self.tcx.fn_sig(pick.item.def_id).skip_binder().inputs().len() != inputs_len);
// Make sure the method is defined for the *actual* receiver: we don't
// want to treat `Box<Self>` as a receiver if it only works because of
// an autoderef to `&self`
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/dont-suggest-pin-array-dot-set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://github.com/rust-lang/rust/issues/96834
//
// This test case verifies that rustc does not make an unhelpful suggestion:
//
// help: consider wrapping the receiver expression with the appropriate type
// |
// 14 | Pin::new(&mut a).set(0, 3);
// | +++++++++++++ +
//
// We can tell that it isn't helpful, because `Pin::set` takes two parameters (including
// the receiver), but the function call on line 14 supplies three.
fn main() {
let mut a = [0u8; 1];
a.set(0, 3); //~ERROR
}
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/dont-suggest-pin-array-dot-set.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0599]: no method named `set` found for array `[u8; 1]` in the current scope
--> $DIR/dont-suggest-pin-array-dot-set.rs:14:7
|
LL | a.set(0, 3);
| ^^^ help: there is an associated function with a similar name: `get`

error: aborting due to previous error

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