-
Notifications
You must be signed in to change notification settings - Fork 899
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
If-chain wrapping when use_small_heuristics = "Max"
#5948
Comments
Thanks for opening the issue. I Just wanted to double check if this was also a case you'd expect to be formatted on a single line even though there's more than one if let 1 = 1 && let 2 = 2 && true {
} Or in this case would this be preferred: if let 1 = 1
&& let 2 = 2
&& true
{
} |
For me personally, I would expect that |
I think that it'd be best to take a step back and think about desired formatting outcomes vs. a specific means to that end.
let-chains are, at least from an AST perspective, just a binary expression with a let expr and some other things contained within. Those are formatted according to their own respective rules and configurations, and i.e. single_line_if_else_max_width only comes into the picture if the what should come first, imo, is a config option that controls the behavior of the "inner" let chain, which could then be wired up into |
perhaps more succinctly and with an example: if fooooooooooooooooooooooooooooooooo(really_really_really_really_really_long_ident, some_other_call(), "long string ", "another llllllllllllllllllllllllllllooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggg string") { .. } else { ..} that can't go on one line just because the only difference here is that instead of a function call we've got the let chain/binary expression, and atm, the default formatting requires that to be multilined, and we've got no configuration option that allows users to do something different |
Agreed on this. I have no particular suggestion for the name of the option, but it makes sense to me that there would be an option specifically for choosing between different flavors of if-chain formatting, and |
thogitha want big smash |
Also agreed with @calebcartwright and in the interest of being more clear and explicit about my expectations: I expect that there should be an option (maybe a yet-to-be-invented option) that would allow |
@ytmimi what I think we should do from the rustfmt team side is either implement (or guide someone in the implementation of) the foundation/option to control this (and fwiw i'd be fine with something as vague as "let_chain_style"), naturally with the default set to where the style guide appears to be heading. that will allow others to suggest/implement other variants, with hopefully relatively minimal input from our end the only concern/question i have from a design perspective is whether that'll suffice. in the style team meetings we basically went through all the different kinds of expressions and weighed whether each could be included in the "allowed to stay single line" list i.e. what if someone is fine with single line formatting for calls, but only calls that had 0 args. but then what if someone else wants to control the number of args, or the types of args, and then... I think you start to get the picture 😄 i think our lens should primarily be: how can we provide some level of control over the wrapping behavior, without grotesquely complicating the config option surface and internal codebase |
Well this one has to be multilined anyway because it literally doesn't fit. It's if ({ let x = true; x }) {
...
} |
re: #5948 (comment) yes, thanks for the correction @digama0 . I was rushing and provided a bad example 😅 the point i was trying to make is that |
@calebcartwright I just opened #5983 to add |
This is a follow up to rust-lang/style-team#169 (comment). Currently, rustfmt is formatting if-chains on nightly, but it uses a line-break-heavy style that is the current front-runner on rust-lang/style-team#169 and other popular styles also exist. In particular,
use_small_heuristics = "Max"
is IMO a strong indication of intent from the author to not have spurious line breaks, and rustfmt should instead format let chains the same as regular binary operators, filling the line if space permits. (An option specific to if-chains is also reasonable, butuse_small_heuristics = "Max"
should imply it.)before:
after:
This relates to a discussion on Zulip.
The text was updated successfully, but these errors were encountered: