]> git.lizzy.rs Git - rust.git/blobdiff - src/macros.rs
Merge pull request #3266 from wada314/fix-2973
[rust.git] / src / macros.rs
index ca0de8fbb237ef7abe0014492a4d15d84ea2eb81..9ce5c913ca55f48a82cadc4c00923098d9025281 100644 (file)
@@ -42,7 +42,7 @@
 use spanned::Spanned;
 use utils::{
     format_visibility, is_empty_line, mk_sp, remove_trailing_white_spaces, rewrite_ident,
-    trim_left_preserve_layout, wrap_str,
+    trim_left_preserve_layout, wrap_str, NodeIdExt,
 };
 use visitor::FmtVisitor;
 
@@ -256,19 +256,7 @@ pub fn rewrite_macro_inner(
             }
             DelimToken::Paren => Some(format!("{}()", macro_name)),
             DelimToken::Bracket => Some(format!("{}[]", macro_name)),
-            DelimToken::Brace => {
-                // Preserve at most one space before the braces.
-                let char_after_bang = context
-                    .snippet(mac.span)
-                    .split('!')
-                    .nth(1)
-                    .and_then(|x| x.chars().next());
-                if let Some(' ') = char_after_bang {
-                    Some(format!("{} {{}}", macro_name))
-                } else {
-                    Some(format!("{}{{}}", macro_name))
-                }
-            }
+            DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
             _ => unreachable!(),
         };
     }
@@ -428,8 +416,15 @@ pub fn rewrite_macro_inner(
             }
         }
         DelimToken::Brace => {
-            // Skip macro invocations with braces, for now.
-            trim_left_preserve_layout(context.snippet(mac.span), shape.indent, &context.config)
+            // For macro invocations with braces, always put a space between
+            // the `macro_name!` and `{ /* macro_body */ }` but skip modifying
+            // anything in between the braces (for now).
+            let snippet = context.snippet(mac.span);
+            let macro_raw = snippet.split_at(snippet.find('!')? + 1).1.trim_start();
+            match trim_left_preserve_layout(macro_raw, shape.indent, &context.config) {
+                Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
+                None => Some(format!("{} {}", macro_name, macro_raw)),
+            }
         }
         _ => unreachable!(),
     }
@@ -1107,7 +1102,6 @@ fn next_space(tok: &Token) -> SpaceState {
         | Token::DotDot
         | Token::DotDotDot
         | Token::DotDotEq
-        | Token::DotEq
         | Token::Question => SpaceState::Punctuation,
 
         Token::ModSep
@@ -1132,7 +1126,7 @@ pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::
         let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
 
         Some(ast::Expr {
-            id: ast::NodeId::new(0), // dummy value
+            id: ast::NodeId::root(), // dummy value
             node: ast::ExprKind::Try(parser.parse_expr().ok()?),
             span: mac.span, // incorrect span, but shouldn't matter too much
             attrs: ThinVec::new(),
@@ -1296,7 +1290,7 @@ fn rewrite(
 
         // Indent the body since it is in a block.
         let indent_str = body_indent.to_string(&config);
-        let mut new_body = LineClasses::new(new_body.trim_right())
+        let mut new_body = LineClasses::new(new_body.trim_end())
             .enumerate()
             .fold(
                 (String::new(), true),