rust-lang / rust-lang/rust-clippy

redundant_allocation warns about Box<Arc<_>>, when it's necessary for downcasting

Open
#7,472 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: redundant_allocation

I tried this code:

use downcast::*;
use std::sync::Arc;

pub trait Foo: Any {}
downcast!(dyn Foo);

pub struct Bar{}

// Perhaps implement Foo requires that the struct be Clone, so it can be
// implemented on Arc<Bar> but not on Bar.
impl Foo for Arc<Bar>{}

pub fn foo() -> Vec<Box<dyn Foo + 'static>> {
    vec![Box::new(Arc::new(Bar{}))]
}

pub fn baz() -> Box<Arc<Bar>> {
    let mut v = foo();
    v.pop()
        .unwrap()
        .downcast::<Arc<Bar>>()
        .unwrap()
}

I expected to see this happen:

No errors reported by clippy. The double allocation should not be considered redundant in this case because it's necessary for downcasting. In my example, Foo is implmented for Arc<Bar>, not for Bar itself. That means that creating a trait object requires using Box<Arc<Bar>>. Clippy is smart enough not to warn about the line that creates the trait object. But it isn't smart enough not to warn about the line that downcasts the trait object to a concrete type.

Instead, this happened:

warning: usage of `Box<Arc<Bar>>`
  --> src/lib.rs:17:17
   |
17 | pub fn baz() -> Box<Arc<Bar>> {
   |                 ^^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::redundant_allocation)]` on by default
   = note: `Arc<Bar>` is already on the heap, `Box<Arc<Bar>>` makes an extra allocation
   = help: consider using just `Box<Bar>` or `Arc<Bar>`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
Meta
  • cargo clippy -V: clippy 0.1.55 (74ef0c3e 2021-07-16)
  • rustc -Vv:
 rustc 1.55.0-nightly (74ef0c3e4 2021-07-16)
binary: rustc
commit-hash: 74ef0c3e404cc72c08b2d1e14506f90d9e877269
commit-date: 2021-07-16
host: x86_64-unknown-freebsd
release: 1.55.0-nightly
LLVM version: 12.0.1

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

Reproduce the warning with cargo clippy using the Rust snippet and focus on the redundant_allocation lint. Trace how its downcast::<Arc>() case is handled, then add coverage so the necessary Box<Arc<_>> downcast is not warned about; done when this example is warning-free without suppressing the lint.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.