Skip to content

Commit 87c6f32

Browse files
committed
Auto merge of rust-lang#7592 - lengyijun:redundant_allocation, r=camsteffen
redundant_allocation: fix 7487 Fixes rust-lang#7487 changelog: [`redundant_allocation`] - allow `Box<Box<dyn T>>` which replaces wide pointers with thin pointers
2 parents 15b1c6f + 641be55 commit 87c6f32

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5454
_ => return false,
5555
};
5656
let inner_span = match get_qpath_generic_tys(inner_qpath).next() {
57-
Some(ty) => ty.span,
57+
Some(ty) => {
58+
// Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
59+
if matches!(ty.kind, TyKind::TraitObject(..)) {
60+
return false;
61+
}
62+
ty.span
63+
},
5864
None => return false,
5965
};
6066
if inner_sym == outer_sym {

tests/ui/redundant_allocation.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,24 @@ mod outer_arc {
7777
}
7878
}
7979

80+
// https://github.com/rust-lang/rust-clippy/issues/7487
81+
mod box_dyn {
82+
use std::boxed::Box;
83+
use std::rc::Rc;
84+
use std::sync::Arc;
85+
86+
pub trait T {}
87+
88+
struct S {
89+
a: Box<Box<dyn T>>,
90+
b: Rc<Box<dyn T>>,
91+
c: Arc<Box<dyn T>>,
92+
}
93+
94+
pub fn test_box(_: Box<Box<dyn T>>) {}
95+
pub fn test_rc(_: Rc<Box<dyn T>>) {}
96+
pub fn test_arc(_: Arc<Box<dyn T>>) {}
97+
pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
98+
}
99+
80100
fn main() {}

tests/ui/redundant_allocation.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,14 @@ LL | pub fn arc_test9<T>(foo: Arc<Rc<T>>) -> Arc<Rc<SubT<T>>> {
134134
= note: `Rc<SubT<T>>` is already on the heap, `Arc<Rc<SubT<T>>>` makes an extra allocation
135135
= help: consider using just `Arc<SubT<T>>` or `Rc<SubT<T>>`
136136

137-
error: aborting due to 15 previous errors
137+
error: usage of `Rc<Box<Box<dyn T>>>`
138+
--> $DIR/redundant_allocation.rs:97:27
139+
|
140+
LL | pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
141+
| ^^^^^^^^^^^^^^^^^^^
142+
|
143+
= note: `Box<Box<dyn T>>` is already on the heap, `Rc<Box<Box<dyn T>>>` makes an extra allocation
144+
= help: consider using just `Rc<Box<dyn T>>` or `Box<Box<dyn T>>`
145+
146+
error: aborting due to 16 previous errors
138147

0 commit comments

Comments
 (0)