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