Skip to content

Commit a2f8617

Browse files
csmoetopecongiro
authored andcommitted
fix adds a trailing comma to struct-like macro (#2490)
* fix adds a trailing comma to struct-like macro
1 parent 06d509c commit a2f8617

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

src/expr.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use chains::rewrite_chain;
2020
use closures;
2121
use codemap::{LineRangeUtils, SpanUtils};
2222
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
23-
rewrite_comment, rewrite_missing_comment, FindUncommented};
23+
rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented};
2424
use config::{Config, ControlBraceStyle, IndentStyle};
2525
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
2626
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting, ListItem, Separator};
@@ -2411,20 +2411,20 @@ pub fn wrap_args_with_parens(
24112411
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
24122412
/// comma from macro can potentially break the code.
24132413
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
2414-
let mut encountered_closing_paren = false;
2415-
for c in context.snippet(span).chars().rev() {
2414+
let mut result: bool = Default::default();
2415+
let mut prev_char: char = Default::default();
2416+
2417+
for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
24162418
match c {
2417-
',' => return true,
2418-
')' => if encountered_closing_paren {
2419-
return false;
2420-
} else {
2421-
encountered_closing_paren = true;
2422-
},
2423-
_ if c.is_whitespace() => continue,
2424-
_ => return false,
2419+
_ if kind.is_comment() || c.is_whitespace() => continue,
2420+
')' | '}' => result = result && prev_char != c,
2421+
',' => result = true,
2422+
_ => result = false,
24252423
}
2424+
prev_char = c;
24262425
}
2427-
false
2426+
2427+
result
24282428
}
24292429

24302430
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
@@ -2608,7 +2608,20 @@ fn rewrite_struct_lit<'a>(
26082608

26092609
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
26102610
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
2611-
let fmt = struct_lit_formatting(nested_shape, tactic, context, base.is_some());
2611+
2612+
let ends_with_comma = span_ends_with_comma(context, span);
2613+
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
2614+
true
2615+
} else {
2616+
false
2617+
};
2618+
2619+
let fmt = struct_lit_formatting(
2620+
nested_shape,
2621+
tactic,
2622+
context,
2623+
force_no_trailing_comma || base.is_some(),
2624+
);
26122625

26132626
write_list(&item_vec, &fmt)?
26142627
};

tests/source/issue-2445.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
test!(RunPassPretty {
2+
// comment
3+
path: "src/test/run-pass/pretty",
4+
mode: "pretty",
5+
suite: "run-pass",
6+
default: false,
7+
host: true // should, force, , no trailing comma here
8+
});
9+
10+
test!(RunPassPretty {
11+
// comment
12+
path: "src/test/run-pass/pretty",
13+
mode: "pretty",
14+
suite: "run-pass",
15+
default: false,
16+
host: true, // should, , preserve, the trailing comma
17+
});
18+
19+
test!(Test{
20+
field: i32, // comment
21+
});

tests/target/issue-2445.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
test!(RunPassPretty {
2+
// comment
3+
path: "src/test/run-pass/pretty",
4+
mode: "pretty",
5+
suite: "run-pass",
6+
default: false,
7+
host: true // should, force, , no trailing comma here
8+
});
9+
10+
test!(RunPassPretty {
11+
// comment
12+
path: "src/test/run-pass/pretty",
13+
mode: "pretty",
14+
suite: "run-pass",
15+
default: false,
16+
host: true, // should, , preserve, the trailing comma
17+
});
18+
19+
test!(Test {
20+
field: i32, // comment
21+
});

0 commit comments

Comments
 (0)