Skip to content

Commit c143ae7

Browse files
committed
Auto merge of #29495 - meqif:fix_unindent_tabs, r=steveklabnik
A line may be indented with either spaces or tabs, but not a mix of both. If there is a mix of tabs and spaces, only the kind that occurs first is counted. This addresses issue #29268.
2 parents f18c905 + a9cbf6c commit c143ae7

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/librustdoc/passes.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,18 @@ pub fn unindent(s: &str) -> String {
331331
min_indent
332332
} else {
333333
saw_first_line = true;
334-
let mut spaces = 0;
334+
let mut whitespace = 0;
335335
line.chars().all(|char| {
336-
// Only comparing against space because I wouldn't
337-
// know what to do with mixed whitespace chars
338-
if char == ' ' {
339-
spaces += 1;
336+
// Compare against either space or tab, ignoring whether they
337+
// are mixed or not
338+
if char == ' ' || char == '\t' {
339+
whitespace += 1;
340340
true
341341
} else {
342342
false
343343
}
344344
});
345-
cmp::min(min_indent, spaces)
345+
cmp::min(min_indent, whitespace)
346346
}
347347
});
348348

@@ -407,4 +407,22 @@ mod unindent_tests {
407407
let r = unindent(&s);
408408
assert_eq!(r, "line1\n\n line2");
409409
}
410+
411+
#[test]
412+
fn should_unindent_tabs() {
413+
let s = "\tline1\n\tline2".to_string();
414+
let r = unindent(&s);
415+
assert_eq!(r, "line1\nline2");
416+
}
417+
418+
#[test]
419+
fn should_trim_mixed_indentation() {
420+
let s = "\t line1\n\t line2".to_string();
421+
let r = unindent(&s);
422+
assert_eq!(r, "line1\nline2");
423+
424+
let s = " \tline1\n \tline2".to_string();
425+
let r = unindent(&s);
426+
assert_eq!(r, "line1\nline2");
427+
}
410428
}

0 commit comments

Comments
 (0)