]> git.lizzy.rs Git - rust.git/commitdiff
Change Rc<Box<T>> recommendation to be Rc<T> instead of Box<T>
authorJarredAllen <jarredallen73@gmail.com>
Wed, 12 Aug 2020 01:01:10 +0000 (18:01 -0700)
committerJarredAllen <jarredallen73@gmail.com>
Wed, 12 Aug 2020 17:35:08 +0000 (10:35 -0700)
clippy_lints/src/types.rs
tests/ui/redundant_allocation.fixed
tests/ui/redundant_allocation.stderr

index c3dea44752133bf9d8a24fee5e402c427651ef76..78cebc30472b7c28657b62d0c2837b6a1f74b1c8 100644 (file)
@@ -353,14 +353,25 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: boo
                             );
                             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
index 266358334587d0a6edb4a94e328e77d1a6515c93..6514fd6d1ac76bbe7ebbbd37453af5002e3c3308 100644 (file)
@@ -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>
 
index eaa57ce3024b604243166b7e78bcf1d58dc6f536..92e4f67f5db6e40e6f2f0f9bd8ed422b3ec32c0c 100644 (file)
@@ -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