]> git.lizzy.rs Git - rust.git/commitdiff
Add and remove comments
authortopecongiro <seuchida@gmail.com>
Thu, 15 Jun 2017 23:28:12 +0000 (08:28 +0900)
committertopecongiro <seuchida@gmail.com>
Thu, 15 Jun 2017 23:28:12 +0000 (08:28 +0900)
src/expr.rs
tests/target/issue-1681.rs

index 22f963e0609f97c686358cfba21933daab450d6e..92c62b08125b11035b649c87e74f31406fac2a1e 100644 (file)
@@ -590,6 +590,7 @@ fn rewrite_closure_fn_decl(
     };
     let list_str = try_opt!(write_list(&item_vec, &fmt));
     let mut prefix = format!("{}|{}|", mover, list_str);
+    // 1 = space between `|...|` and body.
     let extra_offset = extra_offset(&prefix, shape) + 1;
 
     if !ret_str.is_empty() {
@@ -682,6 +683,7 @@ fn rewrite_closure(
     }
 }
 
+// Rewrite closure with a single expression wrapping its body with block.
 fn rewrite_closure_with_block(
     context: &RewriteContext,
     shape: Shape,
@@ -703,6 +705,7 @@ fn rewrite_closure_with_block(
     rewrite_closure_block(&block, prefix, context, shape)
 }
 
+// Rewrite closure with a single expression without wrapping its body with block.
 fn rewrite_closure_expr(
     expr: &ast::Expr,
     prefix: &str,
@@ -716,6 +719,7 @@ fn rewrite_closure_expr(
     rewrite.map(|rw| format!("{} {}", prefix, rw))
 }
 
+// Rewrite closure whose body is block.
 fn rewrite_closure_block(
     block: &ast::Block,
     prefix: &str,
@@ -2217,24 +2221,29 @@ fn rewrite_last_arg_with_overflow<'a, T>(
             // When overflowing the closure which consists of a single control flow expression,
             // force to use block if its condition uses multi line.
             ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
-                if rewrite_cond(context, body, shape).map_or(false, |cond| cond.contains('\n')) {
-                    rewrite_closure_fn_decl(capture, fn_decl, body, expr.span, context, shape)
-                        .and_then(|(prefix, extra_offset)| {
-                            // 1 = space between `|...|` and body.
-                            shape.offset_left(extra_offset).and_then(|body_shape| {
-                                let body = match body.node {
-                                    ast::ExprKind::Block(ref block) => {
-                                        stmt_expr(&block.stmts[0]).unwrap()
-                                    }
-                                    _ => body,
-                                };
-                                rewrite_closure_with_block(context, body_shape, &prefix, body)
-                            })
-                        })
-                        .or_else(|| expr.rewrite(context, shape))
-                } else {
-                    expr.rewrite(context, shape)
-                }
+                let try_closure_with_block = || {
+                    let body = match body.node {
+                        ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
+                            try_opt!(stmt_expr(&block.stmts[0]))
+                        }
+                        _ => body,
+                    };
+                    let (prefix, extra_offset) = try_opt!(rewrite_closure_fn_decl(
+                        capture,
+                        fn_decl,
+                        body,
+                        expr.span,
+                        context,
+                        shape,
+                    ));
+                    let shape = try_opt!(shape.offset_left(extra_offset));
+                    rewrite_cond(context, body, shape).map_or(None, |cond| if cond.contains('\n') {
+                        rewrite_closure_with_block(context, shape, &prefix, body)
+                    } else {
+                        None
+                    })
+                };
+                try_closure_with_block().or_else(|| expr.rewrite(context, shape))
             }
             _ => expr.rewrite(context, shape),
         }
index a13f323e88ff7b1957a19b41dc01e8b5b984e1da..5734fd5502df2fb08a93c4c0b874e9da9cd8561a 100644 (file)
@@ -1,7 +1,9 @@
 // rustfmt-max_width: 80
 
+// We would like to surround closure body with block when overflowing the last
+// argument of function call if the last argument has condition and without
+// block it may go multi lines.
 fn foo() {
-    // This is where it gets good
     refmut_map_result(self.cache.borrow_mut(), |cache| {
         match cache.entry(cache_key) {
             Occupied(entry) => Ok(entry.into_mut()),
@@ -13,7 +15,6 @@ fn foo() {
 
                 Ok(entry.insert(try!(statement)))
             }
-            // and now, casually call a method on this
         }
     }).map(MaybeCached::Cached)
 }