Skip to content

Commit 0def44a

Browse files
committed
Catch other thinning allocations using Ty
1 parent 631b4ee commit 0def44a

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::{path_def_id, qpath_generic_tys};
44
use rustc_errors::Applicability;
5-
use rustc_hir::{self as hir, def, def_id::DefId, PrimTy, QPath, TyKind};
5+
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,7 +55,8 @@ 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-
if alloc_makes_pointer_thin(cx, ty) {
58+
// Reallocation of a fat pointer causes it to become thin
59+
if !hir_ty_to_ty(cx.tcx, ty).is_sized(cx.tcx.at(ty.span), cx.param_env) {
5860
return false;
5961
}
6062
ty.span
@@ -109,20 +111,3 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
109111
}
110112
true
111113
}
112-
113-
/// Returns `true` if the allocation would make `hir_ty` go from fat to thin.
114-
fn alloc_makes_pointer_thin(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) -> bool {
115-
match &hir_ty.kind {
116-
TyKind::TraitObject(..) => true,
117-
TyKind::Path(ty_qpath) => {
118-
let ty_res = cx.qpath_res(ty_qpath, hir_ty.hir_id);
119-
if let def::Res::PrimTy(prim_ty) = ty_res {
120-
if matches!(prim_ty, PrimTy::Str) {
121-
return true;
122-
}
123-
}
124-
false
125-
},
126-
_ => false,
127-
}
128-
}

tests/ui/redundant_allocation.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,38 @@ mod box_dyn {
9898
}
9999

100100
// https://github.com/rust-lang/rust-clippy/issues/8604
101-
mod box_str {
101+
mod box_fat_ptr {
102102
use std::boxed::Box;
103+
use std::path::Path;
103104
use std::rc::Rc;
104105
use std::sync::Arc;
105106

107+
pub struct DynSized {
108+
foo: [usize],
109+
}
110+
106111
struct S {
107112
a: Box<Box<str>>,
108113
b: Rc<Box<str>>,
109114
c: Arc<Box<str>>,
115+
116+
e: Box<Box<[usize]>>,
117+
f: Box<Box<Path>>,
118+
g: Box<Box<DynSized>>,
110119
}
111120

112-
pub fn test_box(_: Box<Box<str>>) {}
113-
pub fn test_rc(_: Rc<Box<str>>) {}
114-
pub fn test_arc(_: Arc<Box<str>>) {}
115-
pub fn test_rc_box(_: Rc<Box<Box<str>>>) {}
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>>>) {}
116133
}
117134

118135
fn main() {}

tests/ui/redundant_allocation.stderr

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,40 @@ LL | pub fn test_rc_box(_: Rc<Box<Box<dyn T>>>) {}
144144
= help: consider using just `Rc<Box<dyn T>>` or `Box<Box<dyn T>>`
145145

146146
error: usage of `Rc<Box<Box<str>>>`
147-
--> $DIR/redundant_allocation.rs:115:27
147+
--> $DIR/redundant_allocation.rs:129:31
148148
|
149-
LL | pub fn test_rc_box(_: Rc<Box<Box<str>>>) {}
150-
| ^^^^^^^^^^^^^^^^^
149+
LL | pub fn test_rc_box_str(_: Rc<Box<Box<str>>>) {}
150+
| ^^^^^^^^^^^^^^^^^
151151
|
152152
= note: `Box<Box<str>>` is already on the heap, `Rc<Box<Box<str>>>` makes an extra allocation
153153
= help: consider using just `Rc<Box<str>>` or `Box<Box<str>>`
154154

155-
error: aborting due to 17 previous errors
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
156183

0 commit comments

Comments
 (0)