]> git.lizzy.rs Git - rust.git/commitdiff
Do not rely on format_missing for rewriting attributes
authortopecongiro <seuchida@gmail.com>
Tue, 13 Jun 2017 15:08:31 +0000 (00:08 +0900)
committertopecongiro <seuchida@gmail.com>
Tue, 13 Jun 2017 15:08:31 +0000 (00:08 +0900)
src/expr.rs
src/visitor.rs

index 80c89a3a91bd28c5a019a48f11a58b2aaab6a8c2..1e92b5b9368e5b26956635e26b90b1fd2d9fca1b 100644 (file)
@@ -47,16 +47,45 @@ enum ExprType {
     SubExpression,
 }
 
+fn combine_attr_and_expr(
+    context: &RewriteContext,
+    shape: Shape,
+    attr_str: &str,
+    expr_str: &str,
+) -> String {
+    let separator = if attr_str.is_empty() {
+        String::new()
+    } else {
+        if expr_str.contains('\n') || attr_str.contains('\n') ||
+            attr_str.len() + expr_str.len() > shape.width
+        {
+            format!("\n{}", shape.indent.to_string(context.config))
+        } else {
+            String::from(" ")
+        }
+    };
+    format!("{}{}{}", attr_str, separator, expr_str)
+}
+
 fn format_expr(
     expr: &ast::Expr,
     expr_type: ExprType,
     context: &RewriteContext,
     shape: Shape,
 ) -> Option<String> {
+    let attr_rw = (&*expr.attrs).rewrite(context, shape);
     if contains_skip(&*expr.attrs) {
-        return Some(context.snippet(expr.span));
+        if let Some(attr_str) = attr_rw {
+            return Some(combine_attr_and_expr(
+                context,
+                shape,
+                &attr_str,
+                &context.snippet(expr.span),
+            ));
+        } else {
+            return Some(context.snippet(expr.span));
+        }
     }
-    let attr_rw = (&*expr.attrs).rewrite(context, shape);
     let expr_rw = match expr.node {
         ast::ExprKind::Array(ref expr_vec) => {
             rewrite_array(
@@ -289,9 +318,8 @@ fn format_expr(
     };
     match (attr_rw, expr_rw) {
         (Some(attr_str), Some(expr_str)) => {
-            let space = if attr_str.is_empty() { "" } else { " " };
             recover_comment_removed(
-                format!("{}{}{}", attr_str, space, expr_str),
+                combine_attr_and_expr(context, shape, &attr_str, &expr_str),
                 expr.span,
                 context,
                 shape,
index 6358cc7531fa382b715eddb8210e12d3dd894feb..08ff3cd4c61ca3683a59a642690f03cf4adaae07 100644 (file)
@@ -82,15 +82,26 @@ fn visit_stmt(&mut self, stmt: &ast::Stmt) {
             ast::StmtKind::Item(ref item) => {
                 self.visit_item(item);
             }
-            ast::StmtKind::Local(..) |
-            ast::StmtKind::Expr(..) |
-            ast::StmtKind::Semi(..) => {
+            ast::StmtKind::Local(..) => {
                 let rewrite = stmt.rewrite(
                     &self.get_context(),
                     Shape::indented(self.block_indent, self.config),
                 );
                 self.push_rewrite(stmt.span, rewrite);
             }
+            ast::StmtKind::Expr(ref expr) |
+            ast::StmtKind::Semi(ref expr) => {
+                let rewrite = stmt.rewrite(
+                    &self.get_context(),
+                    Shape::indented(self.block_indent, self.config),
+                );
+                let span = if expr.attrs.is_empty() {
+                    stmt.span
+                } else {
+                    mk_sp(expr.attrs[0].span.lo, stmt.span.hi)
+                };
+                self.push_rewrite(span, rewrite)
+            }
             ast::StmtKind::Mac(ref mac) => {
                 let (ref mac, _macro_style, _) = **mac;
                 self.visit_mac(mac, None, MacroPosition::Statement);