Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 631b4ee

Browse files
committed
Fix redundant_allocation warning for Rc<Box<str>>
1 parent d422baa commit 631b4ee

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

clippy_lints/src/types/redundant_allocation.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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_id::DefId, QPath, TyKind};
5+
use rustc_hir::{self as hir, def, def_id::DefId, PrimTy, QPath, TyKind};
66
use rustc_lint::LateContext;
77
use rustc_span::symbol::sym;
88

@@ -54,8 +54,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
5454
};
5555
let inner_span = match qpath_generic_tys(inner_qpath).next() {
5656
Some(ty) => {
57-
// Box<Box<dyn T>> is smaller than Box<dyn T> because of wide pointers
58-
if matches!(ty.kind, TyKind::TraitObject(..)) {
57+
if alloc_makes_pointer_thin(cx, ty) {
5958
return false;
6059
}
6160
ty.span
@@ -110,3 +109,20 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
110109
}
111110
true
112111
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,22 @@ 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_str {
102+
use std::boxed::Box;
103+
use std::rc::Rc;
104+
use std::sync::Arc;
105+
106+
struct S {
107+
a: Box<Box<str>>,
108+
b: Rc<Box<str>>,
109+
c: Arc<Box<str>>,
110+
}
111+
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>>>) {}
116+
}
117+
100118
fn main() {}

tests/ui/redundant_allocation.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,14 @@ 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:115:27
148+
|
149+
LL | pub fn test_rc_box(_: 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: aborting due to 17 previous errors
147156

0 commit comments

Comments
 (0)