Skip to content

Commit 6dcbc5d

Browse files
calebcartwrighttopecongiro
authored andcommitted
fix: handle block comments with trailing line comments (rust-lang#3842)
1 parent 8073244 commit 6dcbc5d

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Diff for: src/lists.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,17 @@ where
574574

575575
pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
576576
let trimmed_pre_snippet = pre_snippet.trim();
577-
let has_block_comment = trimmed_pre_snippet.ends_with("*/");
578-
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
579-
if has_block_comment {
577+
// Both start and end are checked to support keeping a block comment inline with
578+
// the item, even if there are preceeding line comments, while still supporting
579+
// a snippet that starts with a block comment but also contains one or more
580+
// trailing single line comments.
581+
// https://github.com/rust-lang/rustfmt/issues/3025
582+
// https://github.com/rust-lang/rustfmt/pull/3048
583+
// https://github.com/rust-lang/rustfmt/issues/3839
584+
let starts_with_block_comment = trimmed_pre_snippet.starts_with("/*");
585+
let ends_with_block_comment = trimmed_pre_snippet.ends_with("*/");
586+
let starts_with_single_line_comment = trimmed_pre_snippet.starts_with("//");
587+
if ends_with_block_comment {
580588
let comment_end = pre_snippet.rfind(|c| c == '/').unwrap();
581589
if pre_snippet[comment_end..].contains('\n') {
582590
(
@@ -589,7 +597,7 @@ pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListIte
589597
ListItemCommentStyle::SameLine,
590598
)
591599
}
592-
} else if has_single_line_comment {
600+
} else if starts_with_single_line_comment || starts_with_block_comment {
593601
(
594602
Some(trimmed_pre_snippet.to_owned()),
595603
ListItemCommentStyle::DifferentLine,

Diff for: tests/source/issue_3839.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Foo {
2+
a: i32,
3+
/*
4+
asd
5+
*/
6+
// foo
7+
b: i32,
8+
}

Diff for: tests/target/issue_3839.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Foo {
2+
a: i32,
3+
/*
4+
asd
5+
*/
6+
// foo
7+
b: i32,
8+
}

0 commit comments

Comments
 (0)