forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#60155 - davidtwco:issue-59819, r=oli-obk
Suggest dereferencing when `Deref` is implemented. Fixes rust-lang#59819. r? @oli-obk cc @estebank
- Loading branch information
Showing
4 changed files
with
175 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
|
||
#![allow(warnings)] | ||
|
||
// Test that suggestion to add `*` characters applies to implementations of `Deref` as well as | ||
// references. | ||
|
||
struct Foo(i32); | ||
|
||
struct Bar(String); | ||
|
||
impl std::ops::Deref for Foo { | ||
type Target = i32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::ops::Deref for Bar { | ||
type Target = String; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = Foo(42); | ||
let y: i32 = *x; //~ ERROR mismatched types | ||
let a = &42; | ||
let b: i32 = *a; //~ ERROR mismatched types | ||
|
||
// Do not make a suggestion when adding a `*` wouldn't actually fix the issue: | ||
let f = Bar("bar".to_string()); | ||
let g: String = f.to_string(); //~ ERROR mismatched types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
|
||
#![allow(warnings)] | ||
|
||
// Test that suggestion to add `*` characters applies to implementations of `Deref` as well as | ||
// references. | ||
|
||
struct Foo(i32); | ||
|
||
struct Bar(String); | ||
|
||
impl std::ops::Deref for Foo { | ||
type Target = i32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::ops::Deref for Bar { | ||
type Target = String; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = Foo(42); | ||
let y: i32 = x; //~ ERROR mismatched types | ||
let a = &42; | ||
let b: i32 = a; //~ ERROR mismatched types | ||
|
||
// Do not make a suggestion when adding a `*` wouldn't actually fix the issue: | ||
let f = Bar("bar".to_string()); | ||
let g: String = f; //~ ERROR mismatched types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-59819.rs:28:18 | ||
| | ||
LL | let y: i32 = x; | ||
| ^ | ||
| | | ||
| expected i32, found struct `Foo` | ||
| help: consider dereferencing the type: `*x` | ||
| | ||
= note: expected type `i32` | ||
found type `Foo` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-59819.rs:30:18 | ||
| | ||
LL | let b: i32 = a; | ||
| ^ | ||
| | | ||
| expected i32, found &{integer} | ||
| help: consider dereferencing the borrow: `*a` | ||
| | ||
= note: expected type `i32` | ||
found type `&{integer}` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-59819.rs:34:21 | ||
| | ||
LL | let g: String = f; | ||
| ^ | ||
| | | ||
| expected struct `std::string::String`, found struct `Bar` | ||
| help: try using a conversion method: `f.to_string()` | ||
| | ||
= note: expected type `std::string::String` | ||
found type `Bar` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |