Skip to content

alloc: reduce code in unsafe blocks - #162149

Draft
DanielEScherzer wants to merge 6 commits into
rust-lang:mainfrom
DanielEScherzer:less-unsafe-in-alloc-2
Draft

DanielEScherzer wants to merge 6 commits into
rust-lang:mainfrom
DanielEScherzer:less-unsafe-in-alloc-2

Conversation

@DanielEScherzer

Copy link
Copy Markdown
Contributor

No description provided.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 1, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
downloading https://static.rust-lang.org/dist/2026-08-30/rustc-nightly-x86_64-unknown-linux-gnu.tar.xz
extracting /checkout/obj/build/cache/2026-08-30/rustc-nightly-x86_64-unknown-linux-gnu.tar.xz to /checkout/obj/build/x86_64-unknown-linux-gnu/rustfmt
[TIMING:end] format::InternalRustfmt {  } -- 12.797
Diff in /checkout/library/alloc/src/collections/vec_deque/iter_mut.rs:213:
         if idx < i1_len {
             // SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
             // that is in bounds.
-            unsafe {
-                self.i1.__iterator_get_unchecked(idx)
-            }
+            unsafe { self.i1.__iterator_get_unchecked(idx) }
         } else {
             // SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
             // that is in bounds.
Diff in /checkout/library/alloc/src/collections/vec_deque/iter_mut.rs:222:
-            unsafe {
-                self.i2.__iterator_get_unchecked(idx - i1_len)
-            }
+            unsafe { self.i2.__iterator_get_unchecked(idx - i1_len) }
         }
     }
 }
Diff in /checkout/library/alloc/src/collections/vec_deque/iter.rs:149:
         if idx < i1_len {
             // SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
             // that is in bounds.
-            unsafe {
-                self.i1.__iterator_get_unchecked(idx)
-            }
+            unsafe { self.i1.__iterator_get_unchecked(idx) }
         } else {
             // SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
             // that is in bounds.
Diff in /checkout/library/alloc/src/collections/vec_deque/iter.rs:158:
-            unsafe {
-                self.i2.__iterator_get_unchecked(idx - i1_len)
-            }
+            unsafe { self.i2.__iterator_get_unchecked(idx - i1_len) }
         }
     }
 }
error: closure bodies that contain statements must be surrounded by braces
##[error]   --> /checkout/library/alloc/src/collections/vec_deque/mod.rs:636:48
    |
636 |         iter.enumerate().for_each(|(i, element)|
    |                                                ^
...
641 |         );
    |         ^
    |
note: statement found outside of a block
   --> /checkout/library/alloc/src/collections/vec_deque/mod.rs:640:26
    |
637 | /             unsafe {
638 | |                 self.buffer_write(dst.add(i), element);
639 | |             }
640 | |             *written += 1;
    | |                         -^ this `;` turns the preceding closure into a statement
    | |_________________________|
    |                           this expression is a statement because of the trailing semicolon
note: the closure body may be incorrectly delimited
   --> /checkout/library/alloc/src/collections/vec_deque/mod.rs:636:35
    |
636 |           iter.enumerate().for_each(|(i, element)|
    |  ___________________________________^
637 | |             unsafe {
638 | |                 self.buffer_write(dst.add(i), element);
639 | |             }
640 | |             *written += 1;
    | |_________________________^ this is the parsed closure...
641 |           );
    |           - ...but likely you meant the closure to end here
help: try adding braces
    |
636 ~         iter.enumerate().for_each(|(i, element)| {
637 |             unsafe {
...
640 |             *written += 1;
641 ~         });
    |

Diff in /checkout/library/alloc/src/boxed/convert.rs:720:
         if self.is::<T>() {
             let raw: *mut dyn Error = Box::into_raw(self);
             // SAFETY: Check ensures the type is correct.
-            unsafe {
-                Ok(Box::from_raw(raw as *mut T))
-            }
+            unsafe { Ok(Box::from_raw(raw as *mut T)) }
         } else {
             Err(self)
         }
fmt: checked 7212 files
Bootstrap failed while executing `test src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck`

@rust-bors

rust-bors Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #165286) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants