-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[type_id_on_box
]: lint on any Box<dyn _>
#11350
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,37 +1,48 @@ | ||
error: calling `.type_id()` on a `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:24:13 | ||
error: calling `.type_id()` on `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:31:13 | ||
| | ||
LL | let _ = any_box.type_id(); | ||
| -------^^^^^^^^^^ | ||
| | | ||
| help: consider dereferencing first: `(*any_box)` | ||
| | ||
= note: this returns the type id of the literal type `Box<dyn Any>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear | ||
= note: `-D clippy::type-id-on-box` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::type_id_on_box)]` | ||
|
||
error: calling `.type_id()` on a `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:28:13 | ||
error: calling `.type_id()` on `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:40:13 | ||
| | ||
LL | let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any` | ||
LL | let _ = any_box.type_id(); | ||
| -------^^^^^^^^^^ | ||
| | | ||
| help: consider dereferencing first: `(**any_box)` | ||
| | ||
= note: this returns the type id of the literal type `Box<dyn Any>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear | ||
|
||
error: calling `.type_id()` on a `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:34:13 | ||
error: calling `.type_id()` on `Box<dyn AnySubTrait>` | ||
--> tests/ui/type_id_on_box.rs:47:13 | ||
| | ||
LL | let _ = b.type_id(); | ||
| -^^^^^^^^^^ | ||
| | | ||
| help: consider dereferencing first: `(*b)` | ||
| | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn AnySubTrait>>()` instead, which makes it more clear | ||
|
||
error: calling `.type_id()` on `Box<dyn Any>` | ||
--> tests/ui/type_id_on_box.rs:51:13 | ||
| | ||
LL | let _ = b.type_id(); | ||
| -^^^^^^^^^^ | ||
| | | ||
| help: consider dereferencing first: `(*b)` | ||
| | ||
= note: this returns the type id of the literal type `Box<dyn Any>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear | ||
|
||
error: aborting due to 3 previous errors | ||
error: aborting due to 4 previous errors | ||
|
This file contains hidden or 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,31 @@ | ||
#![warn(clippy::type_id_on_box)] | ||
|
||
use std::any::{Any, TypeId}; | ||
use std::ops::Deref; | ||
|
||
trait AnySubTrait: Any {} | ||
impl<T: Any> AnySubTrait for T {} | ||
|
||
// `Any` is an indirect supertrait | ||
trait AnySubSubTrait: AnySubTrait {} | ||
impl<T: AnySubTrait> AnySubSubTrait for T {} | ||
|
||
// This trait mentions `Any` in its predicates, but it is not a subtrait of `Any`. | ||
trait NormalTrait | ||
where | ||
i32: Any, | ||
{ | ||
} | ||
impl<T> NormalTrait for T {} | ||
|
||
fn main() { | ||
// (currently we don't look deeper than one level into the supertrait hierachy, but we probably | ||
// could) | ||
let b: Box<dyn AnySubSubTrait> = Box::new(1); | ||
let _ = b.type_id(); | ||
//~^ ERROR: calling `.type_id()` on | ||
|
||
let b: Box<dyn NormalTrait> = Box::new(1); | ||
let _ = b.type_id(); | ||
//~^ ERROR: calling `.type_id()` on | ||
} |
This file contains hidden or 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,22 @@ | ||
error: calling `.type_id()` on `Box<dyn AnySubSubTrait>` | ||
--> tests/ui/type_id_on_box_unfixable.rs:25:13 | ||
| | ||
LL | let _ = b.type_id(); | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn AnySubSubTrait>>()` instead, which makes it more clear | ||
= note: `-D clippy::type-id-on-box` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::type_id_on_box)]` | ||
|
||
error: calling `.type_id()` on `Box<dyn NormalTrait>` | ||
--> tests/ui/type_id_on_box_unfixable.rs:29:13 | ||
| | ||
LL | let _ = b.type_id(); | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: this returns the type id of the literal type `Box<_>` instead of the type id of the boxed value, which is most likely not what you want | ||
= note: if this is intentional, use `TypeId::of::<Box<dyn NormalTrait>>()` instead, which makes it more clear | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.