Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any pitfalls #2988

Open
oli-obk opened this issue Aug 1, 2018 · 0 comments
Open

Any pitfalls #2988

oli-obk opened this issue Aug 1, 2018 · 0 comments
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Aug 1, 2018

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

@oli-obk oli-obk added A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group labels Aug 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant