@@ -286,7 +286,7 @@ impl fmt::Display for FormatReport {
286286
287287/// Format the given snippet. The snippet is expected to be *complete* code.
288288/// When we cannot parse the given snippet, this function returns `None`.
289- fn format_snippet ( snippet : & str , config : & Config ) -> Option < FormattedSnippet > {
289+ fn format_snippet ( snippet : & str , config : & Config , is_macro_def : bool ) -> Option < FormattedSnippet > {
290290 let mut config = config. clone ( ) ;
291291 panic:: catch_unwind ( || {
292292 let mut out: Vec < u8 > = Vec :: with_capacity ( snippet. len ( ) * 2 ) ;
@@ -297,7 +297,7 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
297297 let ( formatting_error, result) = {
298298 let input = Input :: Text ( snippet. into ( ) ) ;
299299 let mut session = Session :: new ( config, Some ( & mut out) ) ;
300- let result = session. format ( input) ;
300+ let result = session. format_input_inner ( input, is_macro_def ) ;
301301 (
302302 session. errors . has_macro_format_failure
303303 || session. out . as_ref ( ) . unwrap ( ) . is_empty ( ) && !snippet. is_empty ( )
@@ -323,7 +323,11 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
323323/// The code block may be incomplete (i.e., parser may be unable to parse it).
324324/// To avoid panic in parser, we wrap the code block with a dummy function.
325325/// The returned code block does **not** end with newline.
326- fn format_code_block ( code_snippet : & str , config : & Config ) -> Option < FormattedSnippet > {
326+ fn format_code_block (
327+ code_snippet : & str ,
328+ config : & Config ,
329+ is_macro_def : bool ,
330+ ) -> Option < FormattedSnippet > {
327331 const FN_MAIN_PREFIX : & str = "fn main() {\n " ;
328332
329333 fn enclose_in_main_block ( s : & str , config : & Config ) -> String {
@@ -356,7 +360,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
356360 config_with_unix_newline
357361 . set ( )
358362 . newline_style ( NewlineStyle :: Unix ) ;
359- let mut formatted = format_snippet ( & snippet, & config_with_unix_newline) ?;
363+ let mut formatted = format_snippet ( & snippet, & config_with_unix_newline, is_macro_def ) ?;
360364 // Remove wrapping main block
361365 formatted. unwrap_code_block ( ) ;
362366
@@ -435,7 +439,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
435439 /// The main entry point for Rustfmt. Formats the given input according to the
436440 /// given config. `out` is only necessary if required by the configuration.
437441 pub fn format ( & mut self , input : Input ) -> Result < FormatReport , ErrorKind > {
438- self . format_input_inner ( input)
442+ self . format_input_inner ( input, false )
439443 }
440444
441445 pub fn override_config < F , U > ( & mut self , mut config : Config , f : F ) -> U
@@ -550,15 +554,15 @@ mod unit_tests {
550554 // `format_snippet()` and `format_code_block()` should not panic
551555 // even when we cannot parse the given snippet.
552556 let snippet = "let" ;
553- assert ! ( format_snippet( snippet, & Config :: default ( ) ) . is_none( ) ) ;
554- assert ! ( format_code_block( snippet, & Config :: default ( ) ) . is_none( ) ) ;
557+ assert ! ( format_snippet( snippet, & Config :: default ( ) , false ) . is_none( ) ) ;
558+ assert ! ( format_code_block( snippet, & Config :: default ( ) , false ) . is_none( ) ) ;
555559 }
556560
557561 fn test_format_inner < F > ( formatter : F , input : & str , expected : & str ) -> bool
558562 where
559- F : Fn ( & str , & Config ) -> Option < FormattedSnippet > ,
563+ F : Fn ( & str , & Config , bool ) -> Option < FormattedSnippet > ,
560564 {
561- let output = formatter ( input, & Config :: default ( ) ) ;
565+ let output = formatter ( input, & Config :: default ( ) , false ) ;
562566 output. is_some ( ) && output. unwrap ( ) . snippet == expected
563567 }
564568
@@ -580,7 +584,7 @@ mod unit_tests {
580584 fn test_format_code_block_fail ( ) {
581585 #[ rustfmt:: skip]
582586 let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);" ;
583- assert ! ( format_code_block( code_block, & Config :: default ( ) ) . is_none( ) ) ;
587+ assert ! ( format_code_block( code_block, & Config :: default ( ) , false ) . is_none( ) ) ;
584588 }
585589
586590 #[ test]
0 commit comments