Skip to content

Commit 8829e45

Browse files
authored
Fix comment removing issue of unit types in struct fields (#4766)
* Fix comment removing issue of unit types in struct fields * Cleanup nits
1 parent 8d5f742 commit 8829e45

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Diff for: src/formatting/overflow.rs

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_span::Span;
1010
use crate::config::lists::*;
1111
use crate::formatting::{
1212
closures,
13+
comment::{comment_style, contains_comment, rewrite_missing_comment},
1314
expr::{
1415
can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call,
1516
is_simple_expr, rewrite_cond,
@@ -588,6 +589,12 @@ impl<'a> Context<'a> {
588589

589590
fn rewrite_items(&self) -> Option<(bool, String)> {
590591
let span = self.items_span();
592+
if self.items.is_empty() && contains_comment(self.context.snippet(span)) {
593+
let hi = self.context.snippet_provider.span_before(span, self.suffix);
594+
let cmnt = rewrite_missing_comment(span.with_hi(hi), self.nested_shape, self.context)?;
595+
let cmnt_style = comment_style(&cmnt, self.context.config.normalize_comments());
596+
return Some((!cmnt.contains('\n') && cmnt_style.is_block_comment(), cmnt));
597+
}
591598
let items = itemize_list(
592599
self.context.snippet_provider,
593600
self.items.iter(),

Diff for: tests/source/issue-4749.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct S {
2+
x: (/* type */),
3+
}
4+
5+
struct S2((/* type */));
6+
7+
struct S3((/* type
8+
2 */));
9+
10+
struct S4((
11+
// Line comment
12+
));
13+
14+
enum E {
15+
Variant1 {
16+
x: (/* type */),
17+
},
18+
Variant2((/* type */)),
19+
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct S {
2+
x: (/* type */),
3+
}
4+
5+
struct S2((/* type */));
6+
7+
struct S3(
8+
(
9+
/* type
10+
2 */
11+
),
12+
);
13+
14+
struct S4(
15+
(
16+
// Line comment
17+
),
18+
);
19+
20+
enum E {
21+
Variant1 { x: (/* type */) },
22+
Variant2((/* type */)),
23+
}

0 commit comments

Comments
 (0)