From e0dacad2460319924818d28418b88ab89f6bd698 Mon Sep 17 00:00:00 2001 From: Matthew Hughes Date: Fri, 9 Oct 2026 18:03:42 +0200 Subject: [PATCH 1/2] Rename `line_start` to `comment_prefix` This naming was a bit confusing: I would expect a 'line start' of a comment to include any leading indent etc., e.g. for the comments in: // top level comment fn f() { // function comment ... } I would expect: * `line_start`: `// `, and * `line_start`: ` // ` Rather than both to be `// `. --- src/comment.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 43e8e64a7a9..3ce2c3bbafe 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -94,7 +94,7 @@ impl<'a> CommentStyle<'a> { } } - pub(crate) fn line_start(&self) -> &'a str { + pub(crate) fn comment_prefix(&self) -> &'a str { match *self { CommentStyle::DoubleSlash => "// ", CommentStyle::TripleSlash => "/// ", @@ -107,7 +107,7 @@ impl<'a> CommentStyle<'a> { } pub(crate) fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) { - (self.opener(), self.closer(), self.line_start()) + (self.opener(), self.closer(), self.comment_prefix()) } } @@ -306,7 +306,7 @@ fn identify_comment( let (has_bare_lines, first_group_ending) = match style { CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => { - let line_start = style.line_start().trim_start(); + let line_start = style.comment_prefix().trim_start(); consume_same_line_comments(style, orig, line_start) } CommentStyle::Custom(opener) => { @@ -583,7 +583,7 @@ struct CommentRewrite<'a> { opener: String, closer: String, - line_start: String, + comment_prefix: String, style: CommentStyle<'a>, } @@ -633,7 +633,7 @@ impl<'a> CommentRewrite<'a> { opener: opener.to_owned(), closer: closer.to_owned(), - line_start: line_start.to_owned(), + comment_prefix: line_start.to_owned(), style, }; cr.result.push_str(opener); @@ -883,7 +883,7 @@ impl<'a> CommentRewrite<'a> { self.fmt.shape = if self.is_prev_line_multi_line { // 1 = " " let offset = 1 + last_line_width(&self.result, self.fmt.config.tab_spaces()) - - self.line_start.len(); + - self.comment_prefix.len(); Shape { width: self.max_width.saturating_sub(offset), indent: self.fmt_indent, From ded31f4664a5ce8eab0f59129edd0bcff1031fe4 Mon Sep 17 00:00:00 2001 From: Matthew Hughes Date: Fri, 9 Oct 2026 17:53:52 +0200 Subject: [PATCH 2/2] Fix comments not taking full width when combined By excluding the width of the indent when combining comments, since `last_line_width` includes this. Otherwise e.g. when formatting: V------V (indent) (end of line) ---V // some comment with final word // new line fits word We'd would compute the offset using: last_line_width = " // word".len()` (i.e. indent+comment prefix+content), then subtract only the width of the comment prefix (and `" "`) and incorrectly conclude that won't fit if combined with the following comment. References: rust-lang/rustfmt#7167 --- src/comment.rs | 3 ++- tests/source/comment_wrapping.rs | 18 ++++++++++++++++++ tests/target/comment_wrapping.rs | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/source/comment_wrapping.rs create mode 100644 tests/target/comment_wrapping.rs diff --git a/src/comment.rs b/src/comment.rs index 3ce2c3bbafe..f9875d6e4b1 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -883,7 +883,8 @@ impl<'a> CommentRewrite<'a> { self.fmt.shape = if self.is_prev_line_multi_line { // 1 = " " let offset = 1 + last_line_width(&self.result, self.fmt.config.tab_spaces()) - - self.comment_prefix.len(); + - self.comment_prefix.len() + - self.fmt_indent.width(); Shape { width: self.max_width.saturating_sub(offset), indent: self.fmt_indent, diff --git a/tests/source/comment_wrapping.rs b/tests/source/comment_wrapping.rs new file mode 100644 index 00000000000..ba892a256d2 --- /dev/null +++ b/tests/source/comment_wrapping.rs @@ -0,0 +1,18 @@ +// rustfmt-wrap_comments: true +// rustfmt-comment_width: 100 + +fn f() { + // Note: off-by-one bug: the 100 width wide line should not be formatted + // https://github.com/rust-lang/rustfmt/issues/7167 + foo( + // this comment is 100 characters wide! The last word should fit on the following line -- aa + // This line is 92 characters wide ------------------------------------------------- + 1, + ); + + // this comment should be left alone + // this comment is 100 characters wide! -------------------------------------------------------- + + // this comment is 101 characters wide! The last word should fit on the following line ------- aa + // This line is 92 characters wide ---------------------------------------------------- +} diff --git a/tests/target/comment_wrapping.rs b/tests/target/comment_wrapping.rs new file mode 100644 index 00000000000..bab45fb32fa --- /dev/null +++ b/tests/target/comment_wrapping.rs @@ -0,0 +1,18 @@ +// rustfmt-wrap_comments: true +// rustfmt-comment_width: 100 + +fn f() { + // Note: off-by-one bug: the 100 width wide line should not be formatted + // https://github.com/rust-lang/rustfmt/issues/7167 + foo( + // this comment is 100 characters wide! The last word should fit on the following line -- + // aa This line is 92 characters wide ------------------------------------------------- + 1, + ); + + // this comment should be left alone + // this comment is 100 characters wide! -------------------------------------------------------- + + // this comment is 101 characters wide! The last word should fit on the following line ------- + // aa This line is 92 characters wide ---------------------------------------------------- +}