@@ -190,6 +190,30 @@ impl Sub<usize> for Indent {
190
190
}
191
191
}
192
192
193
+
194
+ #[ must_use]
195
+ pub struct ErrorSummary {
196
+ has_errors : bool ,
197
+ }
198
+
199
+ impl ErrorSummary {
200
+ pub fn new ( ) -> ErrorSummary {
201
+ ErrorSummary { has_errors : false }
202
+ }
203
+
204
+ pub fn add_error ( & mut self ) {
205
+ self . has_errors = true ;
206
+ }
207
+
208
+ pub fn has_errors ( & self ) -> bool {
209
+ self . has_errors
210
+ }
211
+
212
+ pub fn add ( & mut self , other : ErrorSummary ) {
213
+ self . has_errors |= other. has_errors ;
214
+ }
215
+ }
216
+
193
217
pub enum ErrorKind {
194
218
// Line has exceeded character limit
195
219
LineOverflow ,
@@ -239,9 +263,17 @@ pub struct FormatReport {
239
263
}
240
264
241
265
impl FormatReport {
266
+ fn new ( ) -> FormatReport {
267
+ FormatReport { file_error_map : HashMap :: new ( ) }
268
+ }
269
+
242
270
pub fn warning_count ( & self ) -> usize {
243
271
self . file_error_map . iter ( ) . map ( |( _, ref errors) | errors. len ( ) ) . fold ( 0 , |acc, x| acc + x)
244
272
}
273
+
274
+ pub fn has_warnings ( & self ) -> bool {
275
+ self . warning_count ( ) > 0
276
+ }
245
277
}
246
278
247
279
impl fmt:: Display for FormatReport {
@@ -289,7 +321,7 @@ fn format_ast(krate: &ast::Crate,
289
321
// TODO(#20) other stuff for parity with make tidy
290
322
fn format_lines ( file_map : & mut FileMap , config : & Config ) -> FormatReport {
291
323
let mut truncate_todo = Vec :: new ( ) ;
292
- let mut report = FormatReport { file_error_map : HashMap :: new ( ) } ;
324
+ let mut report = FormatReport :: new ( ) ;
293
325
294
326
// Iterate over the chars in the file map.
295
327
for ( f, text) in file_map. iter ( ) {
@@ -378,7 +410,8 @@ fn parse_input(input: Input, parse_session: &ParseSess) -> Result<ast::Crate, Di
378
410
krate
379
411
}
380
412
381
- pub fn format_input ( input : Input , config : & Config ) -> ( FileMap , FormatReport ) {
413
+ pub fn format_input ( input : Input , config : & Config ) -> ( ErrorSummary , FileMap , FormatReport ) {
414
+ let mut error_summary = ErrorSummary :: new ( ) ;
382
415
let codemap = Rc :: new ( CodeMap :: new ( ) ) ;
383
416
384
417
let tty_handler = Handler :: with_tty_emitter ( ColorConfig :: Auto ,
@@ -397,10 +430,15 @@ pub fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
397
430
Ok ( krate) => krate,
398
431
Err ( mut diagnostic) => {
399
432
diagnostic. emit ( ) ;
400
- panic ! ( "Unrecoverable parse error" ) ;
433
+ error_summary. add_error ( ) ;
434
+ return ( error_summary, FileMap :: new ( ) , FormatReport :: new ( ) ) ;
401
435
}
402
436
} ;
403
437
438
+ if parse_session. span_diagnostic . has_errors ( ) {
439
+ error_summary. add_error ( ) ;
440
+ }
441
+
404
442
// Suppress error output after parsing.
405
443
let silent_emitter = Box :: new ( EmitterWriter :: new ( Box :: new ( Vec :: new ( ) ) , None , codemap. clone ( ) ) ) ;
406
444
parse_session. span_diagnostic = Handler :: with_emitter ( true , false , silent_emitter) ;
@@ -412,16 +450,19 @@ pub fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
412
450
filemap:: append_newlines ( & mut file_map) ;
413
451
414
452
let report = format_lines ( & mut file_map, config) ;
415
- ( file_map, report)
453
+ if report. has_warnings ( ) {
454
+ error_summary. add_error ( ) ;
455
+ }
456
+ ( error_summary, file_map, report)
416
457
}
417
458
418
459
pub enum Input {
419
460
File ( PathBuf ) ,
420
461
Text ( String ) ,
421
462
}
422
463
423
- pub fn run ( input : Input , config : & Config ) {
424
- let ( file_map, report) = format_input ( input, config) ;
464
+ pub fn run ( input : Input , config : & Config ) -> ErrorSummary {
465
+ let ( mut error_summary , file_map, report) = format_input ( input, config) ;
425
466
426
467
let ignore_errors = config. write_mode == WriteMode :: Plain ;
427
468
if !ignore_errors {
@@ -433,5 +474,8 @@ pub fn run(input: Input, config: &Config) {
433
474
434
475
if let Err ( msg) = write_result {
435
476
msg ! ( "Error writing files: {}" , msg) ;
477
+ error_summary. add_error ( ) ;
436
478
}
479
+
480
+ error_summary
437
481
}
0 commit comments