58
58
//! [`push`]: PathBuf::push
59
59
60
60
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
61
+ #![ deny( unsafe_op_in_unsafe_fn) ]
61
62
62
63
#[ cfg( test) ]
63
64
mod tests;
@@ -294,7 +295,8 @@ fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
294
295
unsafe { & * ( s as * const OsStr as * const [ u8 ] ) }
295
296
}
296
297
unsafe fn u8_slice_as_os_str ( s : & [ u8 ] ) -> & OsStr {
297
- & * ( s as * const [ u8 ] as * const OsStr )
298
+ // SAFETY: see the comment of `os_str_as_u8_slice`
299
+ unsafe { & * ( s as * const [ u8 ] as * const OsStr ) }
298
300
}
299
301
300
302
// Detect scheme on Redox
@@ -314,24 +316,21 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
314
316
315
317
// basic workhorse for splitting stem and extension
316
318
fn split_file_at_dot ( file : & OsStr ) -> ( Option < & OsStr > , Option < & OsStr > ) {
317
- unsafe {
318
- if os_str_as_u8_slice ( file) == b".." {
319
- return ( Some ( file) , None ) ;
320
- }
321
-
322
- // The unsafety here stems from converting between &OsStr and &[u8]
323
- // and back. This is safe to do because (1) we only look at ASCII
324
- // contents of the encoding and (2) new &OsStr values are produced
325
- // only from ASCII-bounded slices of existing &OsStr values.
319
+ if os_str_as_u8_slice ( file) == b".." {
320
+ return ( Some ( file) , None ) ;
321
+ }
326
322
327
- let mut iter = os_str_as_u8_slice ( file) . rsplitn ( 2 , |b| * b == b'.' ) ;
328
- let after = iter. next ( ) ;
329
- let before = iter. next ( ) ;
330
- if before == Some ( b"" ) {
331
- ( Some ( file) , None )
332
- } else {
333
- ( before. map ( |s| u8_slice_as_os_str ( s) ) , after. map ( |s| u8_slice_as_os_str ( s) ) )
334
- }
323
+ // The unsafety here stems from converting between &OsStr and &[u8]
324
+ // and back. This is safe to do because (1) we only look at ASCII
325
+ // contents of the encoding and (2) new &OsStr values are produced
326
+ // only from ASCII-bounded slices of existing &OsStr values.
327
+ let mut iter = os_str_as_u8_slice ( file) . rsplitn ( 2 , |b| * b == b'.' ) ;
328
+ let after = iter. next ( ) ;
329
+ let before = iter. next ( ) ;
330
+ if before == Some ( b"" ) {
331
+ ( Some ( file) , None )
332
+ } else {
333
+ unsafe { ( before. map ( |s| u8_slice_as_os_str ( s) ) , after. map ( |s| u8_slice_as_os_str ( s) ) ) }
335
334
}
336
335
}
337
336
@@ -1702,7 +1701,7 @@ impl Path {
1702
1701
// The following (private!) function allows construction of a path from a u8
1703
1702
// slice, which is only safe when it is known to follow the OsStr encoding.
1704
1703
unsafe fn from_u8_slice ( s : & [ u8 ] ) -> & Path {
1705
- Path :: new ( u8_slice_as_os_str ( s) )
1704
+ unsafe { Path :: new ( u8_slice_as_os_str ( s) ) }
1706
1705
}
1707
1706
// The following (private!) function reveals the byte encoding used for OsStr.
1708
1707
fn as_u8_slice ( & self ) -> & [ u8 ] {
0 commit comments