@@ -286,7 +286,7 @@ impl fmt::Display for FormatReport {
286
286
287
287
/// Format the given snippet. The snippet is expected to be *complete* code.
288
288
/// 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 > {
290
290
let mut config = config. clone ( ) ;
291
291
panic:: catch_unwind ( || {
292
292
let mut out: Vec < u8 > = Vec :: with_capacity ( snippet. len ( ) * 2 ) ;
@@ -297,7 +297,7 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
297
297
let ( formatting_error, result) = {
298
298
let input = Input :: Text ( snippet. into ( ) ) ;
299
299
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 ) ;
301
301
(
302
302
session. errors . has_macro_format_failure
303
303
|| session. out . as_ref ( ) . unwrap ( ) . is_empty ( ) && !snippet. is_empty ( )
@@ -323,7 +323,11 @@ fn format_snippet(snippet: &str, config: &Config) -> Option<FormattedSnippet> {
323
323
/// The code block may be incomplete (i.e., parser may be unable to parse it).
324
324
/// To avoid panic in parser, we wrap the code block with a dummy function.
325
325
/// 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 > {
327
331
const FN_MAIN_PREFIX : & str = "fn main() {\n " ;
328
332
329
333
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
356
360
config_with_unix_newline
357
361
. set ( )
358
362
. 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 ) ?;
360
364
// Remove wrapping main block
361
365
formatted. unwrap_code_block ( ) ;
362
366
@@ -435,7 +439,7 @@ impl<'b, T: Write + 'b> Session<'b, T> {
435
439
/// The main entry point for Rustfmt. Formats the given input according to the
436
440
/// given config. `out` is only necessary if required by the configuration.
437
441
pub fn format ( & mut self , input : Input ) -> Result < FormatReport , ErrorKind > {
438
- self . format_input_inner ( input)
442
+ self . format_input_inner ( input, false )
439
443
}
440
444
441
445
pub fn override_config < F , U > ( & mut self , mut config : Config , f : F ) -> U
@@ -550,15 +554,15 @@ mod unit_tests {
550
554
// `format_snippet()` and `format_code_block()` should not panic
551
555
// even when we cannot parse the given snippet.
552
556
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( ) ) ;
555
559
}
556
560
557
561
fn test_format_inner < F > ( formatter : F , input : & str , expected : & str ) -> bool
558
562
where
559
- F : Fn ( & str , & Config ) -> Option < FormattedSnippet > ,
563
+ F : Fn ( & str , & Config , bool ) -> Option < FormattedSnippet > ,
560
564
{
561
- let output = formatter ( input, & Config :: default ( ) ) ;
565
+ let output = formatter ( input, & Config :: default ( ) , false ) ;
562
566
output. is_some ( ) && output. unwrap ( ) . snippet == expected
563
567
}
564
568
@@ -580,7 +584,7 @@ mod unit_tests {
580
584
fn test_format_code_block_fail ( ) {
581
585
#[ rustfmt:: skip]
582
586
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( ) ) ;
584
588
}
585
589
586
590
#[ test]
0 commit comments