Skip to content

Commit d5f5487

Browse files
committed
Auto merge of #5899 - JarredAllen:rc-box-suggestion, r=yaahc
Change Rc<Box<T>> recommendation to be Rc<T> instead of Box<T> Fixes #5722 changelog: Suggest `Rc<Box<T>>` -> `Rc<T>` in [`redundant_allocation`] lint
2 parents fc4fd91 + 4e28d99 commit d5f5487

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clippy_lints/src/types.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,25 @@ impl Types {
353353
);
354354
return; // don't recurse into the type
355355
}
356-
if let Some(span) = match_type_parameter(cx, qpath, &paths::BOX) {
356+
if match_type_parameter(cx, qpath, &paths::BOX).is_some() {
357+
let box_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
358+
GenericArg::Type(ty) => match &ty.kind {
359+
TyKind::Path(qpath) => qpath,
360+
_ => return,
361+
},
362+
_ => return,
363+
};
364+
let inner_span = match &last_path_segment(&box_ty).args.unwrap().args[0] {
365+
GenericArg::Type(ty) => ty.span,
366+
_ => return,
367+
};
357368
span_lint_and_sugg(
358369
cx,
359370
REDUNDANT_ALLOCATION,
360371
hir_ty.span,
361372
"usage of `Rc<Box<T>>`",
362373
"try",
363-
snippet(cx, span, "..").to_string(),
374+
format!("Rc<{}>", snippet(cx, inner_span, "..")),
364375
Applicability::MachineApplicable,
365376
);
366377
return; // don't recurse into the type

tests/ui/redundant_allocation.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn test5(a: Rc<bool>) {}
3333

3434
// Rc<Box<T>>
3535

36-
pub fn test6(a: Box<bool>) {}
36+
pub fn test6(a: Rc<bool>) {}
3737

3838
// Box<&T>
3939

tests/ui/redundant_allocation.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error: usage of `Rc<Box<T>>`
2828
--> $DIR/redundant_allocation.rs:36:17
2929
|
3030
LL | pub fn test6(a: Rc<Box<bool>>) {}
31-
| ^^^^^^^^^^^^^ help: try: `Box<bool>`
31+
| ^^^^^^^^^^^^^ help: try: `Rc<bool>`
3232

3333
error: usage of `Box<&T>`
3434
--> $DIR/redundant_allocation.rs:40:22

0 commit comments

Comments
 (0)