Skip to content

Treat empty doc comments that precede doc attributes as significant #5098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,19 @@ fn format_derive(

/// Returns the first group of attributes that fills the given predicate.
/// We consider two doc comments are in different group if they are separated by normal comments.
fn take_while_with_pred<'a, P>(
fn take_while_with_pred<'a, P, N>(
context: &RewriteContext<'_>,
attrs: &'a [ast::Attribute],
pred: P,
) -> &'a [ast::Attribute]
is_next_significant: N,
) -> (&'a [ast::Attribute], bool)
where
P: Fn(&ast::Attribute) -> bool,
N: Fn(&ast::Attribute, &ast::Attribute) -> bool,
{
let mut len = 0;
let mut iter = attrs.iter().peekable();
let mut next_attr_significant = false;

while let Some(attr) = iter.next() {
if pred(attr) {
Expand All @@ -217,6 +220,7 @@ where
break;
}
if let Some(next_attr) = iter.peek() {
next_attr_significant = is_next_significant(attr, next_attr);
// Extract comments between two attributes.
let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
let snippet = context.snippet(span_between_attr);
Expand All @@ -226,7 +230,7 @@ where
}
}

&attrs[..len]
(&attrs[..len], next_attr_significant)
}

/// Rewrite the any doc comments which come before any other attributes.
Expand All @@ -239,7 +243,20 @@ fn rewrite_initial_doc_comments(
return Some((0, None));
}
// Rewrite doc comments
let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_doc_comment());
// We want to treat trailing empty comments as significant if they precede a doc attribute
// see https://github.com/rust-lang/rustfmt/issues/5073
let (sugared_docs, keep_empty_trailing_comment) = take_while_with_pred(
context,
attrs,
|a| a.is_doc_comment(),
|curr_attr, next_attr| {
let is_curr_doc_comment = curr_attr.is_doc_comment();
let symbol = next_attr.name_or_empty();
let is_next_doc_attr = next_attr.has_name(symbol) && symbol.as_str() == "doc";
let next_immediately_after_current = (next_attr.span.lo() - curr_attr.span.hi()).0 == 1;
is_curr_doc_comment && is_next_doc_attr && next_immediately_after_current
},
);
if !sugared_docs.is_empty() {
let snippet = sugared_docs
.iter()
Expand All @@ -252,6 +269,7 @@ fn rewrite_initial_doc_comments(
&snippet,
shape.comment(context.config),
context.config,
keep_empty_trailing_comment,
)?),
));
}
Expand Down Expand Up @@ -333,7 +351,12 @@ impl Rewrite for ast::Attribute {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let snippet = context.snippet(self.span);
if self.is_doc_comment() {
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
rewrite_doc_comment(
snippet,
shape.comment(context.config),
context.config,
false,
)
} else {
let should_skip = self
.ident()
Expand Down Expand Up @@ -362,6 +385,7 @@ impl Rewrite for ast::Attribute {
&doc_comment,
shape.comment(context.config),
context.config,
false,
);
}
}
Expand Down Expand Up @@ -432,7 +456,7 @@ impl Rewrite for [ast::Attribute] {

// Handle derives if we will merge them.
if context.config.merge_derives() && is_derive(&attrs[0]) {
let derives = take_while_with_pred(context, attrs, is_derive);
let (derives, _) = take_while_with_pred(context, attrs, is_derive, |_, _| false);
let derive_str = format_derive(derives, shape, context)?;
result.push_str(&derive_str);

Expand Down
37 changes: 31 additions & 6 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,20 @@ pub(crate) fn combine_strs_with_missing_comments(
Some(result)
}

pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
identify_comment(orig, false, shape, config, true)
pub(crate) fn rewrite_doc_comment(
orig: &str,
shape: Shape,
config: &Config,
keep_empty_trailing_comment: bool,
) -> Option<String> {
identify_comment(
orig,
false,
shape,
config,
true,
keep_empty_trailing_comment,
)
}

pub(crate) fn rewrite_comment(
Expand All @@ -250,7 +262,7 @@ pub(crate) fn rewrite_comment(
shape: Shape,
config: &Config,
) -> Option<String> {
identify_comment(orig, block_style, shape, config, false)
identify_comment(orig, block_style, shape, config, false, false)
}

fn identify_comment(
Expand All @@ -259,6 +271,7 @@ fn identify_comment(
shape: Shape,
config: &Config,
is_doc_comment: bool,
keep_empty_trailing_comment: bool,
) -> Option<String> {
let style = comment_style(orig, false);

Expand Down Expand Up @@ -365,6 +378,7 @@ fn identify_comment(
shape,
config,
is_doc_comment || style.is_doc_comment(),
keep_empty_trailing_comment,
)?
};
if rest.is_empty() {
Expand All @@ -376,6 +390,7 @@ fn identify_comment(
shape,
config,
is_doc_comment,
keep_empty_trailing_comment,
)
.map(|rest_str| {
format!(
Expand Down Expand Up @@ -618,6 +633,7 @@ impl<'a> CommentRewrite<'a> {
i: usize,
line: &'a str,
has_leading_whitespace: bool,
keep_empty_trailing_comment: bool,
) -> bool {
let is_last = i == count_newlines(orig);

Expand Down Expand Up @@ -700,8 +716,10 @@ impl<'a> CommentRewrite<'a> {
}
} else if self.is_prev_line_multi_line && !line.is_empty() {
self.result.push(' ')
} else if is_last && line.is_empty() {
// trailing blank lines are unwanted
} else if is_last && line.is_empty() && !keep_empty_trailing_comment {
// trailing blank lines are unwanted (in most cases)
// see https://github.com/rust-lang/rustfmt/issues/5073 for an example
// where empty comment lines are significant
if !self.closer.is_empty() {
self.result.push_str(&self.indent_str);
}
Expand Down Expand Up @@ -777,6 +795,7 @@ fn rewrite_comment_inner(
shape: Shape,
config: &Config,
is_doc_comment: bool,
keep_empty_trailing_comment: bool,
) -> Option<String> {
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);

Expand Down Expand Up @@ -806,7 +825,13 @@ fn rewrite_comment_inner(
});

for (i, (line, has_leading_whitespace)) in lines.enumerate() {
if rewriter.handle_line(orig, i, line, has_leading_whitespace) {
if rewriter.handle_line(
orig,
i,
line,
has_leading_whitespace,
keep_empty_trailing_comment,
) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_comments: false

//! Text
//!

#![doc = include_str!("something.md")]
//!
//! More text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_comments: true

//! Text
//!

#![doc = include_str!("something.md")]
//!
//! More text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_comments: false

//! Text
//!

#![doc = include_str!("something.md")]
//!
//! More text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-wrap_comments: true

//! Text

#![doc = include_str!("something.md")]
//!
//! More text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-wrap_comments: false

Copy link
Member

Choose a reason for hiding this comment

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

nit but could you fix the typo in these file names? *_commet_* -> *_comment_*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely not a nit at all! I appreciate you pointing this out and I'll get to fixing it!

//! Text
//!
#![doc = include_str!("something.md")]
//!
//! More text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-wrap_comments: true

//! Text
//!
#![doc = include_str!("something.md")]
//!
//! More text