Skip to content

Commit 2038084

Browse files
committed
Auto merge of rust-lang#8813 - evantypanski:redundant_alloc_fat_ptr, r=Alexendoo
Fix redundant_allocation warning for Rc<Box<str>> changelog: [`redundant_allocation`] Fixes rust-lang#8604 Fixes false positives where a fat pointer with `str` type was made thin by another allocation, but that thinning allocation was marked as redundant
2 parents 01d75b1 + ad7338e commit 2038084

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_errors::Applicability;
55
use rustc_hir::{self as hir, def_id::DefId, QPath, TyKind};
66
use rustc_lint::LateContext;
77
use rustc_span::symbol::sym;
8+
use rustc_typeck::hir_ty_to_ty;
89

910
use super::{utils, REDUNDANT_ALLOCATION};
1011

@@ -54,8 +55,10 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5455
};
5556
let inner_span = match qpath_generic_tys(inner_qpath).next() {
5657
Some(ty) => {
57-
// Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
58-
if matches!(ty.kind, TyKind::TraitObject(..)) {
58+
// Reallocation of a fat pointer causes it to become thin. `hir_ty_to_ty` is safe to use
59+
// here because `mod.rs` guarantees this lint is only run on types outside of bodies and
60+
// is not run on locals.
61+
if !hir_ty_to_ty(cx.tcx, ty).is_sized(cx.tcx.at(ty.span), cx.param_env) {
5962
return false;
6063
}
6164
ty.span

tests/ui/redundant_allocation.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,39 @@ mod box_dyn {
9797
pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
9898
}
9999

100+
// https://github.com/rust-lang/rust-clippy/issues/8604
101+
mod box_fat_ptr {
102+
use std::boxed::Box;
103+
use std::path::Path;
104+
use std::rc::Rc;
105+
use std::sync::Arc;
106+
107+
pub struct DynSized {
108+
foo: [usize],
109+
}
110+
111+
struct S {
112+
a: Box<Box<str>>,
113+
b: Rc<Box<str>>,
114+
c: Arc<Box<str>>,
115+
116+
e: Box<Box<[usize]>>,
117+
f: Box<Box<Path>>,
118+
g: Box<Box<DynSized>>,
119+
}
120+
121+
pub fn test_box_str(_: Box<Box<str>>) {}
122+
pub fn test_rc_str(_: Rc<Box<str>>) {}
123+
pub fn test_arc_str(_: Arc<Box<str>>) {}
124+
125+
pub fn test_box_slice(_: Box<Box<[usize]>>) {}
126+
pub fn test_box_path(_: Box<Box<Path>>) {}
127+
pub fn test_box_custom(_: Box<Box<DynSized>>) {}
128+
129+
pub fn test_rc_box_str(_: Rc<Box<Box<str>>>) {}
130+
pub fn test_rc_box_slice(_: Rc<Box<Box<[usize]>>>) {}
131+
pub fn test_rc_box_path(_: Rc<Box<Box<Path>>>) {}
132+
pub fn test_rc_box_custom(_: Rc<Box<Box<DynSized>>>) {}
133+
}
134+
100135
fn main() {}

tests/ui/redundant_allocation.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,41 @@ LL | pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
143143
= note: `Box<Box<dyn T>>` is already on the heap, `Rc<Box<Box<dyn T>>>` makes an extra allocation
144144
= help: consider using just `Rc<Box<dyn T>>` or `Box<Box<dyn T>>`
145145

146-
error: aborting due to 16 previous errors
146+
error: usage of `Rc<Box<Box<str>>>`
147+
--> $DIR/redundant_allocation.rs:129:31
148+
|
149+
LL | pub fn test_rc_box_str(_: Rc<Box<Box<str>>>) {}
150+
| ^^^^^^^^^^^^^^^^^
151+
|
152+
= note: `Box<Box<str>>` is already on the heap, `Rc<Box<Box<str>>>` makes an extra allocation
153+
= help: consider using just `Rc<Box<str>>` or `Box<Box<str>>`
154+
155+
error: usage of `Rc<Box<Box<[usize]>>>`
156+
--> $DIR/redundant_allocation.rs:130:33
157+
|
158+
LL | pub fn test_rc_box_slice(_: Rc<Box<Box<[usize]>>>) {}
159+
| ^^^^^^^^^^^^^^^^^^^^^
160+
|
161+
= note: `Box<Box<[usize]>>` is already on the heap, `Rc<Box<Box<[usize]>>>` makes an extra allocation
162+
= help: consider using just `Rc<Box<[usize]>>` or `Box<Box<[usize]>>`
163+
164+
error: usage of `Rc<Box<Box<Path>>>`
165+
--> $DIR/redundant_allocation.rs:131:32
166+
|
167+
LL | pub fn test_rc_box_path(_: Rc<Box<Box<Path>>>) {}
168+
| ^^^^^^^^^^^^^^^^^^
169+
|
170+
= note: `Box<Box<Path>>` is already on the heap, `Rc<Box<Box<Path>>>` makes an extra allocation
171+
= help: consider using just `Rc<Box<Path>>` or `Box<Box<Path>>`
172+
173+
error: usage of `Rc<Box<Box<DynSized>>>`
174+
--> $DIR/redundant_allocation.rs:132:34
175+
|
176+
LL | pub fn test_rc_box_custom(_: Rc<Box<Box<DynSized>>>) {}
177+
| ^^^^^^^^^^^^^^^^^^^^^^
178+
|
179+
= note: `Box<Box<DynSized>>` is already on the heap, `Rc<Box<Box<DynSized>>>` makes an extra allocation
180+
= help: consider using just `Rc<Box<DynSized>>` or `Box<Box<DynSized>>`
181+
182+
error: aborting due to 20 previous errors
147183

0 commit comments

Comments
 (0)