]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Use rewrite() instead of format_expr
[rust.git] / src / expr.rs
index 2bfbba2fb1f2513e69befab91130dcce59cf2cdc..228722474e1b8706d6aead24912a92b87367c41f 100644 (file)
@@ -26,7 +26,7 @@
 use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
             struct_lit_shape, struct_lit_tactic, write_list, DefinitiveListTactic, ListFormatting,
             ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
-use macros::{rewrite_macro, MacroPosition};
+use macros::{rewrite_macro, MacroArg, MacroPosition};
 use patterns::{can_be_overflowed_pat, TuplePatField};
 use rewrite::{Rewrite, RewriteContext};
 use string::{rewrite_string, StringFormat};
@@ -876,13 +876,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     ""
                 };
 
-                let expr_type = match self.node {
-                    ast::StmtKind::Expr(_) => ExprType::SubExpression,
-                    ast::StmtKind::Semi(_) => ExprType::Statement,
-                    _ => unreachable!(),
-                };
                 let shape = try_opt!(shape.sub_width(suffix.len()));
-                format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
+                format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
             }
             ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
         };
@@ -933,7 +928,7 @@ struct ControlFlow<'a> {
     span: Span,
 }
 
-fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow> {
+fn to_control_flow<'a>(expr: &'a ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'a>> {
     match expr.node {
         ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if(
             cond,
@@ -3012,3 +3007,20 @@ fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
         false
     }
 }
+
+impl<'a> ToExpr for MacroArg {
+    fn to_expr(&self) -> Option<&ast::Expr> {
+        match self {
+            &MacroArg::Expr(ref expr) => Some(expr),
+            _ => None,
+        }
+    }
+
+    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
+        match self {
+            &MacroArg::Expr(ref expr) => can_be_overflowed_expr(context, expr, len),
+            &MacroArg::Ty(ref ty) => can_be_overflowed_type(context, ty, len),
+            &MacroArg::Pat(..) => false,
+        }
+    }
+}