@@ -196,9 +196,10 @@ pub(crate) fn format_expr(
196196 capture, is_async, movability, fn_decl, body, expr. span , context, shape,
197197 )
198198 }
199- ast:: ExprKind :: Try ( ..) | ast:: ExprKind :: Field ( ..) | ast:: ExprKind :: MethodCall ( ..) => {
200- rewrite_chain ( expr, context, shape)
201- }
199+ ast:: ExprKind :: Try ( ..)
200+ | ast:: ExprKind :: Field ( ..)
201+ | ast:: ExprKind :: MethodCall ( ..)
202+ | ast:: ExprKind :: Await ( _) => rewrite_chain ( expr, context, shape) ,
202203 ast:: ExprKind :: MacCall ( ref mac) => {
203204 rewrite_macro ( mac, None , context, shape, MacroPosition :: Expression ) . or_else ( || {
204205 wrap_str (
@@ -377,7 +378,6 @@ pub(crate) fn format_expr(
377378 ) )
378379 }
379380 }
380- ast:: ExprKind :: Await ( _) => rewrite_chain ( expr, context, shape) ,
381381 ast:: ExprKind :: Underscore => Some ( "_" . to_owned ( ) ) ,
382382 ast:: ExprKind :: Err => None ,
383383 } ;
@@ -829,6 +829,7 @@ impl<'a> ControlFlow<'a> {
829829 & format ! ( "{}{}{}" , matcher, pat_string, self . connector) ,
830830 expr,
831831 cond_shape,
832+ & RhsAssignKind :: Expr ( & expr. kind , expr. span ) ,
832833 RhsTactics :: Default ,
833834 comments_span,
834835 true ,
@@ -1839,6 +1840,34 @@ fn rewrite_unary_op(
18391840 rewrite_unary_prefix ( context, ast:: UnOp :: to_string ( op) , expr, shape)
18401841}
18411842
1843+ pub ( crate ) enum RhsAssignKind < ' ast > {
1844+ Expr ( & ' ast ast:: ExprKind , Span ) ,
1845+ Bounds ,
1846+ Ty ,
1847+ }
1848+
1849+ impl < ' ast > RhsAssignKind < ' ast > {
1850+ // TODO(calebcartwright)
1851+ // Preemptive addition for handling RHS with chains, not yet utilized.
1852+ // It may make more sense to construct the chain first and then check
1853+ // whether there are actually chain elements.
1854+ #[ allow( dead_code) ]
1855+ fn is_chain ( & self ) -> bool {
1856+ match self {
1857+ RhsAssignKind :: Expr ( kind, _) => {
1858+ matches ! (
1859+ kind,
1860+ ast:: ExprKind :: Try ( ..)
1861+ | ast:: ExprKind :: Field ( ..)
1862+ | ast:: ExprKind :: MethodCall ( ..)
1863+ | ast:: ExprKind :: Await ( _)
1864+ )
1865+ }
1866+ _ => false ,
1867+ }
1868+ }
1869+ }
1870+
18421871fn rewrite_assignment (
18431872 context : & RewriteContext < ' _ > ,
18441873 lhs : & ast:: Expr ,
@@ -1855,7 +1884,13 @@ fn rewrite_assignment(
18551884 let lhs_shape = shape. sub_width ( operator_str. len ( ) + 1 ) ?;
18561885 let lhs_str = format ! ( "{} {}" , lhs. rewrite( context, lhs_shape) ?, operator_str) ;
18571886
1858- rewrite_assign_rhs ( context, lhs_str, rhs, shape)
1887+ rewrite_assign_rhs (
1888+ context,
1889+ lhs_str,
1890+ rhs,
1891+ & RhsAssignKind :: Expr ( & rhs. kind , rhs. span ) ,
1892+ shape,
1893+ )
18591894}
18601895
18611896/// Controls where to put the rhs.
@@ -1876,16 +1911,18 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18761911 context : & RewriteContext < ' _ > ,
18771912 lhs : S ,
18781913 ex : & R ,
1914+ rhs_kind : & RhsAssignKind < ' _ > ,
18791915 shape : Shape ,
18801916) -> Option < String > {
1881- rewrite_assign_rhs_with ( context, lhs, ex, shape, RhsTactics :: Default )
1917+ rewrite_assign_rhs_with ( context, lhs, ex, shape, rhs_kind , RhsTactics :: Default )
18821918}
18831919
18841920pub ( crate ) fn rewrite_assign_rhs_expr < R : Rewrite > (
18851921 context : & RewriteContext < ' _ > ,
18861922 lhs : & str ,
18871923 ex : & R ,
18881924 shape : Shape ,
1925+ rhs_kind : & RhsAssignKind < ' _ > ,
18891926 rhs_tactics : RhsTactics ,
18901927) -> Option < String > {
18911928 let last_line_width = last_line_width ( lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
@@ -1910,6 +1947,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19101947 ex,
19111948 orig_shape,
19121949 ex. rewrite ( context, orig_shape) ,
1950+ rhs_kind,
19131951 rhs_tactics,
19141952 has_rhs_comment,
19151953 )
@@ -1920,10 +1958,11 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19201958 lhs : S ,
19211959 ex : & R ,
19221960 shape : Shape ,
1961+ rhs_kind : & RhsAssignKind < ' _ > ,
19231962 rhs_tactics : RhsTactics ,
19241963) -> Option < String > {
19251964 let lhs = lhs. into ( ) ;
1926- let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_tactics) ?;
1965+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind , rhs_tactics) ?;
19271966 Some ( lhs + & rhs)
19281967}
19291968
@@ -1932,6 +1971,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
19321971 lhs : S ,
19331972 ex : & R ,
19341973 shape : Shape ,
1974+ rhs_kind : & RhsAssignKind < ' _ > ,
19351975 rhs_tactics : RhsTactics ,
19361976 between_span : Span ,
19371977 allow_extend : bool ,
@@ -1943,7 +1983,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
19431983 } else {
19441984 shape
19451985 } ;
1946- let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_tactics) ?;
1986+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_kind , rhs_tactics) ?;
19471987
19481988 if contains_comment {
19491989 let rhs = rhs. trim_start ( ) ;
@@ -1958,6 +1998,7 @@ fn choose_rhs<R: Rewrite>(
19581998 expr : & R ,
19591999 shape : Shape ,
19602000 orig_rhs : Option < String > ,
2001+ _rhs_kind : & RhsAssignKind < ' _ > ,
19612002 rhs_tactics : RhsTactics ,
19622003 has_rhs_comment : bool ,
19632004) -> Option < String > {
0 commit comments