Skip to content

Commit aa82620

Browse files
committed
Fix formatting of comments in empty structs
1 parent 6495024 commit aa82620

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/items.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,12 +1426,22 @@ fn format_empty_struct_or_tuple(
14261426
Some(ref s) if s.is_empty() => (),
14271427
Some(ref s) => {
14281428
if !is_single_line(s) || first_line_contains_single_line_comment(s) {
1429-
let nested_indent_str = offset
1430-
.block_indent(context.config)
1431-
.to_string_with_newline(context.config);
1432-
result.push_str(&nested_indent_str);
1429+
// Indent every comment line
1430+
for l in s.lines() {
1431+
if l.trim_start().starts_with("//") {
1432+
let nested_indent_str = offset
1433+
.block_indent(context.config)
1434+
.to_string_with_newline(context.config);
1435+
result.push_str(&nested_indent_str);
1436+
} else {
1437+
result.push('\n');
1438+
}
1439+
result.push_str(l);
1440+
}
1441+
} else {
1442+
result.push_str(s);
14331443
}
1434-
result.push_str(s);
1444+
14351445
if last_line_contains_single_line_comment(s) {
14361446
result.push_str(&offset.to_string_with_newline(context.config));
14371447
}

tests/source/structs.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,20 @@ struct Test {
296296
// #2818
297297
struct Paren((i32)) where i32: Trait;
298298
struct Parens((i32, i32)) where i32: Trait;
299+
300+
// #4854
301+
struct Comments {
302+
// Hello, this is my
303+
// struct where I haven't added
304+
// any fields yet.
305+
// This is weird
306+
}
307+
308+
struct CommentsTup(
309+
// Ok comment
310+
// Very good
311+
// Don't format me pls
312+
313+
// With break
314+
// Yes
315+
);

tests/target/structs.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,20 @@ where
356356
struct Parens((i32, i32))
357357
where
358358
i32: Trait;
359+
360+
// #4854
361+
struct Comments {
362+
// Hello, this is my
363+
// struct where I haven't added
364+
// any fields yet.
365+
// This is weird
366+
}
367+
368+
struct CommentsTup(
369+
// Ok comment
370+
// Very good
371+
// Don't format me pls
372+
373+
// With break
374+
// Yes
375+
);

0 commit comments

Comments
 (0)