@@ -8,12 +8,11 @@ pub fn normalize_path(path: &Path) -> String {
8
8
// have to do a bit of work removing certain prefixes and replacing
9
9
// backslashes.
10
10
let mut components: Vec < String > = Vec :: new ( ) ;
11
- let mut is_disk = false ;
12
- for component in path . components ( ) {
11
+ let mut path_components = path . components ( ) . peekable ( ) ;
12
+ while let Some ( component) = path_components . next ( ) {
13
13
match component {
14
14
std:: path:: Component :: Prefix ( prefix) => match prefix. kind ( ) {
15
15
std:: path:: Prefix :: Disk ( letter) | std:: path:: Prefix :: VerbatimDisk ( letter) => {
16
- is_disk = true ;
17
16
components. push ( format ! ( "{}:" , letter as char ) ) ;
18
17
}
19
18
std:: path:: Prefix :: Verbatim ( x) | std:: path:: Prefix :: DeviceNS ( x) => {
@@ -28,16 +27,17 @@ pub fn normalize_path(path: &Path) -> String {
28
27
std:: path:: Component :: Normal ( n) => {
29
28
components. push ( n. to_string_lossy ( ) . to_string ( ) ) ;
30
29
}
31
- std:: path:: Component :: RootDir => { }
30
+ std:: path:: Component :: RootDir => {
31
+ if path_components. peek ( ) . is_none ( ) {
32
+ // The path points at a root directory, so we need to add a
33
+ // trailing slash, e.g. `C:/` instead of `C:`.
34
+ components. push ( "" . to_string ( ) ) ;
35
+ }
36
+ }
32
37
std:: path:: Component :: CurDir => { }
33
38
std:: path:: Component :: ParentDir => { }
34
39
}
35
40
}
36
- if components. len ( ) == 1 && is_disk {
37
- // If the path is just a drive letter, we need to add a trailing
38
- // slash to match the CodeQL spec.
39
- components. push ( "" . to_string ( ) ) ;
40
- }
41
41
components. join ( "/" )
42
42
} else {
43
43
// For other operating systems, we can use the canonicalized path
0 commit comments