-
Notifications
You must be signed in to change notification settings - Fork 924
Formatting adds erroneous comma at end of struct update syntax when missing comma before .. #5604
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
Comments
Thanks for reaching out. Using the latest master Input fn main(){
Outer {
inner: Inner {
a: 1,
b: 2,
}
..Default::default()
};
} Output fn main() {
Outer {
inner: Inner { a: 1, b: 2 }..Default::default(),
};
} From my understanding the expected output is something like: fn main() {
Outer {
inner: Inner { a: 1, b: 2 },
..Default::default(),
};
} |
If we want to stick to pure formatting, the input shouldn't change at all. But if you want to automatically fix this class of mistake (which would be super slick, but maybe too implicit?), the expected output is actually fn main() {
Outer {
inner: Inner { a: 1, b: 2 },
..Default::default()
};
} |
Is it possible that the issue is that the input looks like range syntax? |
@TehPers your hunch is correct. I just dug into this and from the perspective of the AST, which rustfmt operates on, Sadly, I don't think there's an easy fix for this on the rustfmt side 😕 |
I agree. I think there's some ambiguity that's preventing this. For example, how is the following supposed to be formatted? #[derive(Default)]
struct Inner;
struct Outer {
inner: Range<Inner>,
}
fn main() {
let outer = Outer {
// is this supposed to be Range<Inner> or Inner? compiles as Inner
inner: Default::default()
..Default::default()
};
} |
Although I'm not sure that rustfmt is the right place to implement a fix for this, if anyone's interested in coming up with a solution this might be a good place to start since it's where we handle rewriting ranges: Lines 269 to 342 in ee2bed9
|
@alice-i-cecile given that rust-lang/rust#104373 is closed can this be closed too? As mentioned above, the reason why rustfmt produces the formatting that it does is a result of how the rustc_parser parses |
If we do want to try to special case the formatting on the rustfmt side it might be better to look into |
Following up on rust-lang/rust#104373, when using functional record update syntax, if a comma is missed before the
..Default::default()
, rustfmt will then "helpfully" add a comma after the..Default::default()
.When you fix the error with the missing comma, you now have a different error ("no trailing commas after...") that the user did not make, as automatic formatting added a comma in its confusion.
I'm new to this project, but the new error enum variant proposed in rust-lang/rust#104504 may be helpful to identify these cases.
The text was updated successfully, but these errors were encountered: