From ea9f750b31a83ebfd33c7a712c0e395bad5d85fb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 8 Jan 2025 22:49:11 -0800 Subject: [PATCH] Right side of assignment can never require synthetic paren --- src/expr.rs | 25 ++++++++++++++----------- src/fixup.rs | 10 ++-------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 03f243a05..0184821a4 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -3296,12 +3296,10 @@ pub(crate) mod printing { fixup.leftmost_subexpression_with_operator(&e.left, false, false, Precedence::Assign); print_subexpression(&e.left, left_prec <= Precedence::Range, tokens, left_fixup); e.eq_token.to_tokens(tokens); - let (right_prec, right_fixup) = fixup.rightmost_subexpression(&e.right, Precedence::Assign); - print_subexpression( + print_expr( &e.right, - right_prec < Precedence::Assign, tokens, - right_fixup, + fixup.rightmost_subexpression_fixup(false, false, Precedence::Assign), ); } @@ -3370,17 +3368,22 @@ pub(crate) mod printing { #[cfg(feature = "full")] binop_prec, ); + let left_needs_group = match binop_prec { + Precedence::Assign => left_prec <= Precedence::Range, + Precedence::Compare => left_prec <= binop_prec, + _ => left_prec < binop_prec, + }; - let (right_prec, right_fixup) = fixup.rightmost_subexpression( - &e.right, + let right_fixup = fixup.rightmost_subexpression_fixup( + #[cfg(feature = "full")] + false, + #[cfg(feature = "full")] + false, #[cfg(feature = "full")] binop_prec, ); - let (left_needs_group, right_needs_group) = match binop_prec { - Precedence::Assign => (left_prec <= Precedence::Range, right_prec < binop_prec), - Precedence::Compare => (left_prec <= binop_prec, right_prec <= binop_prec), - _ => (left_prec < binop_prec, right_prec <= binop_prec), - }; + let right_needs_group = binop_prec != Precedence::Assign + && right_fixup.rightmost_subexpression_precedence(&e.right) <= binop_prec; print_subexpression(&e.left, left_needs_group, tokens, left_fixup); e.op.to_tokens(tokens); diff --git a/src/fixup.rs b/src/fixup.rs index 126700cc8..648fbab4d 100644 --- a/src/fixup.rs +++ b/src/fixup.rs @@ -511,9 +511,6 @@ fn scan_right( 1, ); if let Scan::Bailout | Scan::Consume = scan { - return Scan::Consume; - } - if right_fixup.rightmost_subexpression_precedence(&e.right) < Precedence::Assign { Scan::Consume } else if let Precedence::Unambiguous = fixup.next_operator { Scan::Fail @@ -552,11 +549,8 @@ fn scan_right( } { return Scan::Consume; } - let right_prec = right_fixup.rightmost_subexpression_precedence(&e.right); - let right_needs_group = match binop_prec { - Precedence::Assign => right_prec < binop_prec, - _ => right_prec <= binop_prec, - }; + let right_needs_group = binop_prec != Precedence::Assign + && right_fixup.rightmost_subexpression_precedence(&e.right) <= binop_prec; if right_needs_group { Scan::Consume } else if let (Scan::Fail, Precedence::Unambiguous) = (scan, fixup.next_operator) {