File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -1555,6 +1555,21 @@ impl<'a> State<'a> {
1555
1555
self . pclose ( )
1556
1556
}
1557
1557
1558
+ pub fn check_expr_bin_needs_paren ( & mut self , sub_expr : & ast:: Expr ,
1559
+ binop : ast:: BinOp ) -> bool {
1560
+ match sub_expr. node {
1561
+ ast:: ExprBinary ( ref sub_op, _, _) => {
1562
+ if ast_util:: operator_prec ( sub_op. node ) <
1563
+ ast_util:: operator_prec ( binop. node ) {
1564
+ true
1565
+ } else {
1566
+ false
1567
+ }
1568
+ }
1569
+ _ => true
1570
+ }
1571
+ }
1572
+
1558
1573
pub fn print_expr_maybe_paren ( & mut self , expr : & ast:: Expr ) -> io:: Result < ( ) > {
1559
1574
let needs_par = needs_parentheses ( expr) ;
1560
1575
if needs_par {
@@ -1670,10 +1685,18 @@ impl<'a> State<'a> {
1670
1685
op : ast:: BinOp ,
1671
1686
lhs : & ast:: Expr ,
1672
1687
rhs : & ast:: Expr ) -> io:: Result < ( ) > {
1673
- try!( self . print_expr_maybe_paren ( lhs) ) ;
1688
+ if self . check_expr_bin_needs_paren ( lhs, op) {
1689
+ try!( self . print_expr_maybe_paren ( lhs) ) ;
1690
+ } else {
1691
+ try!( self . print_expr ( lhs) ) ;
1692
+ }
1674
1693
try!( space ( & mut self . s ) ) ;
1675
1694
try!( self . word_space ( ast_util:: binop_to_string ( op. node ) ) ) ;
1676
- self . print_expr_maybe_paren ( rhs)
1695
+ if self . check_expr_bin_needs_paren ( rhs, op) {
1696
+ self . print_expr_maybe_paren ( rhs)
1697
+ } else {
1698
+ self . print_expr ( rhs)
1699
+ }
1677
1700
}
1678
1701
1679
1702
fn print_expr_unary ( & mut self ,
You can’t perform that action at this time.
0 commit comments