RS.CLIPPY.TYPE_ID_ON_BOX

Calling `.type_id()` on a boxed trait object

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: type_id_on_box. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Looks for calls to .type_id() on a Box<dyn _>.

Why is this bad?

This almost certainly does not do what the user expects and can lead to subtle bugs. Calling .type_id() on a Box<dyn Trait> returns a fixed TypeId of the Box itself, rather than returning the TypeId of the underlying type behind the trait object.

For Box<dyn Any> specifically (and trait objects that have Any as its supertrait), this lint will provide a suggestion, which is to dereference the receiver explicitly to go from Box<dyn Any> to dyn Any. This makes sure that .type_id() resolves to a dynamic call on the trait object and not on the box.

If the fixed TypeId of the Box is the intended behavior, it's better to be explicit about it and write TypeId::of::<Box<dyn Trait>>(): this makes it clear that a fixed TypeId is returned and not the TypeId of the implementor.

Example

use std::any::{Any, TypeId};

let any_box: Box<dyn Any> = Box::new(42_i32);
assert_eq!(any_box.type_id(), TypeId::of::<i32>()); // ⚠ this fails!

Use instead:

use std::any::{Any, TypeId};

let any_box: Box<dyn Any> = Box::new(42_i32);
assert_eq!((*any_box).type_id(), TypeId::of::<i32>());
//          ^ dereference first, to call `type_id` on `dyn Any`