add a few more test cases

This commit is contained in:
y21 2023-06-19 00:36:16 +02:00
parent 26ac76c15f
commit b0dfecd8c1
3 changed files with 61 additions and 10 deletions

View file

@ -3,6 +3,19 @@
#![warn(clippy::type_id_on_box)]
use std::any::{Any, TypeId};
use std::ops::Deref;
type SomeBox = Box<dyn Any>;
struct BadBox(Box<dyn Any>);
impl Deref for BadBox {
type Target = Box<dyn Any>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn existential() -> impl Any {
Box::new(1) as Box<dyn Any>
@ -11,10 +24,17 @@ fn existential() -> impl Any {
fn main() {
let any_box: Box<dyn Any> = Box::new(0usize);
let _ = (*any_box).type_id();
let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
let _ = (*any_box).type_id();
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
let _ = (**any_box).type_id(); // 2 derefs are needed here
let _ = (**any_box).type_id(); // 2 derefs are needed here to get to the `dyn Any`
let b = existential();
let _ = b.type_id(); // don't lint
let _ = b.type_id(); // Don't lint.
let b: SomeBox = Box::new(0usize);
let _ = (*b).type_id();
let b = BadBox(Box::new(0usize));
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
}

View file

@ -3,6 +3,19 @@
#![warn(clippy::type_id_on_box)]
use std::any::{Any, TypeId};
use std::ops::Deref;
type SomeBox = Box<dyn Any>;
struct BadBox(Box<dyn Any>);
impl Deref for BadBox {
type Target = Box<dyn Any>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn existential() -> impl Any {
Box::new(1) as Box<dyn Any>
@ -11,10 +24,17 @@ fn existential() -> impl Any {
fn main() {
let any_box: Box<dyn Any> = Box::new(0usize);
let _ = any_box.type_id();
let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
let _ = (*any_box).type_id();
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
let _ = any_box.type_id(); // 2 derefs are needed here
let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
let b = existential();
let _ = b.type_id(); // don't lint
let _ = b.type_id(); // Don't lint.
let b: SomeBox = Box::new(0usize);
let _ = b.type_id();
let b = BadBox(Box::new(0usize));
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
}

View file

@ -1,5 +1,5 @@
error: calling `.type_id()` on a `Box<dyn Any>`
--> $DIR/type_id_on_box.rs:13:13
--> $DIR/type_id_on_box.rs:26:13
|
LL | let _ = any_box.type_id();
| -------^^^^^^^^^^
@ -11,9 +11,9 @@ LL | let _ = any_box.type_id();
= note: `-D clippy::type-id-on-box` implied by `-D warnings`
error: calling `.type_id()` on a `Box<dyn Any>`
--> $DIR/type_id_on_box.rs:17:13
--> $DIR/type_id_on_box.rs:30:13
|
LL | let _ = any_box.type_id(); // 2 derefs are needed here
LL | let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
| -------^^^^^^^^^^
| |
| help: consider dereferencing first: `(**any_box)`
@ -21,5 +21,16 @@ LL | let _ = any_box.type_id(); // 2 derefs are needed here
= 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: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
error: aborting due to 2 previous errors
error: calling `.type_id()` on a `Box<dyn Any>`
--> $DIR/type_id_on_box.rs:36: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: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
error: aborting due to 3 previous errors