Skip to content

Commit c57920b

Browse files
committed
path: Fix joining Windows path when the receiver is "C:"
WindowsPath::new("C:").join("a") produces r"C:\a". This is incorrect. It should produce "C:a".
1 parent 9008931 commit c57920b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/libstd/path/windows.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,13 @@ impl GenericPathUnsafe for Path {
261261
let mut s = str::with_capacity(me.repr.len() + 1 + pathlen);
262262
s.push_str(me.repr);
263263
let plen = me.prefix_len();
264-
if !(me.repr.len() > plen && me.repr[me.repr.len()-1] == sep as u8) {
265-
s.push_char(sep);
264+
// if me is "C:" we don't want to add a path separator
265+
match me.prefix {
266+
Some(DiskPrefix) if me.repr.len() == plen => (),
267+
_ if !(me.repr.len() > plen && me.repr[me.repr.len()-1] == sep as u8) => {
268+
s.push_char(sep);
269+
}
270+
_ => ()
266271
}
267272
match path_ {
268273
None => s.push_str(path),
@@ -1549,6 +1554,8 @@ mod tests {
15491554
t!(s: "C:a\\b\\c", "C:d", "C:a\\b\\c\\d");
15501555
t!(s: "C:a\\b", "..\\..\\..\\c", "C:..\\c");
15511556
t!(s: "C:\\a\\b", "..\\..\\..\\c", "C:\\c");
1557+
t!(s: "C:", r"a\b\c", r"C:a\b\c");
1558+
t!(s: "C:", r"..\a", r"C:..\a");
15521559
t!(s: "\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
15531560
t!(s: "\\\\server\\share\\foo", "..\\..\\bar", "\\\\server\\share\\bar");
15541561
t!(s: "\\\\server\\share\\foo", "C:baz", "C:baz");

0 commit comments

Comments
 (0)