Auto merge of #7592 - lengyijun:redundant_allocation, r=camsteffen

redundant_allocation: fix 7487

Fixes #7487

changelog: [`redundant_allocation`] - allow `Box<Box<dyn T>>` which replaces wide pointers with thin pointers
This commit is contained in:
bors 2021-08-24 12:32:01 +00:00
commit 87c6f32756
3 changed files with 37 additions and 2 deletions

View file

@ -54,7 +54,13 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
_ => return false,
};
let inner_span = match get_qpath_generic_tys(inner_qpath).next() {
Some(ty) => ty.span,
Some(ty) => {
// Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
if matches!(ty.kind, TyKind::TraitObject(..)) {
return false;
}
ty.span
},
None => return false,
};
if inner_sym == outer_sym {

View file

@ -77,4 +77,24 @@ mod outer_arc {
}
}
// https://github.com/rust-lang/rust-clippy/issues/7487
mod box_dyn {
use std::boxed::Box;
use std::rc::Rc;
use std::sync::Arc;
pub trait T {}
struct S {
a: Box<Box<dyn T>>,
b: Rc<Box<dyn T>>,
c: Arc<Box<dyn T>>,
}
pub fn test_box(_: Box<Box<dyn T>>) {}
pub fn test_rc(_: Rc<Box<dyn T>>) {}
pub fn test_arc(_: Arc<Box<dyn T>>) {}
pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
}
fn main() {}

View file

@ -134,5 +134,14 @@ LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
= note: `Rc<SubT<T>>` is already on the heap, `Arc<Rc<SubT<T>>>` makes an extra allocation
= help: consider using just `Arc<SubT<T>>` or `Rc<SubT<T>>`
error: aborting due to 15 previous errors
error: usage of `Rc<Box<Box<dyn T>>>`
--> $DIR/redundant_allocation.rs:97:27
|
LL | pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
| ^^^^^^^^^^^^^^^^^^^
|
= note: `Box<Box<dyn T>>` is already on the heap, `Rc<Box<Box<dyn T>>>` makes an extra allocation
= help: consider using just `Rc<Box<dyn T>>` or `Box<Box<dyn T>>`
error: aborting due to 16 previous errors