Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8573df6

Browse files
authored
Merge pull request rust-lang#2925 from scampi/issue539
discard trailing blank comments
2 parents f40baaa + 2db2327 commit 8573df6

File tree

7 files changed

+74
-33
lines changed

7 files changed

+74
-33
lines changed

src/comment.rs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ impl<'a> CommentStyle<'a> {
9696
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
9797
(self.opener(), self.closer(), self.line_start())
9898
}
99-
100-
pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
101-
match *self {
102-
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
103-
line.trim_left().starts_with(self.line_start().trim_left())
104-
|| comment_style(line, normalize_comments) == *self
105-
}
106-
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
107-
line.trim_left().starts_with(self.closer().trim_left())
108-
|| line.trim_left().starts_with(self.line_start().trim_left())
109-
|| comment_style(line, normalize_comments) == *self
110-
}
111-
CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
112-
}
113-
}
11499
}
115100

116101
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
@@ -273,19 +258,56 @@ fn identify_comment(
273258
is_doc_comment: bool,
274259
) -> Option<String> {
275260
let style = comment_style(orig, false);
276-
let first_group = orig
277-
.lines()
278-
.take_while(|l| style.line_with_same_comment_style(l, false))
279-
.collect::<Vec<_>>()
280-
.join("\n");
281-
let rest = orig
282-
.lines()
283-
.skip(first_group.lines().count())
284-
.collect::<Vec<_>>()
285-
.join("\n");
261+
let mut first_group_ending = 0;
286262

263+
fn compute_len(orig: &str, line: &str) -> usize {
264+
if orig.len() > line.len() {
265+
if orig.as_bytes()[line.len()] == b'\r' {
266+
line.len() + 2
267+
} else {
268+
line.len() + 1
269+
}
270+
} else {
271+
line.len()
272+
}
273+
}
274+
275+
match style {
276+
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
277+
let line_start = style.line_start().trim_left();
278+
for line in orig.lines() {
279+
if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
280+
first_group_ending += compute_len(&orig[first_group_ending..], line);
281+
} else {
282+
break;
283+
}
284+
}
285+
}
286+
CommentStyle::Custom(opener) => {
287+
let trimmed_opener = opener.trim_right();
288+
for line in orig.lines() {
289+
if line.trim_left().starts_with(trimmed_opener) {
290+
first_group_ending += compute_len(&orig[first_group_ending..], line);
291+
} else {
292+
break;
293+
}
294+
}
295+
}
296+
// for a block comment, search for the closing symbol
297+
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
298+
let closer = style.closer().trim_left();
299+
for line in orig.lines() {
300+
first_group_ending += compute_len(&orig[first_group_ending..], line);
301+
if line.trim_left().ends_with(closer) {
302+
break;
303+
}
304+
}
305+
}
306+
}
307+
308+
let (first_group, rest) = orig.split_at(first_group_ending);
287309
let first_group_str = rewrite_comment_inner(
288-
&first_group,
310+
first_group,
289311
block_style,
290312
style,
291313
shape,
@@ -295,7 +317,7 @@ fn identify_comment(
295317
if rest.is_empty() {
296318
Some(first_group_str)
297319
} else {
298-
identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
320+
identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
299321
format!(
300322
"{}\n{}{}",
301323
first_group_str,
@@ -427,8 +449,12 @@ fn rewrite_comment_inner(
427449
}
428450
} else if is_prev_line_multi_line && !line.is_empty() {
429451
result.push(' ')
430-
} else if is_last && !closer.is_empty() && line.is_empty() {
431-
result.push_str(&indent_str);
452+
} else if is_last && line.is_empty() {
453+
// trailing blank lines are unwanted
454+
if !closer.is_empty() {
455+
result.push_str(&indent_str);
456+
}
457+
break;
432458
} else {
433459
result.push_str(&comment_line_separator);
434460
if !has_leading_whitespace && result.ends_with(' ') {

src/formatting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ fn format_project<T: FormatHandler>(
8787
if (config.skip_children() && path != main_file) || config.ignore().skip_file(&path) {
8888
continue;
8989
}
90-
should_emit_verbose(!input_is_stdin, config, || println!("Formatting {}", path));
90+
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
9191
let is_root = path == main_file;
9292
context.format_file(path, module, is_root)?;
9393
}
9494
timer = timer.done_formatting();
9595

96-
should_emit_verbose(!input_is_stdin, config, || {
96+
should_emit_verbose(input_is_stdin, config, || {
9797
println!(
9898
"Spent {0:.3} secs in the parsing phase, and {1:.3} secs in the formatting phase",
9999
timer.get_parse_time(),
@@ -611,7 +611,7 @@ fn parse_crate(
611611
// Note that if you see this message and want more information,
612612
// then run the `parse_crate_mod` function above without
613613
// `catch_unwind` so rustfmt panics and you can get a backtrace.
614-
should_emit_verbose(!input_is_stdin, config, || {
614+
should_emit_verbose(input_is_stdin, config, || {
615615
println!("The Rust parser panicked")
616616
});
617617
}

tests/source/issue-539.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-normalize_comments: true
2+
/*
3+
FIXME (#3300): Should allow items to be anonymous. Right now
4+
we just use dummy names for anon items.
5+
*/

tests/source/issue-683.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-normalize_comments: true
2+
/*
3+
* FIXME (#3300): Should allow items to be anonymous. Right now
4+
* we just use dummy names for anon items.
5+
*/

tests/target/doc-comment-with-example.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
/// # #![cfg_attr(not(dox), no_std)]
99
/// fn foo() {}
1010
/// ```
11-
///
1211
fn foo() {}

tests/target/issue-539.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// rustfmt-normalize_comments: true
2+
// FIXME (#3300): Should allow items to be anonymous. Right now
3+
// we just use dummy names for anon items.

tests/target/issue-683.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// rustfmt-normalize_comments: true
2+
// FIXME (#3300): Should allow items to be anonymous. Right now
3+
// we just use dummy names for anon items.

0 commit comments

Comments
 (0)