From b28a0cd6e68e342aa026e25dcc651cb9bc5007f8 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 17 Jul 2018 15:38:50 +1200 Subject: [PATCH] Fix an anomaly with comments and array literals Closes #2842 --- src/lists.rs | 10 +++++----- tests/source/array_comment.rs | 19 +++++++++++++++++++ tests/target/array_comment.rs | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tests/source/array_comment.rs create mode 100644 tests/target/array_comment.rs diff --git a/src/lists.rs b/src/lists.rs index c415032ad67..0e3baa7c1e1 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 00000000000..87372b2793f --- /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 00000000000..93e1f5f4034 --- /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, +];