Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => "/// ",
Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -583,7 +583,7 @@ struct CommentRewrite<'a> {

opener: String,
closer: String,
line_start: String,
comment_prefix: String,
style: CommentStyle<'a>,
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.line_start.len();
- self.comment_prefix.len()
- self.fmt_indent.width();
Shape {
width: self.max_width.saturating_sub(offset),
indent: self.fmt_indent,
Expand Down
18 changes: 18 additions & 0 deletions tests/source/comment_wrapping.rs
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +5 to +6

@jieyouxu jieyouxu Oct 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just,

// FIXME(#7167): off-by-one bug: the 100 width wide line should not be formatted

View changes since the review

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 ----------------------------------------------------
}
18 changes: 18 additions & 0 deletions tests/target/comment_wrapping.rs
Original file line number Diff line number Diff line change
@@ -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 ----------------------------------------------------
}
Loading