]> git.lizzy.rs Git - rust.git/blobdiff - src/closures.rs
Merge pull request #3266 from wada314/fix-2973
[rust.git] / src / closures.rs
index e786339ed7c0b7300be28af975d8d51182355205..e29a412a5154491fa19524530420ca770345ee97 100644 (file)
@@ -9,17 +9,18 @@
 // except according to those terms.
 
 use config::lists::*;
-use syntax::source_map::Span;
 use syntax::parse::classify;
+use syntax::source_map::Span;
 use syntax::{ast, ptr};
 
-use source_map::SpanUtils;
-use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond, ToExpr};
+use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
 use items::{span_hi_for_arg, span_lo_for_arg};
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
+use overflow::OverflowableItem;
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
-use utils::{last_line_width, left_most_sub_expr, stmt_expr};
+use source_map::SpanUtils;
+use utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
 
 // This module is pretty messy because of the rules around closures and blocks:
 // FIXME - the below is probably no longer true in full.
@@ -149,11 +150,11 @@ fn rewrite_closure_with_block(
 
     let block = ast::Block {
         stmts: vec![ast::Stmt {
-            id: ast::NodeId::new(0),
+            id: ast::NodeId::root(),
             node: ast::StmtKind::Expr(ptr::P(body.clone())),
             span: body.span,
         }],
-        id: ast::NodeId::new(0),
+        id: ast::NodeId::root(),
         rules: ast::BlockCheckMode::Default,
         span: body.span,
         recovered: false,
@@ -173,7 +174,7 @@ fn allow_multi_line(expr: &ast::Expr) -> bool {
         match expr.node {
             ast::ExprKind::Match(..)
             | ast::ExprKind::Block(..)
-            | ast::ExprKind::Catch(..)
+            | ast::ExprKind::TryBlock(..)
             | ast::ExprKind::Loop(..)
             | ast::ExprKind::Struct(..) => true,
 
@@ -198,7 +199,8 @@ fn allow_multi_line(expr: &ast::Expr) -> bool {
             } else {
                 Some(rw)
             }
-        }).map(|rw| format!("{} {}", prefix, rw))
+        })
+        .map(|rw| format!("{} {}", prefix, rw))
 }
 
 // Rewrite closure whose body is block.
@@ -343,9 +345,9 @@ pub fn rewrite_last_closure(
 
         // When overflowing the closure which consists of a single control flow expression,
         // force to use block if its condition uses multi line.
-        let is_multi_lined_cond = rewrite_cond(context, body, body_shape)
-            .map(|cond| cond.contains('\n') || cond.len() > body_shape.width)
-            .unwrap_or(false);
+        let is_multi_lined_cond = rewrite_cond(context, body, body_shape).map_or(false, |cond| {
+            cond.contains('\n') || cond.len() > body_shape.width
+        });
         if is_multi_lined_cond {
             return rewrite_closure_with_block(body, &prefix, context, body_shape);
         }
@@ -357,18 +359,14 @@ pub fn rewrite_last_closure(
 }
 
 /// Returns true if the given vector of arguments has more than one `ast::ExprKind::Closure`.
-pub fn args_have_many_closure<T>(args: &[&T]) -> bool
-where
-    T: ToExpr,
-{
+pub fn args_have_many_closure(args: &[OverflowableItem]) -> bool {
     args.iter()
-        .filter(|arg| {
-            arg.to_expr()
-                .map(|e| match e.node {
-                    ast::ExprKind::Closure(..) => true,
-                    _ => false,
-                }).unwrap_or(false)
-        }).count()
+        .filter_map(|arg| arg.to_expr())
+        .filter(|expr| match expr.node {
+            ast::ExprKind::Closure(..) => true,
+            _ => false,
+        })
+        .count()
         > 1
 }