]> git.lizzy.rs Git - rust.git/commitdiff
Check format failures explicitly in visit_block
authortopecongiro <seuchida@gmail.com>
Wed, 3 May 2017 15:21:51 +0000 (00:21 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 3 May 2017 15:21:51 +0000 (00:21 +0900)
src/expr.rs
src/visitor.rs

index 75a7ff21abe4a7547dc979617c1d2e3f32e60efe..5876409a9fa22e95c488dae8a1cb399cd4e746e8 100644 (file)
@@ -570,19 +570,6 @@ fn rewrite_closure_expr(expr: &ast::Expr,
         rewrite.map(|rw| format!("{} {}", prefix, rw))
     }
 
-    fn no_weird_visual_indent(block_str: &str, context: &RewriteContext) -> bool {
-        let mut prev_indent_width = 0;
-        for line in block_str.lines() {
-            let cur_indent_width = line.find(|c: char| !c.is_whitespace()).unwrap_or(0);
-            if prev_indent_width > cur_indent_width + context.config.tab_spaces &&
-               line.find('}').unwrap_or(0) != cur_indent_width {
-                return false;
-            }
-            prev_indent_width = cur_indent_width;
-        }
-        true
-    }
-
     fn rewrite_closure_block(block: &ast::Block,
                              prefix: String,
                              context: &RewriteContext,
@@ -592,9 +579,7 @@ fn rewrite_closure_block(block: &ast::Block,
         // closure is large.
         if let Some(block_str) = block.rewrite(&context, shape) {
             let block_threshold = context.config.closure_block_indent_threshold;
-            if (block_threshold < 0 ||
-                block_str.matches('\n').count() <= block_threshold as usize) &&
-               no_weird_visual_indent(&block_str, context) {
+            if block_threshold < 0 || block_str.matches('\n').count() <= block_threshold as usize {
                 if let Some(block_str) = block_str.rewrite(context, shape) {
                     return Some(format!("{} {}", prefix, block_str));
                 }
@@ -697,8 +682,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         };
 
         visitor.visit_block(self);
-
-        Some(format!("{}{}", prefix, visitor.buffer))
+        if visitor.failed {
+            None
+        } else {
+            Some(format!("{}{}", prefix, visitor.buffer))
+        }
     }
 }
 
index 86fb1bce9d987b7ecaff18657763e57894040ed0..71013d085d51e93f2978f88a437c61a2a8e4fd80 100644 (file)
@@ -39,6 +39,7 @@ pub struct FmtVisitor<'a> {
     // FIXME: use an RAII util or closure for indenting
     pub block_indent: Indent,
     pub config: &'a Config,
+    pub failed: bool,
 }
 
 impl<'a> FmtVisitor<'a> {
@@ -65,6 +66,9 @@ fn visit_stmt(&mut self, stmt: &ast::Stmt) {
                                            Shape::legacy(self.config.max_width -
                                                          self.block_indent.width(),
                                                          self.block_indent));
+                if rewrite.is_none() {
+                    self.failed = true;
+                }
                 self.push_rewrite(stmt.span, rewrite);
             }
             ast::StmtKind::Mac(ref mac) => {
@@ -457,6 +461,7 @@ pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisi
                 alignment: 0,
             },
             config: config,
+            failed: false,
         }
     }