66
66
clippy:: style,
67
67
clippy:: suspicious,
68
68
clippy:: todo,
69
+ clippy:: undocumented_unsafe_blocks,
69
70
clippy:: unimplemented,
70
71
clippy:: unnested_or_patterns,
71
72
clippy:: unwrap_used,
@@ -143,22 +144,26 @@ mod zerocopy {
143
144
// Implements an unsafe trait for a range of container types.
144
145
macro_rules! impl_for_composite_types {
145
146
( $trait: ident) => {
147
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
148
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
146
149
unsafe impl <T > $trait for PhantomData <T > {
147
150
fn only_derive_is_allowed_to_implement_this_trait( )
148
151
where
149
152
Self : Sized ,
150
153
{
151
154
}
152
155
}
156
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
157
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
153
158
unsafe impl <T : $trait> $trait for [ T ] {
154
159
fn only_derive_is_allowed_to_implement_this_trait( )
155
160
where
156
161
Self : Sized ,
157
162
{
158
163
}
159
164
}
160
- // According to the `Wrapping` docs, "`Wrapping<T>` is guaranteed to
161
- // have the same layout and ABI as `T`."
165
+ // SAFETY: According to the `Wrapping` docs, "`Wrapping<T>` is
166
+ // guaranteed to have the same layout and ABI as `T`."
162
167
unsafe impl <T : $trait> $trait for Wrapping <T > {
163
168
fn only_derive_is_allowed_to_implement_this_trait( )
164
169
where
@@ -167,6 +172,9 @@ macro_rules! impl_for_composite_types {
167
172
}
168
173
}
169
174
// Unit type has an empty representation.
175
+ //
176
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
177
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
170
178
unsafe impl $trait for ( ) {
171
179
fn only_derive_is_allowed_to_implement_this_trait( )
172
180
where
@@ -175,6 +183,9 @@ macro_rules! impl_for_composite_types {
175
183
}
176
184
}
177
185
// Constant sized array with elements implementing `$trait`.
186
+ //
187
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
188
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
178
189
unsafe impl <T : $trait, const N : usize > $trait for [ T ; N ] {
179
190
fn only_derive_is_allowed_to_implement_this_trait( )
180
191
where
@@ -189,6 +200,8 @@ macro_rules! impl_for_composite_types {
189
200
macro_rules! impl_for_types {
190
201
( $trait: ident, $( $types: ty) ,* $( , ) ?) => (
191
202
$(
203
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
204
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
192
205
unsafe impl $trait for $types {
193
206
fn only_derive_is_allowed_to_implement_this_trait( ) { }
194
207
}
@@ -339,11 +352,9 @@ pub unsafe trait FromBytes {
339
352
where
340
353
Self : Sized ,
341
354
{
342
- unsafe {
343
- // SAFETY: `FromBytes` says all bit patterns (including zeroes) are
344
- // legal.
345
- mem:: zeroed ( )
346
- }
355
+ // SAFETY: `FromBytes` says all bit patterns (including zeroes) are
356
+ // legal.
357
+ unsafe { mem:: zeroed ( ) }
347
358
}
348
359
349
360
/// Creates a `Box<Self>` from zeroed bytes.
@@ -502,6 +513,8 @@ pub unsafe trait AsBytes {
502
513
/// `as_bytes` provides access to the bytes of this value as an immutable
503
514
/// byte slice.
504
515
fn as_bytes ( & self ) -> & [ u8 ] {
516
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
517
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
505
518
unsafe {
506
519
// Note that this method does not have a `Self: Sized` bound;
507
520
// `size_of_val` works for unsized values too.
@@ -519,6 +532,8 @@ pub unsafe trait AsBytes {
519
532
where
520
533
Self : FromBytes ,
521
534
{
535
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
536
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
522
537
unsafe {
523
538
// Note that this method does not have a `Self: Sized` bound;
524
539
// `size_of_val` works for unsized values too.
@@ -889,10 +904,10 @@ macro_rules! transmute {
889
904
}
890
905
transmute( e)
891
906
} else {
892
- // `core::mem::transmute` ensures that the type of `e` and the type
893
- // of this macro invocation expression have the same size. We know
894
- // this transmute is safe thanks to the `AsBytes` and `FromBytes`
895
- // bounds enforced by the `false` branch.
907
+ // SAFETY: `core::mem::transmute` ensures that the type of `e` and
908
+ // the type of this macro invocation expression have the same size.
909
+ // We know this transmute is safe thanks to the `AsBytes` and
910
+ // `FromBytes` bounds enforced by the `false` branch.
896
911
//
897
912
// We use `$crate::__real_transmute` because we know it will always
898
913
// be available for crates which are using the 2015 edition of Rust.
@@ -1613,7 +1628,11 @@ where
1613
1628
/// and no mutable references to the same memory may be constructed during
1614
1629
/// `'a`.
1615
1630
unsafe fn deref_helper < ' a > ( & self ) -> & ' a T {
1616
- unsafe { & * self . 0 . as_ptr ( ) . cast :: < T > ( ) }
1631
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
1632
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
1633
+ unsafe {
1634
+ & * self . 0 . as_ptr ( ) . cast :: < T > ( )
1635
+ }
1617
1636
}
1618
1637
}
1619
1638
@@ -1634,7 +1653,11 @@ where
1634
1653
/// and no other references - mutable or immutable - to the same memory may
1635
1654
/// be constructed during `'a`.
1636
1655
unsafe fn deref_mut_helper < ' a > ( & mut self ) -> & ' a mut T {
1637
- unsafe { & mut * self . 0 . as_mut_ptr ( ) . cast :: < T > ( ) }
1656
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
1657
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
1658
+ unsafe {
1659
+ & mut * self . 0 . as_mut_ptr ( ) . cast :: < T > ( )
1660
+ }
1638
1661
}
1639
1662
}
1640
1663
@@ -1659,7 +1682,11 @@ where
1659
1682
debug_assert_eq ! ( len % elem_size, 0 ) ;
1660
1683
len / elem_size
1661
1684
} ;
1662
- unsafe { slice:: from_raw_parts ( self . 0 . as_ptr ( ) . cast :: < T > ( ) , elems) }
1685
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
1686
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
1687
+ unsafe {
1688
+ slice:: from_raw_parts ( self . 0 . as_ptr ( ) . cast :: < T > ( ) , elems)
1689
+ }
1663
1690
}
1664
1691
}
1665
1692
@@ -1685,7 +1712,11 @@ where
1685
1712
debug_assert_eq ! ( len % elem_size, 0 ) ;
1686
1713
len / elem_size
1687
1714
} ;
1688
- unsafe { slice:: from_raw_parts_mut ( self . 0 . as_mut_ptr ( ) . cast :: < T > ( ) , elems) }
1715
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
1716
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
1717
+ unsafe {
1718
+ slice:: from_raw_parts_mut ( self . 0 . as_mut_ptr ( ) . cast :: < T > ( ) , elems)
1719
+ }
1689
1720
}
1690
1721
}
1691
1722
@@ -2032,32 +2063,48 @@ pub unsafe trait ByteSliceMut: ByteSlice + DerefMut {
2032
2063
}
2033
2064
}
2034
2065
2066
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2067
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2035
2068
unsafe impl < ' a > ByteSlice for & ' a [ u8 ] {
2036
2069
#[ inline]
2037
2070
fn split_at ( self , mid : usize ) -> ( Self , Self ) {
2038
2071
<[ u8 ] >:: split_at ( self , mid)
2039
2072
}
2040
2073
}
2074
+
2075
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2076
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2041
2077
unsafe impl < ' a > ByteSlice for & ' a mut [ u8 ] {
2042
2078
#[ inline]
2043
2079
fn split_at ( self , mid : usize ) -> ( Self , Self ) {
2044
2080
<[ u8 ] >:: split_at_mut ( self , mid)
2045
2081
}
2046
2082
}
2083
+
2084
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2085
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2047
2086
unsafe impl < ' a > ByteSlice for Ref < ' a , [ u8 ] > {
2048
2087
#[ inline]
2049
2088
fn split_at ( self , mid : usize ) -> ( Self , Self ) {
2050
2089
Ref :: map_split ( self , |slice| <[ u8 ] >:: split_at ( slice, mid) )
2051
2090
}
2052
2091
}
2092
+
2093
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2094
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2053
2095
unsafe impl < ' a > ByteSlice for RefMut < ' a , [ u8 ] > {
2054
2096
#[ inline]
2055
2097
fn split_at ( self , mid : usize ) -> ( Self , Self ) {
2056
2098
RefMut :: map_split ( self , |slice| <[ u8 ] >:: split_at_mut ( slice, mid) )
2057
2099
}
2058
2100
}
2059
2101
2102
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2103
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2060
2104
unsafe impl < ' a > ByteSliceMut for & ' a mut [ u8 ] { }
2105
+
2106
+ // TODO(#61): Add a "SAFETY" comment and remove this `allow`.
2107
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
2061
2108
unsafe impl < ' a > ByteSliceMut for RefMut < ' a , [ u8 ] > { }
2062
2109
2063
2110
#[ cfg( feature = "alloc" ) ]
@@ -2326,7 +2373,7 @@ pub use alloc_support::*;
2326
2373
mod tests {
2327
2374
#![ allow( clippy:: unreadable_literal) ]
2328
2375
2329
- use core:: ops:: Deref ;
2376
+ use core:: { convert :: TryInto , ops:: Deref } ;
2330
2377
2331
2378
use super :: * ;
2332
2379
@@ -2346,8 +2393,7 @@ mod tests {
2346
2393
2347
2394
// Converts a `u64` to bytes using this platform's endianness.
2348
2395
fn u64_to_bytes ( u : u64 ) -> [ u8 ; 8 ] {
2349
- let u: * const u64 = & u;
2350
- unsafe { ptr:: read ( u. cast :: < [ u8 ; 8 ] > ( ) ) }
2396
+ U64 :: < NativeEndian > :: new ( u) . as_bytes ( ) . try_into ( ) . unwrap ( )
2351
2397
}
2352
2398
2353
2399
#[ test]
0 commit comments