From 19d93ba852c96886679d5a57108bf83c13c43635 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 1 Jan 2025 11:29:20 -0800 Subject: [PATCH] Merge leftmost_subexpression_with_operator and leftmost_subexpression into one method --- src/expr.rs | 8 ++++++-- src/fixup.rs | 34 +++++++++++++--------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 8efb1f04d9..060a205a7f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -3306,7 +3306,7 @@ pub(crate) mod printing { &e.left, Precedence::of(&e.left) <= Precedence::Range, tokens, - fixup.leftmost_subexpression(), + fixup.leftmost_subexpression_with_operator(false, false), ); e.eq_token.to_tokens(tokens); print_subexpression( @@ -3475,7 +3475,11 @@ pub(crate) mod printing { &e.expr, Precedence::of(&e.expr) < Precedence::Cast, tokens, - fixup.leftmost_subexpression(), + fixup.leftmost_subexpression_with_operator( + #[cfg(feature = "full")] + false, + false, + ), ); e.as_token.to_tokens(tokens); e.ty.to_tokens(tokens); diff --git a/src/fixup.rs b/src/fixup.rs index e4c726e41c..f8ba0d02f1 100644 --- a/src/fixup.rs +++ b/src/fixup.rs @@ -195,7 +195,11 @@ impl FixupContext { /// /// Not every expression has a leftmost subexpression. For example neither /// `-$a` nor `[$a]` have one. - pub fn leftmost_subexpression(self) -> Self { + pub fn leftmost_subexpression_with_operator( + self, + #[cfg(feature = "full")] next_operator_can_begin_expr: bool, + next_operator_can_begin_generics: bool, + ) -> Self { FixupContext { #[cfg(feature = "full")] stmt: false, @@ -209,10 +213,10 @@ impl FixupContext { #[cfg(feature = "full")] rightmost_subexpression: false, #[cfg(feature = "full")] - next_operator_can_begin_expr: false, + next_operator_can_begin_expr, #[cfg(feature = "full")] next_operator_can_continue_expr: true, - next_operator_can_begin_generics: false, + next_operator_can_begin_generics, ..self } } @@ -242,22 +246,6 @@ impl FixupContext { } } - /// Transform this fixup into the one that should apply when printing a - /// leftmost subexpression followed by punctuation that is legal as the - /// first token of an expression. - pub fn leftmost_subexpression_with_operator( - self, - #[cfg(feature = "full")] next_operator_can_begin_expr: bool, - next_operator_can_begin_generics: bool, - ) -> Self { - FixupContext { - #[cfg(feature = "full")] - next_operator_can_begin_expr, - next_operator_can_begin_generics, - ..self.leftmost_subexpression() - } - } - /// Transform this fixup into the one that should apply when printing the /// rightmost subexpression of the current expression. /// @@ -393,10 +381,14 @@ fn test_leftmost_rightmost_invariant() { }; assert_eq!(i, BITS); assert_eq!( - fixup.leftmost_subexpression().rightmost_subexpression(), + fixup + .leftmost_subexpression_with_operator(false, false) + .rightmost_subexpression(), FixupContext { rightmost_subexpression: true, - ..fixup.rightmost_subexpression().leftmost_subexpression() + ..fixup + .rightmost_subexpression() + .leftmost_subexpression_with_operator(false, false) }, ); }