Omit check of a successive line in loop

I think that s == "" is the only edge case (as it makes iter.next() return None the first time). The early return is necessary so that the last character of 'out' isn't popped if s == "" && !frag.need_backline
This commit is contained in:
vacuus 2021-12-18 21:54:59 -05:00 committed by Roc Yu
parent 41c3017c82
commit 38167a806c
No known key found for this signature in database
GPG key ID: 5068CE514A79D27F

View file

@ -939,7 +939,13 @@ crate enum DocFragmentKind {
// `need_backline` field).
fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
let s = frag.doc.as_str();
let mut iter = s.lines().peekable();
let mut iter = s.lines();
if s == "" {
if frag.need_backline {
out.push('\n');
}
return;
}
while let Some(line) = iter.next() {
if line.chars().any(|c| !c.is_whitespace()) {
assert!(line.len() >= frag.indent);
@ -947,13 +953,11 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
} else {
out.push_str(line);
}
if iter.peek().is_some() {
out.push('\n');
}
}
if frag.need_backline {
out.push('\n');
}
if !frag.need_backline {
out.pop();
}
}
/// Collapse a collection of [`DocFragment`]s into one string,