-
Notifications
You must be signed in to change notification settings - Fork 933
fix adds a trailing comma to struct-like macro #2490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix adds a trailing comma to struct-like macro #2490
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your PR! And sorry about the late review.
The added test is wrong, it could be that span_ends_with_comma
does not work with comments (I added it, sorry!). Would you please update this PR to fix it?
}); | ||
|
||
test!(Test { | ||
field: i32 // comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test seems wrong: we need to preserve a trailing comma if the macro originally has it.
rustfmt-core/src/expr.rs
Outdated
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing | ||
/// comma from macro can potentially break the code. | ||
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool { | ||
let mut encountered_closing_paren = false; | ||
let mut encountered_closing_braces = false; | ||
for c in context.snippet(span).chars().rev() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please try using CharClasses
here?
And this PR needs rebase. Sorry! |
@csmoe I think the rebase went wrong. Within the branch |
21916b9
to
5df80f2
Compare
…com/csmoe/rustfmt into struct_macro_trailing_comma" This reverts commit 21916b9.
5df80f2
to
73b06bb
Compare
src/expr.rs
Outdated
#[allow(dead_code)] | ||
/// Check if a struct representation ends with comma by match on the chars of struct | ||
/// snippet reversely, once a FullCodeCharKind::Normal comma matched, returns true | ||
fn struct_lit_ends_with_comma(context: &RewriteContext, span: Span) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a function to check if a struct lit ends with comma here, but I need to match the comma reversely because normally match might never handle the last field before it returns.
I had tried to impl DoubleEndedIterator for CharClasses {...}
, the basic next_back
method is easy to learn from std-docs, but it's a bit difficult for me to tackle the method on CharClasses
, could you please give me some tips the deal with it? @topecongiro
Input
Output(before)
After
|
10e808b
to
a4cbcb9
Compare
@csmoe Thank you for your update! And my apologies that I could not give you a feedback in a timely manner. I am afraid that using I think that you do not need to implement let mut result;
let mut prev_char;
for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
match c {
_ if kind.is_comment() || c.is_whitespace() => continue,
')' | '}' => result = result && prev_char != c,
',' => result = true,
_ => result = false,
}
prev_char = c;
}
result |
@topecongiro Comma in comment fixed. Thank for your patience on guiding me here and sorry for bothering you with the annoying errors in those undeliberate commits. I have learned some tips to write a proper test case and skills to tackle string scanning, Thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the update! I left two inline comments about minor updates, otherwise LGTM.
src/expr.rs
Outdated
@@ -7,7 +7,6 @@ | |||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |||
// option. This file may not be copied, modified, or distributed | |||
// except according to those terms. | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please bring back an empty line here?
src/lib.rs
Outdated
@@ -12,6 +12,7 @@ | |||
#![feature(decl_macro)] | |||
#![feature(match_default_bindings)] | |||
#![feature(type_ascription)] | |||
#![feature(string_retain)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please remove this unused feature?
Thank you! |
should resolve #2445
Input
Output(before)
After