Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Rc<Box<T>> recommendation to be Rc<T> instead of Box<T> #5899

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,25 @@ impl Types {
);
return; // don't recurse into the type
}
if let Some(span) = match_type_parameter(cx, qpath, &paths::BOX) {
if match_type_parameter(cx, qpath, &paths::BOX).is_some() {
let box_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
GenericArg::Type(ty) => match &ty.kind {
TyKind::Path(qpath) => qpath,
_ => return,
},
_ => return,
};
let inner_span = match &last_path_segment(&box_ty).args.unwrap().args[0] {
GenericArg::Type(ty) => ty.span,
_ => return,
};
span_lint_and_sugg(
cx,
REDUNDANT_ALLOCATION,
hir_ty.span,
"usage of `Rc<Box<T>>`",
"try",
snippet(cx, span, "..").to_string(),
format!("Rc<{}>", snippet(cx, inner_span, "..")),
Applicability::MachineApplicable,
);
return; // don't recurse into the type
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/redundant_allocation.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn test5(a: Rc<bool>) {}

// Rc<Box<T>>

pub fn test6(a: Box<bool>) {}
pub fn test6(a: Rc<bool>) {}

// Box<&T>

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/redundant_allocation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: usage of `Rc<Box<T>>`
--> $DIR/redundant_allocation.rs:36:17
|
LL | pub fn test6(a: Rc<Box<bool>>) {}
| ^^^^^^^^^^^^^ help: try: `Box<bool>`
| ^^^^^^^^^^^^^ help: try: `Rc<bool>`

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