68
68
//! [`OsStr`]: ../../std/ffi/struct.OsStr.html
69
69
70
70
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
71
+ #![ deny( unsafe_op_in_unsafe_fn) ]
71
72
72
73
use crate :: borrow:: { Borrow , Cow } ;
73
74
use crate :: cmp;
@@ -301,7 +302,8 @@ fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
301
302
unsafe { & * ( s as * const OsStr as * const [ u8 ] ) }
302
303
}
303
304
unsafe fn u8_slice_as_os_str ( s : & [ u8 ] ) -> & OsStr {
304
- & * ( s as * const [ u8 ] as * const OsStr )
305
+ // SAFETY: see the comment of `os_str_as_u8_slice`
306
+ unsafe { & * ( s as * const [ u8 ] as * const OsStr ) }
305
307
}
306
308
307
309
// Detect scheme on Redox
@@ -321,24 +323,21 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
321
323
322
324
// basic workhorse for splitting stem and extension
323
325
fn split_file_at_dot ( file : & OsStr ) -> ( Option < & OsStr > , Option < & OsStr > ) {
324
- unsafe {
325
- if os_str_as_u8_slice ( file) == b".." {
326
- return ( Some ( file) , None ) ;
327
- }
328
-
329
- // The unsafety here stems from converting between &OsStr and &[u8]
330
- // and back. This is safe to do because (1) we only look at ASCII
331
- // contents of the encoding and (2) new &OsStr values are produced
332
- // only from ASCII-bounded slices of existing &OsStr values.
333
-
334
- let mut iter = os_str_as_u8_slice ( file) . rsplitn ( 2 , |b| * b == b'.' ) ;
335
- let after = iter. next ( ) ;
336
- let before = iter. next ( ) ;
337
- if before == Some ( b"" ) {
338
- ( Some ( file) , None )
339
- } else {
340
- ( before. map ( |s| u8_slice_as_os_str ( s) ) , after. map ( |s| u8_slice_as_os_str ( s) ) )
341
- }
326
+ if os_str_as_u8_slice ( file) == b".." {
327
+ return ( Some ( file) , None ) ;
328
+ }
329
+
330
+ // The unsafety here stems from converting between &OsStr and &[u8]
331
+ // and back. This is safe to do because (1) we only look at ASCII
332
+ // contents of the encoding and (2) new &OsStr values are produced
333
+ // only from ASCII-bounded slices of existing &OsStr values.
334
+ let mut iter = os_str_as_u8_slice ( file) . rsplitn ( 2 , |b| * b == b'.' ) ;
335
+ let after = iter. next ( ) ;
336
+ let before = iter. next ( ) ;
337
+ if before == Some ( b"" ) {
338
+ ( Some ( file) , None )
339
+ } else {
340
+ unsafe { ( before. map ( |s| u8_slice_as_os_str ( s) ) , after. map ( |s| u8_slice_as_os_str ( s) ) ) }
342
341
}
343
342
}
344
343
@@ -1755,7 +1754,7 @@ impl Path {
1755
1754
// The following (private!) function allows construction of a path from a u8
1756
1755
// slice, which is only safe when it is known to follow the OsStr encoding.
1757
1756
unsafe fn from_u8_slice ( s : & [ u8 ] ) -> & Path {
1758
- Path :: new ( u8_slice_as_os_str ( s) )
1757
+ unsafe { Path :: new ( u8_slice_as_os_str ( s) ) }
1759
1758
}
1760
1759
// The following (private!) function reveals the byte encoding used for OsStr.
1761
1760
fn as_u8_slice ( & self ) -> & [ u8 ] {
0 commit comments