Skip to content

Commit 18737dd

Browse files
pan93412ytmimi
andauthored
Prefer light_rewrite_comment if it is not a doccomment (#5536)
Fixes 5533, 5568, 5907 Prevent `format_code_in_doc_comments=true` from affecting how non-doc comments are formatted. --------- Co-authored-by: Yacin Tmimi <[email protected]>
1 parent 262feb3 commit 18737dd

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Diff for: src/comment.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ fn identify_comment(
365365
trim_left_preserve_layout(first_group, shape.indent, config)?
366366
} else if !config.normalize_comments()
367367
&& !config.wrap_comments()
368-
&& !config.format_code_in_doc_comments()
368+
&& !(
369+
// `format_code_in_doc_comments` should only take effect on doc comments,
370+
// so we only consider it when this comment block is a doc comment block.
371+
is_doc_comment && config.format_code_in_doc_comments()
372+
)
369373
{
370374
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
371375
} else {

Diff for: tests/target/issue-5568.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-max_width: 119
2+
// rustfmt-format_code_in_doc_comments: true
3+
4+
mod libs {
5+
fn mrbgems_sources() {
6+
[
7+
"mrbgems/mruby-compiler/core/codegen.c", // Ruby parser and bytecode generation
8+
"mrbgems/mruby-compiler/core/y.tab.c", // Ruby parser and bytecode generation
9+
"mrbgems/mruby-metaprog/src/metaprog.c", // APIs on Kernel and Module for accessing classes and variables
10+
"mrbgems/mruby-method/src/method.c", // `Method`, `UnboundMethod`, and method APIs on Kernel and Module
11+
"mrbgems/mruby-pack/src/pack.c", // Array#pack and String#unpack
12+
]
13+
}
14+
}

Diff for: tests/target/issue_5533.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
struct TestStruct {
4+
position_currency: String, // Currency for position of this contract. If not null, 1 contract = 1 positionCurrency.
5+
pu: Option<i64>, // Previous event update sequense ("u" of previous message), -1 also means None
6+
}

Diff for: tests/target/issue_5907.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
// ```
4+
// [
5+
// ]
6+
// ```

0 commit comments

Comments
 (0)