diff --git a/src/comment.rs b/src/comment.rs index f9d8a0fa70c..1f1372269d4 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -865,6 +865,7 @@ fn rewrite_comment_inner( is_doc_comment: bool, ) -> Option { let mut rewriter = CommentRewrite::new(orig, block_style, shape, config); + let is_line_comment = rewriter.style.is_line_comment(); let line_breaks = count_newlines(orig.trim_end()); let lines = orig @@ -873,7 +874,11 @@ fn rewrite_comment_inner( .map(|(i, mut line)| { line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment); // Drop old closer. - if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") { + // When converting block comments to line comments always remove block comment closers + if (i == line_breaks || is_line_comment) + && line.ends_with("*/") + && !line.starts_with("//") + { line = line[..(line.len() - 2)].trim_end(); } diff --git a/tests/source/issue-4971/normalize_comments_true.rs b/tests/source/issue-4971/normalize_comments_true.rs new file mode 100644 index 00000000000..93497c17cd2 --- /dev/null +++ b/tests/source/issue-4971/normalize_comments_true.rs @@ -0,0 +1,23 @@ +// rustfmt-normalize_comments:true + +struct A { + x: usize, + /* protected int[] observed; */ + /* (int, int)[] stack; */ +} + +struct B { + x: usize, + /* protected int[] observed; + * some more details here */ + /* (int, int)[] stack; */ +} + +struct C { + x: usize, + /* protected int[] observed; + * some more details here; + /* nested comment; */ + */ + /* (int, int)[] stack; */ +} diff --git a/tests/target/issue-4971/normalize_comments_false.rs b/tests/target/issue-4971/normalize_comments_false.rs new file mode 100644 index 00000000000..c09d3e407c2 --- /dev/null +++ b/tests/target/issue-4971/normalize_comments_false.rs @@ -0,0 +1,23 @@ +// rustfmt-normalize_comments:false + +struct A { + x: usize, + /* protected int[] observed; */ + /* (int, int)[] stack; */ +} + +struct B { + x: usize, + /* protected int[] observed; + * some more details here */ + /* (int, int)[] stack; */ +} + +struct C { + x: usize, + /* protected int[] observed; + * some more details here; + /* nested comment; */ + */ + /* (int, int)[] stack; */ +} diff --git a/tests/target/issue-4971/normalize_comments_true.rs b/tests/target/issue-4971/normalize_comments_true.rs new file mode 100644 index 00000000000..cad66c1289c --- /dev/null +++ b/tests/target/issue-4971/normalize_comments_true.rs @@ -0,0 +1,23 @@ +// rustfmt-normalize_comments:true + +struct A { + x: usize, + // protected int[] observed; + // (int, int)[] stack; +} + +struct B { + x: usize, + // protected int[] observed; + // some more details here + // (int, int)[] stack; +} + +struct C { + x: usize, + // protected int[] observed; + // some more details here; + // nested comment; + // + // (int, int)[] stack; +}