Skip to content

Commit f65a907

Browse files
committed
Use fold instead of collect/join and add comments
1 parent 43b10fa commit f65a907

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/librustc_errors/emitter.rs

+39-10
Original file line numberDiff line numberDiff line change
@@ -924,19 +924,48 @@ impl EmitterWriter {
924924
}
925925
},
926926
None => {
927+
// Diagnostic with lists need to render the list items at the
928+
// appropriate depth and composed into the body of the message.
927929
let msg = if child.list.len() == 0 {
930+
// Diagnostics without lists just need the original message
928931
child.message.to_owned()
929932
} else {
930-
format!("{}\n{}",
931-
&child.message,
932-
&child.list.iter().map(|item| {
933-
format!("{} - {}",
934-
(0..max_line_num_len)
935-
.map(|_| " ")
936-
.collect::<String>(),
937-
item)
938-
}).collect::<Vec<String>>()
939-
.join("\n"))
933+
// Diagnostic with a list of items needs to be rendered with the
934+
// appropriate padding at the left to have a consistent margin with
935+
// the `note: ` text.
936+
937+
// Add as many ` ` chars at the beggining to align the `- item`
938+
// text to the beggining of the `note: ` text. The extra 9 ` ` is
939+
// the padding that's always needed to align to the `note: `.
940+
let padding = (0..max_line_num_len + 9)
941+
.map(|_| " ")
942+
.collect::<String>();
943+
944+
// Concatenate the message and all the list items, properly aligned
945+
child.list.iter().fold(child.message.to_owned(), |mut acc, x| {
946+
acc.push_str("\n");
947+
acc.push_str(&padding);
948+
acc.push_str("- ");
949+
acc.push_str(x);
950+
acc
951+
})
952+
// msg will now be:
953+
//
954+
// child.message's content
955+
// - item 1
956+
// - item 2
957+
//
958+
// and the diagnostic will look like
959+
//
960+
// error: message
961+
// --> file.rs:3:20
962+
// |
963+
// 3 | <Code>
964+
// | ^^^^ highlight
965+
// |
966+
// = help: child.message's content
967+
// - item 1
968+
// - item 2
940969
};
941970
match self.emit_message_default(&child.span,
942971
&msg,

0 commit comments

Comments
 (0)