-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a lint for certain suspicious trait object usage
- Loading branch information
Showing
7 changed files
with
188 additions
and
0 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,34 @@ | ||
#![crate_type = "lib"] | ||
|
||
pub trait Foo {} | ||
|
||
pub trait Bar<T = dyn Foo> | ||
where | ||
T: ?Sized, | ||
{ | ||
} | ||
|
||
#[allow(invalid_type_param_default)] // not the lint we're interested in testing | ||
pub fn sus_fn<T = dyn Foo>() | ||
where | ||
T: ?Sized, | ||
{ | ||
} | ||
|
||
pub struct SusStruct<T = dyn Foo>(pub Box<T>) | ||
where | ||
T: ?Sized; | ||
|
||
pub enum SusEnum<T = dyn Foo> | ||
where | ||
T: ?Sized, | ||
{ | ||
Something(Box<T>), | ||
} | ||
|
||
pub union SusUnion<T = dyn Foo> | ||
where | ||
T: ?Sized, | ||
{ | ||
pub something: *const T, | ||
} |
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,54 @@ | ||
error: trait objects in generic defaults are forbidden | ||
--> $DIR/sus_trait_obj_items.rs:5:1 | ||
| | ||
LL | / pub trait Bar<T = dyn Foo> | ||
LL | | where | ||
LL | | T: ?Sized, | ||
LL | | { | ||
LL | | } | ||
| |_^ | ||
| | ||
= note: `-F plrust-suspicious-trait-object` implied by `-F plrust-lints` | ||
|
||
error: trait objects in generic defaults are forbidden | ||
--> $DIR/sus_trait_obj_items.rs:12:1 | ||
| | ||
LL | / pub fn sus_fn<T = dyn Foo>() | ||
LL | | where | ||
LL | | T: ?Sized, | ||
LL | | { | ||
LL | | } | ||
| |_^ | ||
|
||
error: trait objects in generic defaults are forbidden | ||
--> $DIR/sus_trait_obj_items.rs:18:1 | ||
| | ||
LL | / pub struct SusStruct<T = dyn Foo>(pub Box<T>) | ||
LL | | where | ||
LL | | T: ?Sized; | ||
| |______________^ | ||
|
||
error: trait objects in generic defaults are forbidden | ||
--> $DIR/sus_trait_obj_items.rs:22:1 | ||
| | ||
LL | / pub enum SusEnum<T = dyn Foo> | ||
LL | | where | ||
LL | | T: ?Sized, | ||
LL | | { | ||
LL | | Something(Box<T>), | ||
LL | | } | ||
| |_^ | ||
|
||
error: trait objects in generic defaults are forbidden | ||
--> $DIR/sus_trait_obj_items.rs:29:1 | ||
| | ||
LL | / pub union SusUnion<T = dyn Foo> | ||
LL | | where | ||
LL | | T: ?Sized, | ||
LL | | { | ||
LL | | pub something: *const T, | ||
LL | | } | ||
| |_^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
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,17 @@ | ||
#![crate_type = "lib"] | ||
|
||
trait Object<U> { | ||
type Output; | ||
} | ||
|
||
impl<T: ?Sized, U> Object<U> for T { | ||
type Output = U; | ||
} | ||
|
||
fn foo<T: ?Sized, U>(x: <T as Object<U>>::Output) -> U { | ||
x | ||
} | ||
|
||
pub fn transmute<T, U>(x: T) -> U { | ||
foo::<dyn Object<U, Output = T>, U>(x) | ||
} |
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,10 @@ | ||
error: trait objects in turbofish are forbidden | ||
--> $DIR/sus_trait_obj_transmute.rs:16:5 | ||
| | ||
LL | foo::<dyn Object<U, Output = T>, U>(x) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-F plrust-suspicious-trait-object` implied by `-F plrust-lints` | ||
|
||
error: aborting due to previous error | ||
|