Skip to content

Commit

Permalink
Right side of assignment can never require synthetic paren
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 9, 2025
1 parent 533caa2 commit ea9f750
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
25 changes: 14 additions & 11 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand Down Expand Up @@ -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);
Expand Down
10 changes: 2 additions & 8 deletions src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ea9f750

Please sign in to comment.