Skip to content

Commit f6c0063

Browse files
committed
split up tests into fixable and unfixable now and add annotations
1 parent 36e4c20 commit f6c0063

5 files changed

+99
-58
lines changed

tests/ui/type_id_on_box.fixed

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,35 @@ fn existential() -> impl Any {
2222
trait AnySubTrait: Any {}
2323
impl<T: Any> AnySubTrait for T {}
2424

25-
// `Any` is an indirect supertrait
26-
trait AnySubSubTrait: AnySubTrait {}
27-
impl<T: AnySubTrait> AnySubSubTrait for T {}
28-
29-
// This trait mentions `Any` in its predicates, but it is not a subtrait of `Any`.
30-
trait NormalTrait
31-
where
32-
i32: Any,
33-
{
34-
}
35-
impl<T> NormalTrait for T {}
36-
3725
fn main() {
26+
// Don't lint, calling `.type_id()` on a `&dyn Any` does the expected thing
27+
let ref_dyn: &dyn Any = &42;
28+
let _ = ref_dyn.type_id();
29+
3830
let any_box: Box<dyn Any> = Box::new(0usize);
3931
let _ = (*any_box).type_id();
40-
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
32+
//~^ ERROR: calling `.type_id()` on
33+
34+
// Don't lint. We explicitly say "do this instead" if this is intentional
35+
let _ = TypeId::of::<Box<dyn Any>>();
4136
let _ = (*any_box).type_id();
37+
38+
// 2 derefs are needed here to get to the `dyn Any`
4239
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
43-
let _ = (**any_box).type_id(); // 2 derefs are needed here to get to the `dyn Any`
40+
let _ = (**any_box).type_id();
41+
//~^ ERROR: calling `.type_id()` on
4442

4543
let b = existential();
46-
let _ = b.type_id(); // Don't lint.
44+
let _ = b.type_id(); // Don't
45+
46+
let b: Box<dyn AnySubTrait> = Box::new(1);
47+
let _ = (*b).type_id();
48+
//~^ ERROR: calling `.type_id()` on
4749

4850
let b: SomeBox = Box::new(0usize);
4951
let _ = (*b).type_id();
52+
//~^ ERROR: calling `.type_id()` on
5053

5154
let b = BadBox(Box::new(0usize));
5255
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
53-
54-
let b: Box<dyn AnySubTrait> = Box::new(1);
55-
let _ = (*b).type_id(); // Lint if calling `type_id` on a `dyn Trait` where `Trait: Any`
56-
57-
let b: Box<dyn AnySubSubTrait> = Box::new(1);
58-
let _ = b.type_id(); // Known FN - Any is not an "immediate" supertrait
59-
60-
let b: Box<dyn NormalTrait> = Box::new(1);
61-
let _ = b.type_id(); // `NormalTrait` does not have `Any` as its supertrait (even though it mentions it in `i32: Any`)
6256
}

tests/ui/type_id_on_box.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,35 @@ fn existential() -> impl Any {
2222
trait AnySubTrait: Any {}
2323
impl<T: Any> AnySubTrait for T {}
2424

25-
// `Any` is an indirect supertrait
26-
trait AnySubSubTrait: AnySubTrait {}
27-
impl<T: AnySubTrait> AnySubSubTrait for T {}
28-
29-
// This trait mentions `Any` in its predicates, but it is not a subtrait of `Any`.
30-
trait NormalTrait
31-
where
32-
i32: Any,
33-
{
34-
}
35-
impl<T> NormalTrait for T {}
36-
3725
fn main() {
26+
// Don't lint, calling `.type_id()` on a `&dyn Any` does the expected thing
27+
let ref_dyn: &dyn Any = &42;
28+
let _ = ref_dyn.type_id();
29+
3830
let any_box: Box<dyn Any> = Box::new(0usize);
3931
let _ = any_box.type_id();
40-
let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
32+
//~^ ERROR: calling `.type_id()` on
33+
34+
// Don't lint. We explicitly say "do this instead" if this is intentional
35+
let _ = TypeId::of::<Box<dyn Any>>();
4136
let _ = (*any_box).type_id();
37+
38+
// 2 derefs are needed here to get to the `dyn Any`
4239
let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
43-
let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
40+
let _ = any_box.type_id();
41+
//~^ ERROR: calling `.type_id()` on
4442

4543
let b = existential();
46-
let _ = b.type_id(); // Don't lint.
44+
let _ = b.type_id(); // Don't
45+
46+
let b: Box<dyn AnySubTrait> = Box::new(1);
47+
let _ = b.type_id();
48+
//~^ ERROR: calling `.type_id()` on
4749

4850
let b: SomeBox = Box::new(0usize);
4951
let _ = b.type_id();
52+
//~^ ERROR: calling `.type_id()` on
5053

5154
let b = BadBox(Box::new(0usize));
5255
let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
53-
54-
let b: Box<dyn AnySubTrait> = Box::new(1);
55-
let _ = b.type_id(); // Lint if calling `type_id` on a `dyn Trait` where `Trait: Any`
56-
57-
let b: Box<dyn AnySubSubTrait> = Box::new(1);
58-
let _ = b.type_id(); // Known FN - Any is not an "immediate" supertrait
59-
60-
let b: Box<dyn NormalTrait> = Box::new(1);
61-
let _ = b.type_id(); // `NormalTrait` does not have `Any` as its supertrait (even though it mentions it in `i32: Any`)
6256
}

tests/ui/type_id_on_box.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: calling `.type_id()` on `Box<dyn Any>`
2-
--> tests/ui/type_id_on_box.rs:39:13
2+
--> tests/ui/type_id_on_box.rs:31:13
33
|
44
LL | let _ = any_box.type_id();
55
| -------^^^^^^^^^^
@@ -12,37 +12,37 @@ LL | let _ = any_box.type_id();
1212
= help: to override `-D warnings` add `#[allow(clippy::type_id_on_box)]`
1313

1414
error: calling `.type_id()` on `Box<dyn Any>`
15-
--> tests/ui/type_id_on_box.rs:43:13
15+
--> tests/ui/type_id_on_box.rs:40:13
1616
|
17-
LL | let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
17+
LL | let _ = any_box.type_id();
1818
| -------^^^^^^^^^^
1919
| |
2020
| help: consider dereferencing first: `(**any_box)`
2121
|
2222
= 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
2323
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
2424

25-
error: calling `.type_id()` on `Box<dyn Any>`
26-
--> tests/ui/type_id_on_box.rs:49:13
25+
error: calling `.type_id()` on `Box<dyn AnySubTrait>`
26+
--> tests/ui/type_id_on_box.rs:47:13
2727
|
2828
LL | let _ = b.type_id();
2929
| -^^^^^^^^^^
3030
| |
3131
| help: consider dereferencing first: `(*b)`
3232
|
3333
= 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
34-
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
34+
= note: if this is intentional, use `TypeId::of::<Box<dyn AnySubTrait>>()` instead, which makes it more clear
3535

36-
error: calling `.type_id()` on `Box<dyn AnySubTrait>`
37-
--> tests/ui/type_id_on_box.rs:55:13
36+
error: calling `.type_id()` on `Box<dyn Any>`
37+
--> tests/ui/type_id_on_box.rs:51:13
3838
|
39-
LL | let _ = b.type_id(); // Lint if calling `type_id` on a `dyn Trait` where `Trait: Any`
39+
LL | let _ = b.type_id();
4040
| -^^^^^^^^^^
4141
| |
4242
| help: consider dereferencing first: `(*b)`
4343
|
4444
= 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
45-
= note: if this is intentional, use `TypeId::of::<Box<dyn AnySubTrait>>()` instead, which makes it more clear
45+
= note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
4646

4747
error: aborting due to 4 previous errors
4848

tests/ui/type_id_on_box_unfixable.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![warn(clippy::type_id_on_box)]
2+
3+
use std::any::{Any, TypeId};
4+
use std::ops::Deref;
5+
6+
trait AnySubTrait: Any {}
7+
impl<T: Any> AnySubTrait for T {}
8+
9+
// `Any` is an indirect supertrait
10+
trait AnySubSubTrait: AnySubTrait {}
11+
impl<T: AnySubTrait> AnySubSubTrait for T {}
12+
13+
// This trait mentions `Any` in its predicates, but it is not a subtrait of `Any`.
14+
trait NormalTrait
15+
where
16+
i32: Any,
17+
{
18+
}
19+
impl<T> NormalTrait for T {}
20+
21+
fn main() {
22+
// (currently we don't look deeper than one level into the supertrait hierachy, but we probably
23+
// could)
24+
let b: Box<dyn AnySubSubTrait> = Box::new(1);
25+
let _ = b.type_id();
26+
//~^ ERROR: calling `.type_id()` on
27+
28+
let b: Box<dyn NormalTrait> = Box::new(1);
29+
let _ = b.type_id();
30+
//~^ ERROR: calling `.type_id()` on
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: calling `.type_id()` on `Box<dyn AnySubSubTrait>`
2+
--> tests/ui/type_id_on_box_unfixable.rs:25:13
3+
|
4+
LL | let _ = b.type_id();
5+
| ^^^^^^^^^^^
6+
|
7+
= 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
8+
= note: if this is intentional, use `TypeId::of::<Box<dyn AnySubSubTrait>>()` instead, which makes it more clear
9+
= note: `-D clippy::type-id-on-box` implied by `-D warnings`
10+
= help: to override `-D warnings` add `#[allow(clippy::type_id_on_box)]`
11+
12+
error: calling `.type_id()` on `Box<dyn NormalTrait>`
13+
--> tests/ui/type_id_on_box_unfixable.rs:29:13
14+
|
15+
LL | let _ = b.type_id();
16+
| ^^^^^^^^^^^
17+
|
18+
= 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
19+
= note: if this is intentional, use `TypeId::of::<Box<dyn NormalTrait>>()` instead, which makes it more clear
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)