-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
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 (74ef0c3 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
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have