Skip to content

Suggest remove deref for type mismatch #107203

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

Merged
merged 1 commit into from
Jan 23, 2023
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
16 changes: 16 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sugg_sp = receiver.span;
}
}

if let hir::ExprKind::Unary(hir::UnOp::Deref, ref inner) = expr.kind
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice to generalize this to multiple "&&" but whatever, this seems like a very common case already!

&& let Some(1) = self.deref_steps(expected, checked_ty) {
// We have `*&T`, check if what was expected was `&T`.
// If so, we may want to suggest removing a `*`.
sugg_sp = sugg_sp.with_hi(inner.span.lo());
return Some((
sugg_sp,
"consider removing deref here".to_string(),
"".to_string(),
Applicability::MachineApplicable,
true,
false,
));
}

if let Ok(src) = sm.span_to_snippet(sugg_sp) {
let needs_parens = match expr.kind {
// parenthesize if needed (Issue #46756)
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/generic-associated-types/issue-88360.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// run-rustfix

trait GatTrait {
type Gat<'a> where Self: 'a;

fn test(&self) -> Self::Gat<'_>;
}

trait SuperTrait<T>
where
Self: 'static,
for<'a> Self: GatTrait<Gat<'a> = &'a T>,
{
fn copy(&self) -> Self::Gat<'_> where T: Copy {
self.test()
//~^ mismatched types
}
}

fn main() {}
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/issue-88360.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// run-rustfix

trait GatTrait {
type Gat<'a> where Self: 'a;

Expand Down
12 changes: 7 additions & 5 deletions tests/ui/generic-associated-types/issue-88360.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
error[E0308]: mismatched types
--> $DIR/issue-88360.rs:13:9
--> $DIR/issue-88360.rs:15:9
|
LL | trait SuperTrait<T>
| - this type parameter
...
LL | fn copy(&self) -> Self::Gat<'_> where T: Copy {
| ------------- expected `&T` because of return type
LL | *self.test()
| ^^^^^^^^^^^^
| |
| expected `&T`, found type parameter `T`
| help: consider borrowing here: `&*self.test()`
| ^^^^^^^^^^^^ expected `&T`, found type parameter `T`
|
= note: expected reference `&T`
found type parameter `T`
help: consider removing deref here
|
LL - *self.test()
LL + self.test()
|

error: aborting due to previous error

Expand Down
28 changes: 28 additions & 0 deletions tests/ui/suggestions/suggest-remove-deref.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// run-rustfix

//issue #106496

struct S;

trait X {}
impl X for S {}

fn foo<T: X>(_: &T) {}
fn test_foo() {
let hello = &S;
foo(hello);
//~^ ERROR mismatched types
}

fn bar(_: &String) {}
fn test_bar() {
let v = String::from("hello");
let s = &v;
bar(s);
//~^ ERROR mismatched types
}

fn main() {
test_foo();
test_bar();
}
28 changes: 28 additions & 0 deletions tests/ui/suggestions/suggest-remove-deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// run-rustfix

//issue #106496

struct S;

trait X {}
impl X for S {}

fn foo<T: X>(_: &T) {}
fn test_foo() {
let hello = &S;
foo(*hello);
//~^ ERROR mismatched types
}

fn bar(_: &String) {}
fn test_bar() {
let v = String::from("hello");
let s = &v;
bar(*s);
//~^ ERROR mismatched types
}

fn main() {
test_foo();
test_bar();
}
43 changes: 43 additions & 0 deletions tests/ui/suggestions/suggest-remove-deref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0308]: mismatched types
--> $DIR/suggest-remove-deref.rs:13:9
|
LL | foo(*hello);
| --- ^^^^^^ expected reference, found struct `S`
| |
| arguments to this function are incorrect
|
= note: expected reference `&_`
found struct `S`
note: function defined here
--> $DIR/suggest-remove-deref.rs:10:4
|
LL | fn foo<T: X>(_: &T) {}
| ^^^ -----
help: consider removing deref here
|
LL - foo(*hello);
LL + foo(hello);
|

error[E0308]: mismatched types
--> $DIR/suggest-remove-deref.rs:21:9
|
LL | bar(*s);
| --- ^^ expected `&String`, found struct `String`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/suggest-remove-deref.rs:17:4
|
LL | fn bar(_: &String) {}
| ^^^ ----------
help: consider removing deref here
|
LL - bar(*s);
LL + bar(s);
|

error: aborting due to 2 previous errors

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