# Static rustc_lint::builtin::PTR_CAST_ADD_AUTO_TO_OBJECT

source ·

    pub static PTR_CAST_ADD_AUTO_TO_OBJECT: &'static Lint

Expand description

The `ptr_cast_add_auto_to_object` lint detects casts of raw pointers to trait objects, which add auto traits.

#### §Example

ⓘ

    let ptr: *const dyn core::any::Any = &();
    _ = ptr as *const dyn core::any::Any + Send;

{{produces}}

#### §Explanation

Adding an auto trait can make the vtable invalid, potentially causing UB in safe code afterwards. For example:

ⓘ

    #![feature(arbitrary_self_types)]

    trait Trait {
        fn f(self: *const Self)
        where
            Self: Send;
    }

    impl Trait for *const () {
        fn f(self: *const Self) {
            unreachable!()
        }
    }

    fn main() {
        let unsend: *const () = &();
        let unsend: *const dyn Trait = &unsend;
        let send_bad: *const (dyn Trait + Send) = unsend as _;
        send_bad.f(); // this crashes, since vtable for `*const ()` does not have an entry for `f`
    }

Generally you must ensure that vtable is right for the pointer’s type, before passing the pointer to safe code.