@@ -46,8 +46,12 @@ impl<'b, T: Write + 'b> Session<'b, T> {
46
46
let config = & self . config . clone ( ) ;
47
47
let format_result = format_project ( input, config, self ) ;
48
48
49
- format_result. map ( |( report, summary) | {
50
- self . summary . add ( summary) ;
49
+ format_result. map ( |report| {
50
+ {
51
+ let new_errors = & report. internal . borrow ( ) . 1 ;
52
+
53
+ self . errors . add ( new_errors) ;
54
+ }
51
55
report
52
56
} )
53
57
} )
@@ -59,8 +63,7 @@ fn format_project<T: FormatHandler>(
59
63
input : Input ,
60
64
config : & Config ,
61
65
handler : & mut T ,
62
- ) -> Result < ( FormatReport , Summary ) , ErrorKind > {
63
- let mut summary = Summary :: default ( ) ;
66
+ ) -> Result < FormatReport , ErrorKind > {
64
67
let mut timer = Timer :: Initialized ( Instant :: now ( ) ) ;
65
68
66
69
let main_file = input. file_name ( ) ;
@@ -69,21 +72,15 @@ fn format_project<T: FormatHandler>(
69
72
// Parse the crate.
70
73
let codemap = Rc :: new ( CodeMap :: new ( FilePathMapping :: empty ( ) ) ) ;
71
74
let mut parse_session = make_parse_sess ( codemap. clone ( ) , config) ;
72
- let krate = parse_crate ( input, & parse_session, config, & mut summary) ?;
75
+ let mut report = FormatReport :: new ( ) ;
76
+ let krate = parse_crate ( input, & parse_session, config, & mut report) ?;
73
77
timer = timer. done_parsing ( ) ;
74
78
75
79
// Suppress error output if we have to do any further parsing.
76
80
let silent_emitter = silent_emitter ( codemap) ;
77
81
parse_session. span_diagnostic = Handler :: with_emitter ( true , false , silent_emitter) ;
78
82
79
- let mut context = FormatContext :: new (
80
- & krate,
81
- FormatReport :: new ( ) ,
82
- summary,
83
- parse_session,
84
- config,
85
- handler,
86
- ) ;
83
+ let mut context = FormatContext :: new ( & krate, report, parse_session, config, handler) ;
87
84
88
85
let files = modules:: list_files ( & krate, context. parse_session . codemap ( ) ) ?;
89
86
for ( path, module) in files {
@@ -104,36 +101,20 @@ fn format_project<T: FormatHandler>(
104
101
)
105
102
} ) ;
106
103
107
- context. summarise_errors ( ) ;
108
- Ok ( ( context. report , context. summary ) )
104
+ Ok ( context. report )
109
105
}
110
106
111
107
// Used for formatting files.
112
108
#[ derive( new) ]
113
109
struct FormatContext < ' a , T : FormatHandler + ' a > {
114
110
krate : & ' a ast:: Crate ,
115
111
report : FormatReport ,
116
- summary : Summary ,
117
112
parse_session : ParseSess ,
118
113
config : & ' a Config ,
119
114
handler : & ' a mut T ,
120
115
}
121
116
122
117
impl < ' a , T : FormatHandler + ' a > FormatContext < ' a , T > {
123
- // Moves errors from the report to the summary.
124
- fn summarise_errors ( & mut self ) {
125
- if self . report . has_warnings ( ) {
126
- self . summary . add_formatting_error ( ) ;
127
- }
128
- let report_errs = & self . report . internal . borrow ( ) . 1 ;
129
- if report_errs. has_check_errors {
130
- self . summary . add_check_error ( ) ;
131
- }
132
- if report_errs. has_operational_errors {
133
- self . summary . add_operational_error ( ) ;
134
- }
135
- }
136
-
137
118
// Formats a single file/module.
138
119
fn format_file (
139
120
& mut self ,
@@ -188,16 +169,22 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
188
169
replace_with_system_newlines ( & mut visitor. buffer , & self . config ) ;
189
170
190
171
if visitor. macro_rewrite_failure {
191
- self . summary . add_macro_format_failure ( ) ;
172
+ self . report . add_macro_format_failure ( ) ;
192
173
}
193
174
194
- self . handler . handle_formatted_file ( path, visitor. buffer )
175
+ self . handler
176
+ . handle_formatted_file ( path, visitor. buffer , & mut self . report )
195
177
}
196
178
}
197
179
198
180
// Handle the results of formatting.
199
181
trait FormatHandler {
200
- fn handle_formatted_file ( & mut self , path : FileName , result : String ) -> Result < ( ) , ErrorKind > ;
182
+ fn handle_formatted_file (
183
+ & mut self ,
184
+ path : FileName ,
185
+ result : String ,
186
+ report : & mut FormatReport ,
187
+ ) -> Result < ( ) , ErrorKind > ;
201
188
}
202
189
203
190
impl < ' b , T : Write + ' b > FormatHandler for Session < ' b , T > {
@@ -206,10 +193,11 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
206
193
& mut self ,
207
194
path : FileName ,
208
195
mut result : String ,
196
+ report : & mut FormatReport ,
209
197
) -> Result < ( ) , ErrorKind > {
210
198
if let Some ( ref mut out) = self . out {
211
199
match filemap:: write_file ( & mut result, & path, out, & self . config ) {
212
- Ok ( b) if b => self . summary . add_diff ( ) ,
200
+ Ok ( b) if b => report . add_diff ( ) ,
213
201
Err ( e) => {
214
202
// Create a new error with path_str to help users see which files failed
215
203
let err_msg = format ! ( "{}: {}" , path, e) ;
@@ -298,114 +286,56 @@ pub(crate) type FormatErrorMap = HashMap<FileName, Vec<FormattingError>>;
298
286
299
287
#[ derive( Default , Debug ) ]
300
288
pub ( crate ) struct ReportedErrors {
301
- pub ( crate ) has_operational_errors : bool ,
302
- pub ( crate ) has_check_errors : bool ,
303
- }
304
-
305
- /// A single span of changed lines, with 0 or more removed lines
306
- /// and a vector of 0 or more inserted lines.
307
- #[ derive( Debug , PartialEq , Eq ) ]
308
- pub ( crate ) struct ModifiedChunk {
309
- /// The first to be removed from the original text
310
- pub line_number_orig : u32 ,
311
- /// The number of lines which have been replaced
312
- pub lines_removed : u32 ,
313
- /// The new lines
314
- pub lines : Vec < String > ,
315
- }
316
-
317
- /// Set of changed sections of a file.
318
- #[ derive( Debug , PartialEq , Eq ) ]
319
- pub ( crate ) struct ModifiedLines {
320
- /// The set of changed chunks.
321
- pub chunks : Vec < ModifiedChunk > ,
322
- }
323
-
324
- /// A summary of a Rustfmt run.
325
- #[ derive( Debug , Default , Clone , Copy ) ]
326
- pub struct Summary {
327
289
// Encountered e.g. an IO error.
328
- has_operational_errors : bool ,
290
+ pub ( crate ) has_operational_errors : bool ,
329
291
330
292
// Failed to reformat code because of parsing errors.
331
- has_parsing_errors : bool ,
293
+ pub ( crate ) has_parsing_errors : bool ,
332
294
333
295
// Code is valid, but it is impossible to format it properly.
334
- has_formatting_errors : bool ,
296
+ pub ( crate ) has_formatting_errors : bool ,
335
297
336
298
// Code contains macro call that was unable to format.
337
299
pub ( crate ) has_macro_format_failure : bool ,
338
300
339
301
// Failed a check, such as the license check or other opt-in checking.
340
- has_check_errors : bool ,
302
+ pub ( crate ) has_check_errors : bool ,
341
303
342
304
/// Formatted code differs from existing code (--check only).
343
- pub has_diff : bool ,
305
+ pub ( crate ) has_diff : bool ,
344
306
}
345
307
346
- impl Summary {
347
- pub fn has_operational_errors ( & self ) -> bool {
348
- self . has_operational_errors
349
- }
350
-
351
- pub fn has_parsing_errors ( & self ) -> bool {
352
- self . has_parsing_errors
353
- }
354
-
355
- pub fn has_formatting_errors ( & self ) -> bool {
356
- self . has_formatting_errors
357
- }
358
-
359
- pub fn has_check_errors ( & self ) -> bool {
360
- self . has_check_errors
361
- }
362
-
363
- pub ( crate ) fn has_macro_formatting_failure ( & self ) -> bool {
364
- self . has_macro_format_failure
365
- }
366
-
367
- pub fn add_operational_error ( & mut self ) {
368
- self . has_operational_errors = true ;
369
- }
370
-
371
- pub ( crate ) fn add_parsing_error ( & mut self ) {
372
- self . has_parsing_errors = true ;
373
- }
374
-
375
- pub ( crate ) fn add_formatting_error ( & mut self ) {
376
- self . has_formatting_errors = true ;
377
- }
378
-
379
- pub ( crate ) fn add_check_error ( & mut self ) {
380
- self . has_check_errors = true ;
381
- }
382
-
383
- pub ( crate ) fn add_diff ( & mut self ) {
384
- self . has_diff = true ;
385
- }
386
-
387
- pub ( crate ) fn add_macro_format_failure ( & mut self ) {
388
- self . has_macro_format_failure = true ;
389
- }
390
-
391
- pub fn has_no_errors ( & self ) -> bool {
392
- !( self . has_operational_errors
393
- || self . has_parsing_errors
394
- || self . has_formatting_errors
395
- || self . has_diff )
396
- }
397
-
308
+ impl ReportedErrors {
398
309
/// Combine two summaries together.
399
- pub fn add ( & mut self , other : Summary ) {
310
+ pub fn add ( & mut self , other : & ReportedErrors ) {
400
311
self . has_operational_errors |= other. has_operational_errors ;
312
+ self . has_parsing_errors |= other. has_parsing_errors ;
401
313
self . has_formatting_errors |= other. has_formatting_errors ;
402
314
self . has_macro_format_failure |= other. has_macro_format_failure ;
403
- self . has_parsing_errors |= other. has_parsing_errors ;
404
315
self . has_check_errors |= other. has_check_errors ;
405
316
self . has_diff |= other. has_diff ;
406
317
}
407
318
}
408
319
320
+ /// A single span of changed lines, with 0 or more removed lines
321
+ /// and a vector of 0 or more inserted lines.
322
+ #[ derive( Debug , PartialEq , Eq ) ]
323
+ pub ( crate ) struct ModifiedChunk {
324
+ /// The first to be removed from the original text
325
+ pub line_number_orig : u32 ,
326
+ /// The number of lines which have been replaced
327
+ pub lines_removed : u32 ,
328
+ /// The new lines
329
+ pub lines : Vec < String > ,
330
+ }
331
+
332
+ /// Set of changed sections of a file.
333
+ #[ derive( Debug , PartialEq , Eq ) ]
334
+ pub ( crate ) struct ModifiedLines {
335
+ /// The set of changed chunks.
336
+ pub chunks : Vec < ModifiedChunk > ,
337
+ }
338
+
409
339
#[ derive( Clone , Copy , Debug ) ]
410
340
enum Timer {
411
341
Initialized ( Instant ) ,
@@ -647,7 +577,7 @@ fn parse_crate(
647
577
input : Input ,
648
578
parse_session : & ParseSess ,
649
579
config : & Config ,
650
- summary : & mut Summary ,
580
+ report : & mut FormatReport ,
651
581
) -> Result < ast:: Crate , ErrorKind > {
652
582
let input_is_stdin = input. is_text ( ) ;
653
583
@@ -685,7 +615,7 @@ fn parse_crate(
685
615
}
686
616
}
687
617
688
- summary . add_parsing_error ( ) ;
618
+ report . add_parsing_error ( ) ;
689
619
Err ( ErrorKind :: ParseError )
690
620
}
691
621
0 commit comments