-
Notifications
You must be signed in to change notification settings - Fork 926
Wrap cast expr in parens when the ty ends with empty angle brackets #5386
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
base: master
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
To expand on what I was alluding to in #4621 (comment), I don't think we can necessarily solve this problem at the cast expression level. Even with casts, the issue only occurs when it is in the lhs operand position of a bin expression which also has the less than/less than equal to operators. For example, Additionally, I'm not sure if there's other scenarios/expr variants in the lhs position where this could occur too |
From what I can tell there are only three binary operator that starts with a less than (
|
ast::ExprKind::Binary(op, ref lhs, ref rhs) => { | |
// FIXME: format comments between operands and operator | |
rewrite_all_pairs(expr, shape, context).or_else(|| { | |
rewrite_pair( | |
&**lhs, | |
&**rhs, | |
PairParts::infix(&format!(" {} ", context.snippet(op.span))), | |
context, | |
shape, | |
context.config.binop_separator(), | |
) | |
}) | |
} |
Lines 251 to 256 in 33c6074
impl FlattenPair for ast::Expr { | |
fn flatten( | |
&self, | |
context: &RewriteContext<'_>, | |
shape: Shape, | |
) -> Option<PairList<'_, '_, ast::Expr>> { |
The needle we have to try to thread at this point is solving for the case that's an issue without applying breaking formatting changes in unnecessary cases (and yes this would qualify as a breaking change in those unnecessary cases). If we could go back in time and establish this as the formatting approach from day 1 I'd definitely agree this would be the right way to go. However, we've got a lot of historical context to account for. I'm not sure what the best path forward is, but given the rarity of the problematic scenario and the multiple viable workarounds, this isn't something we should try to push a broader change for in order to get an easier fix to the more narrow scenario |
I totally understand that this would be a breaking change for those cases where things already work out just fine. I'll continue to explore what options we have to fix this! |
Sounds good. This isn't a terribly urgent item, so what would you think about us closing for now and we can revisit if you come up with a different solution? |
I think I'd want to hack on this at least through the weekend, and if I can't make any meaningful progress then I'd be happy to close this and revisit it at a later time. |
Alright, I went ahead and moved the fix up to the Binary expression level. I plan on adding a test to cover the |
Fixes 4621 Previously rustfmt would remove empty generic args from cast expressions e.g. `x as i32<>` => `x as i32`. This lead to code that could not compile when the cast occurred in the context of a binary expression where the operand was a less than (`<`) or left shift (`<<`). The advice from the parse error emitted by the compiler was to wrap the cast in parentheses, which is now the behavior rustfmt employs. * `x as i32<> < y` => `(x as i32) < y` * `x as i32<> << y` => `(x as i32) << y`
Fixes #4621
Previously rustfmt would remove empty angle brackets from cast expressions e.g.
x as i32<>
=>x as i32
. This lead to code that could not compile when the cast occurred in an if statement.The advice from the compiler was to wrap the cast in parentheses, which is now the behavior rustfmt employs.