fix adds a trailing comma to struct-like macro (#2490)
* fix adds a trailing comma to struct-like macro
This commit is contained in:
parent
06d509c25c
commit
a2f861730e
3 changed files with 68 additions and 13 deletions
39
src/expr.rs
39
src/expr.rs
|
@ -20,7 +20,7 @@ use chains::rewrite_chain;
|
||||||
use closures;
|
use closures;
|
||||||
use codemap::{LineRangeUtils, SpanUtils};
|
use codemap::{LineRangeUtils, SpanUtils};
|
||||||
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
|
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
|
||||||
rewrite_comment, rewrite_missing_comment, FindUncommented};
|
rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented};
|
||||||
use config::{Config, ControlBraceStyle, IndentStyle};
|
use config::{Config, ControlBraceStyle, IndentStyle};
|
||||||
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
|
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
|
||||||
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting, ListItem, Separator};
|
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting, ListItem, Separator};
|
||||||
|
@ -2411,20 +2411,20 @@ pub fn wrap_args_with_parens(
|
||||||
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
|
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
|
||||||
/// comma from macro can potentially break the code.
|
/// comma from macro can potentially break the code.
|
||||||
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
|
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
|
||||||
let mut encountered_closing_paren = false;
|
let mut result: bool = Default::default();
|
||||||
for c in context.snippet(span).chars().rev() {
|
let mut prev_char: char = Default::default();
|
||||||
|
|
||||||
|
for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
|
||||||
match c {
|
match c {
|
||||||
',' => return true,
|
_ if kind.is_comment() || c.is_whitespace() => continue,
|
||||||
')' => if encountered_closing_paren {
|
')' | '}' => result = result && prev_char != c,
|
||||||
return false;
|
',' => result = true,
|
||||||
} else {
|
_ => result = false,
|
||||||
encountered_closing_paren = true;
|
|
||||||
},
|
|
||||||
_ if c.is_whitespace() => continue,
|
|
||||||
_ => return false,
|
|
||||||
}
|
}
|
||||||
|
prev_char = c;
|
||||||
}
|
}
|
||||||
false
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
|
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
|
||||||
|
@ -2608,7 +2608,20 @@ fn rewrite_struct_lit<'a>(
|
||||||
|
|
||||||
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
|
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
|
||||||
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
|
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
|
||||||
let fmt = struct_lit_formatting(nested_shape, tactic, context, base.is_some());
|
|
||||||
|
let ends_with_comma = span_ends_with_comma(context, span);
|
||||||
|
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
let fmt = struct_lit_formatting(
|
||||||
|
nested_shape,
|
||||||
|
tactic,
|
||||||
|
context,
|
||||||
|
force_no_trailing_comma || base.is_some(),
|
||||||
|
);
|
||||||
|
|
||||||
write_list(&item_vec, &fmt)?
|
write_list(&item_vec, &fmt)?
|
||||||
};
|
};
|
||||||
|
|
21
tests/source/issue-2445.rs
Normal file
21
tests/source/issue-2445.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
test!(RunPassPretty {
|
||||||
|
// comment
|
||||||
|
path: "src/test/run-pass/pretty",
|
||||||
|
mode: "pretty",
|
||||||
|
suite: "run-pass",
|
||||||
|
default: false,
|
||||||
|
host: true // should, force, , no trailing comma here
|
||||||
|
});
|
||||||
|
|
||||||
|
test!(RunPassPretty {
|
||||||
|
// comment
|
||||||
|
path: "src/test/run-pass/pretty",
|
||||||
|
mode: "pretty",
|
||||||
|
suite: "run-pass",
|
||||||
|
default: false,
|
||||||
|
host: true, // should, , preserve, the trailing comma
|
||||||
|
});
|
||||||
|
|
||||||
|
test!(Test{
|
||||||
|
field: i32, // comment
|
||||||
|
});
|
21
tests/target/issue-2445.rs
Normal file
21
tests/target/issue-2445.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
test!(RunPassPretty {
|
||||||
|
// comment
|
||||||
|
path: "src/test/run-pass/pretty",
|
||||||
|
mode: "pretty",
|
||||||
|
suite: "run-pass",
|
||||||
|
default: false,
|
||||||
|
host: true // should, force, , no trailing comma here
|
||||||
|
});
|
||||||
|
|
||||||
|
test!(RunPassPretty {
|
||||||
|
// comment
|
||||||
|
path: "src/test/run-pass/pretty",
|
||||||
|
mode: "pretty",
|
||||||
|
suite: "run-pass",
|
||||||
|
default: false,
|
||||||
|
host: true, // should, , preserve, the trailing comma
|
||||||
|
});
|
||||||
|
|
||||||
|
test!(Test {
|
||||||
|
field: i32, // comment
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue