1
Fork 0

Use BinOpKind instead of BinOp for function args where possible.

Because it's nice to avoid passing in unnecessary data.
This commit is contained in:
Nicholas Nethercote 2024-12-20 08:33:43 +11:00
parent 076cccd412
commit ac8ccf09b4
7 changed files with 77 additions and 78 deletions

View file

@ -274,22 +274,22 @@ impl<'a> State<'a> {
fn print_expr_binary(
&mut self,
op: ast::BinOp,
op: ast::BinOpKind,
lhs: &ast::Expr,
rhs: &ast::Expr,
fixup: FixupContext,
) {
let binop_prec = op.node.precedence();
let binop_prec = op.precedence();
let left_prec = lhs.precedence();
let right_prec = rhs.precedence();
let (mut left_needs_paren, right_needs_paren) = match op.node.fixity() {
let (mut left_needs_paren, right_needs_paren) = match op.fixity() {
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),
};
match (&lhs.kind, op.node) {
match (&lhs.kind, op) {
// These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is
// the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead
// of `(x as i32) < ...`. We need to convince it _not_ to do that.
@ -312,7 +312,7 @@ impl<'a> State<'a> {
self.print_expr_cond_paren(lhs, left_needs_paren, fixup.leftmost_subexpression());
self.space();
self.word_space(op.node.as_str());
self.word_space(op.as_str());
self.print_expr_cond_paren(rhs, right_needs_paren, fixup.subsequent_subexpression());
}
@ -410,7 +410,7 @@ impl<'a> State<'a> {
self.print_expr_method_call(seg, receiver, args, fixup);
}
ast::ExprKind::Binary(op, lhs, rhs) => {
self.print_expr_binary(*op, lhs, rhs, fixup);
self.print_expr_binary(op.node, lhs, rhs, fixup);
}
ast::ExprKind::Unary(op, expr) => {
self.print_expr_unary(*op, expr, fixup);