-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
E0277: suggest dereferencing function arguments in more cases
- Loading branch information
Showing
6 changed files
with
206 additions
and
113 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
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,37 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #90997. | ||
//! test that E0277 suggests dereferences to satisfy bounds when the referent is `Copy` or boxed. | ||
use std::ops::Deref; | ||
|
||
trait Test { | ||
fn test(self); | ||
} | ||
fn consume_test(x: impl Test) { x.test() } | ||
|
||
impl Test for u32 { | ||
fn test(self) {} | ||
} | ||
struct MyRef(u32); | ||
impl Deref for MyRef { | ||
type Target = u32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
struct NonCopy; | ||
impl Test for NonCopy { | ||
fn test(self) {} | ||
} | ||
|
||
fn main() { | ||
let my_ref = MyRef(0); | ||
consume_test(*my_ref); | ||
//~^ ERROR the trait bound `MyRef: Test` is not satisfied | ||
//~| SUGGESTION * | ||
|
||
let nested_box = Box::new(Box::new(Box::new(NonCopy))); | ||
consume_test(***nested_box); | ||
//~^ ERROR the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
//~| SUGGESTION *** | ||
} |
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,37 @@ | ||
//@ run-rustfix | ||
//! diagnostic test for #90997. | ||
//! test that E0277 suggests dereferences to satisfy bounds when the referent is `Copy` or boxed. | ||
use std::ops::Deref; | ||
|
||
trait Test { | ||
fn test(self); | ||
} | ||
fn consume_test(x: impl Test) { x.test() } | ||
|
||
impl Test for u32 { | ||
fn test(self) {} | ||
} | ||
struct MyRef(u32); | ||
impl Deref for MyRef { | ||
type Target = u32; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
struct NonCopy; | ||
impl Test for NonCopy { | ||
fn test(self) {} | ||
} | ||
|
||
fn main() { | ||
let my_ref = MyRef(0); | ||
consume_test(my_ref); | ||
//~^ ERROR the trait bound `MyRef: Test` is not satisfied | ||
//~| SUGGESTION * | ||
|
||
let nested_box = Box::new(Box::new(Box::new(NonCopy))); | ||
consume_test(nested_box); | ||
//~^ ERROR the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
//~| SUGGESTION *** | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/ui/traits/suggest-dereferences/deref-argument.stderr
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[E0277]: the trait bound `MyRef: Test` is not satisfied | ||
--> $DIR/deref-argument.rs:29:18 | ||
| | ||
LL | consume_test(my_ref); | ||
| ------------ ^^^^^^ the trait `Test` is not implemented for `MyRef` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `consume_test` | ||
--> $DIR/deref-argument.rs:9:25 | ||
| | ||
LL | fn consume_test(x: impl Test) { x.test() } | ||
| ^^^^ required by this bound in `consume_test` | ||
help: consider dereferencing here | ||
| | ||
LL | consume_test(*my_ref); | ||
| + | ||
|
||
error[E0277]: the trait bound `Box<Box<Box<NonCopy>>>: Test` is not satisfied | ||
--> $DIR/deref-argument.rs:34:18 | ||
| | ||
LL | consume_test(nested_box); | ||
| ------------ ^^^^^^^^^^ the trait `Test` is not implemented for `Box<Box<Box<NonCopy>>>` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
note: required by a bound in `consume_test` | ||
--> $DIR/deref-argument.rs:9:25 | ||
| | ||
LL | fn consume_test(x: impl Test) { x.test() } | ||
| ^^^^ required by this bound in `consume_test` | ||
help: consider dereferencing here | ||
| | ||
LL | consume_test(***nested_box); | ||
| +++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |