Skip to content

Any pitfalls #2988

Open
Open
@oli-obk

Description

@oli-obk

Since any value can be converted to an Any trait object, you can run into subtle bugs where you put the wrong value behind the Any:

use std::any::Any;

struct Foo;

fn main() {
    let x: Box<Foo> = Box::new(Foo);
    let mut y: Box<Any> = x;
    let z: &mut Any = &mut *y; // remove the `*` and the next line panics
    let a: &mut Foo = z.downcast_mut().unwrap(); // panics if `z` contains `Box<Any>` instead of `Any`
}

We should be linting all coercions of references/boxes to Any to a reference to Any. Especially around double references and similar this seems like it can go south very fast

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-correctnessLint: Belongs in the correctness lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions