about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTimo <30553356+y21@users.noreply.github.com>2025-04-23 19:59:20 +0000
committerGitHub <noreply@github.com>2025-04-23 19:59:20 +0000
commit34f81f96e9f43ba5c033f146eabbd5eb0d48638f (patch)
tree6d445effb9d1d005244c35bcd90616e4a837f85f
parentdc695f53cd4105abf2e1247cda709be6f1c8509f (diff)
parentbf713a0e78208889c666e3656b073a8971fd4dd2 (diff)
downloadrust-34f81f96e9f43ba5c033f146eabbd5eb0d48638f.tar.gz
rust-34f81f96e9f43ba5c033f146eabbd5eb0d48638f.zip
style: pull one more `if` into the let-chain (#14669)
`cargo dev fmt` seems to not work for some reason (possibly because of
all the comments in the let-chain), so I formatted it manually -- I hope
it's right

changelog: none
-rw-r--r--clippy_lints/src/types/vec_box.rs105
1 files changed, 51 insertions, 54 deletions
diff --git a/clippy_lints/src/types/vec_box.rs b/clippy_lints/src/types/vec_box.rs
index 769244c675e..f13042a6fa6 100644
--- a/clippy_lints/src/types/vec_box.rs
+++ b/clippy_lints/src/types/vec_box.rs
@@ -19,61 +19,58 @@ pub(super) fn check<'tcx>(
     def_id: DefId,
     box_size_threshold: u64,
 ) -> bool {
-    if cx.tcx.is_diagnostic_item(sym::Vec, def_id) {
-        if let Some(last) = last_path_segment(qpath).args
-            // Get the _ part of Vec<_>
-            && let Some(GenericArg::Type(ty)) = last.args.first()
-            // extract allocator from the Vec for later
-            && let vec_alloc_ty = last.args.get(1)
-            // ty is now _ at this point
-            && let TyKind::Path(ref ty_qpath) = ty.kind
-            && let res = cx.qpath_res(ty_qpath, ty.hir_id)
-            && let Some(def_id) = res.opt_def_id()
-            && Some(def_id) == cx.tcx.lang_items().owned_box()
-            // At this point, we know ty is Box<T>, now get T
-            && let Some(last) = last_path_segment(ty_qpath).args
-            && let Some(GenericArg::Type(boxed_ty)) = last.args.first()
-            // extract allocator from the Box for later
-            && let boxed_alloc_ty = last.args.get(1)
-            // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
-            && let ty_ty = lower_ty(cx.tcx, boxed_ty.as_unambig_ty())
-            && !ty_ty.has_escaping_bound_vars()
-            && ty_ty.is_sized(cx.tcx, cx.typing_env())
-            && let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes())
-            && ty_ty_size < box_size_threshold
-            // https://github.com/rust-lang/rust-clippy/issues/7114
-            && match (vec_alloc_ty, boxed_alloc_ty) {
-                (None, None) => true,
-                // this is in the event that we have something like
-                // Vec<_, Global>, in which case is equivalent to
-                // Vec<_>
-                (None, Some(GenericArg::Type(inner))) | (Some(GenericArg::Type(inner)), None) => {
-                    if let TyKind::Path(path) = inner.kind
-                        && let Some(did) = cx.qpath_res(&path, inner.hir_id).opt_def_id() {
-                        cx.tcx.lang_items().get(LangItem::GlobalAlloc) == Some(did)
-                    } else {
-                        false
-                    }
-                },
-                (Some(GenericArg::Type(l)), Some(GenericArg::Type(r))) =>
-                    // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
-                    lower_ty(cx.tcx, l.as_unambig_ty()) == lower_ty(cx.tcx, r.as_unambig_ty()),
-                _ => false
-            }
-        {
-            span_lint_and_sugg(
-                cx,
-                VEC_BOX,
-                hir_ty.span,
-                "`Vec<T>` is already on the heap, the boxing is unnecessary",
-                "try",
-                format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
-                Applicability::Unspecified,
-            );
-            true
-        } else {
-            false
+    if cx.tcx.is_diagnostic_item(sym::Vec, def_id)
+        && let Some(last) = last_path_segment(qpath).args
+        // Get the _ part of Vec<_>
+        && let Some(GenericArg::Type(ty)) = last.args.first()
+        // extract allocator from the Vec for later
+        && let vec_alloc_ty = last.args.get(1)
+        // ty is now _ at this point
+        && let TyKind::Path(ref ty_qpath) = ty.kind
+        && let res = cx.qpath_res(ty_qpath, ty.hir_id)
+        && let Some(def_id) = res.opt_def_id()
+        && Some(def_id) == cx.tcx.lang_items().owned_box()
+        // At this point, we know ty is Box<T>, now get T
+        && let Some(last) = last_path_segment(ty_qpath).args
+        && let Some(GenericArg::Type(boxed_ty)) = last.args.first()
+        // extract allocator from the Box for later
+        && let boxed_alloc_ty = last.args.get(1)
+        // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
+        && let ty_ty = lower_ty(cx.tcx, boxed_ty.as_unambig_ty())
+        && !ty_ty.has_escaping_bound_vars()
+        && ty_ty.is_sized(cx.tcx, cx.typing_env())
+        && let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes())
+        && ty_ty_size < box_size_threshold
+        // https://github.com/rust-lang/rust-clippy/issues/7114
+        && match (vec_alloc_ty, boxed_alloc_ty) {
+            (None, None) => true,
+            // this is in the event that we have something like
+            // Vec<_, Global>, in which case is equivalent to
+            // Vec<_>
+            (None, Some(GenericArg::Type(inner))) | (Some(GenericArg::Type(inner)), None) => {
+                if let TyKind::Path(path) = inner.kind
+                    && let Some(did) = cx.qpath_res(&path, inner.hir_id).opt_def_id() {
+                    cx.tcx.lang_items().get(LangItem::GlobalAlloc) == Some(did)
+                } else {
+                    false
+                }
+            },
+            (Some(GenericArg::Type(l)), Some(GenericArg::Type(r))) =>
+                // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
+                lower_ty(cx.tcx, l.as_unambig_ty()) == lower_ty(cx.tcx, r.as_unambig_ty()),
+            _ => false
         }
+    {
+        span_lint_and_sugg(
+            cx,
+            VEC_BOX,
+            hir_ty.span,
+            "`Vec<T>` is already on the heap, the boxing is unnecessary",
+            "try",
+            format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
+            Applicability::Unspecified,
+        );
+        true
     } else {
         false
     }