rust-lang / rust-lang/rust-clippy

`Any` pitfalls

Open
#2,988 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint L-correctness
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Rust example and comparing the reference and box coercion cases described in the issue. Done means the lint identifies the risky conversions to Any, including double-reference cases, with its supported cases and expected diagnostics covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.