about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJarredAllen <jarredallen73@gmail.com>2020-08-11 18:01:10 -0700
committerJarredAllen <jarredallen73@gmail.com>2020-08-12 10:35:08 -0700
commit480ccc3dbec4440bea0aa1f47d2ad21ebcdd578e (patch)
treeab87257b6715525300b06aac3491016421b70265
parent439bae62a48fee04b0655548d42e7076b6a5042f (diff)
downloadrust-480ccc3dbec4440bea0aa1f47d2ad21ebcdd578e.tar.gz
rust-480ccc3dbec4440bea0aa1f47d2ad21ebcdd578e.zip
Change Rc<Box<T>> recommendation to be Rc<T> instead of Box<T>
-rw-r--r--clippy_lints/src/types.rs15
-rw-r--r--tests/ui/redundant_allocation.fixed2
-rw-r--r--tests/ui/redundant_allocation.stderr2
3 files changed, 15 insertions, 4 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index c3dea447521..78cebc30472 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -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,
+                                    _ => panic!("Box that isn't a type"),
+                                },
+                                _ => panic!("Rc without type argument"),
+                            };
+                            let inner_span = match &last_path_segment(&box_ty).args.unwrap().args[0] {
+                                GenericArg::Type(ty) => ty.span,
+                                _ => panic!("Box without type argument"),
+                            };
                             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
diff --git a/tests/ui/redundant_allocation.fixed b/tests/ui/redundant_allocation.fixed
index 26635833458..6514fd6d1ac 100644
--- a/tests/ui/redundant_allocation.fixed
+++ b/tests/ui/redundant_allocation.fixed
@@ -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>
 
diff --git a/tests/ui/redundant_allocation.stderr b/tests/ui/redundant_allocation.stderr
index eaa57ce3024..92e4f67f5db 100644
--- a/tests/ui/redundant_allocation.stderr
+++ b/tests/ui/redundant_allocation.stderr
@@ -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