Skip to content

Commit b0dfecd

Browse files
committed
add a few more test cases
1 parent 26ac76c commit b0dfecd

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

tests/ui/type_id_on_box.fixed

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
#![warn(clippy::type_id_on_box)]
44

55
use std::any::{Any, TypeId};
6+
use std::ops::Deref;
7+
8+
type SomeBox = Box<dyn Any>;
9+
10+
struct BadBox(Box<dyn Any>);
11+
12+
impl Deref for BadBox {
13+
type Target = Box<dyn Any>;
14+
15+
fn deref(&self) -> &Self::Target {
16+
&self.0
17+
}
18+
}
619

720
fn existential() -> impl Any {
821
Box::new(1) as Box<dyn Any>
@@ -11,10 +24,17 @@ fn existential() -> impl Any {
1124
fn main() {
1225
let any_box: Box<dyn Any> = Box::new(0usize);
1326
let _ = (*any_box).type_id();
14-
let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
27+
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
1528
let _ = (*any_box).type_id();
1629
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
17-
let _ = (**any_box).type_id(); // 2 derefs are needed here
30+
let _ = (**any_box).type_id(); // 2 derefs are needed here to get to the `dyn Any`
31+
1832
let b = existential();
19-
let _ = b.type_id(); // don't lint
33+
let _ = b.type_id(); // Don't lint.
34+
35+
let b: SomeBox = Box::new(0usize);
36+
let _ = (*b).type_id();
37+
38+
let b = BadBox(Box::new(0usize));
39+
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
2040
}

tests/ui/type_id_on_box.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
#![warn(clippy::type_id_on_box)]
44

55
use std::any::{Any, TypeId};
6+
use std::ops::Deref;
7+
8+
type SomeBox = Box<dyn Any>;
9+
10+
struct BadBox(Box<dyn Any>);
11+
12+
impl Deref for BadBox {
13+
type Target = Box<dyn Any>;
14+
15+
fn deref(&self) -> &Self::Target {
16+
&self.0
17+
}
18+
}
619

720
fn existential() -> impl Any {
821
Box::new(1) as Box<dyn Any>
@@ -11,10 +24,17 @@ fn existential() -> impl Any {
1124
fn main() {
1225
let any_box: Box<dyn Any> = Box::new(0usize);
1326
let _ = any_box.type_id();
14-
let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
27+
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
1528
let _ = (*any_box).type_id();
1629
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
17-
let _ = any_box.type_id(); // 2 derefs are needed here
30+
let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
31+
1832
let b = existential();
19-
let _ = b.type_id(); // don't lint
33+
let _ = b.type_id(); // Don't lint.
34+
35+
let b: SomeBox = Box::new(0usize);
36+
let _ = b.type_id();
37+
38+
let b = BadBox(Box::new(0usize));
39+
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
2040
}

tests/ui/type_id_on_box.stderr

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: calling `.type_id()` on a `Box<dyn Any>`
2-
--> $DIR/type_id_on_box.rs:13:13
2+
--> $DIR/type_id_on_box.rs:26:13
33
|
44
LL | let _ = any_box.type_id();
55
| -------^^^^^^^^^^
@@ -11,15 +11,26 @@ LL | let _ = any_box.type_id();
1111
= note: `-D clippy::type-id-on-box` implied by `-D warnings`
1212

1313
error: calling `.type_id()` on a `Box<dyn Any>`
14-
--> $DIR/type_id_on_box.rs:17:13
14+
--> $DIR/type_id_on_box.rs:30:13
1515
|
16-
LL | let _ = any_box.type_id(); // 2 derefs are needed here
16+
LL | let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
1717
| -------^^^^^^^^^^
1818
| |
1919
| help: consider dereferencing first: `(**any_box)`
2020
|
2121
= 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
2222
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
2323

24-
error: aborting due to 2 previous errors
24+
error: calling `.type_id()` on a `Box<dyn Any>`
25+
--> $DIR/type_id_on_box.rs:36:13
26+
|
27+
LL | let _ = b.type_id();
28+
| -^^^^^^^^^^
29+
| |
30+
| help: consider dereferencing first: `(*b)`
31+
|
32+
= 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
33+
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
34+
35+
error: aborting due to 3 previous errors
2536

0 commit comments

Comments
 (0)