diff --git a/src/lists.rs b/src/lists.rs index c415032ad67b..0e3baa7c1e15 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -297,17 +297,15 @@ where if formatting.ends_with_newline { trailing_separator = true; } + } else if line_len > 0 { + result.push(' '); + line_len += 1; } if last && formatting.ends_with_newline { separate = formatting.trailing_separator != SeparatorTactic::Never; } - if line_len > 0 { - result.push(' '); - line_len += 1; - } - line_len += total_width; } _ => {} @@ -341,6 +339,8 @@ where } else { result.push('\n'); result.push_str(indent_str); + // This is the width of the item (without comments). + line_len = item.item.as_ref().map_or(0, |str| str.len()); } } else { result.push(' '); diff --git a/tests/source/array_comment.rs b/tests/source/array_comment.rs new file mode 100644 index 000000000000..87372b2793fc --- /dev/null +++ b/tests/source/array_comment.rs @@ -0,0 +1,19 @@ +// Issue 2842 +// The comment should not make the last line shorter + +static XXX: [i8; 64] = [ + 1, // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; + +static XXX: [i8; 64] = [ + 1, + // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; + +static XXX: [i8; 64] = [ + 1, + // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; diff --git a/tests/target/array_comment.rs b/tests/target/array_comment.rs new file mode 100644 index 000000000000..93e1f5f40345 --- /dev/null +++ b/tests/target/array_comment.rs @@ -0,0 +1,18 @@ +// Issue 2842 +// The comment should not make the last line shorter + +static XXX: [i8; 64] = [ + 1, // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; + +static XXX: [i8; 64] = [ + 1, // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; + +static XXX: [i8; 64] = [ + 1, // Comment + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, +];