@@ -31,9 +31,6 @@ use crate::rules::pycodestyle;
31
31
use crate :: settings:: { flags, Settings } ;
32
32
use crate :: { directives, fs} ;
33
33
34
- const CARGO_PKG_NAME : & str = env ! ( "CARGO_PKG_NAME" ) ;
35
- const CARGO_PKG_REPOSITORY : & str = env ! ( "CARGO_PKG_REPOSITORY" ) ;
36
-
37
34
/// A [`Result`]-like type that returns both data and an error. Used to return
38
35
/// diagnostics even in the face of parse errors, since many diagnostics can be
39
36
/// generated without a full AST.
@@ -453,24 +450,7 @@ pub fn lint_fix<'a>(
453
450
// longer parseable on a subsequent pass, then we've introduced a
454
451
// syntax error. Return the original code.
455
452
if parseable && result. error . is_some ( ) {
456
- #[ allow( clippy:: print_stderr) ]
457
- {
458
- eprintln ! (
459
- r#"
460
- {}: Autofix introduced a syntax error. Reverting all changes.
461
-
462
- This indicates a bug in `{}`. If you could open an issue at:
463
-
464
- {}/issues/new?title=%5BAutofix%20error%5D
465
-
466
- ...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
467
- "# ,
468
- "error" . red( ) . bold( ) ,
469
- CARGO_PKG_NAME ,
470
- CARGO_PKG_REPOSITORY ,
471
- fs:: relativize_path( path) ,
472
- ) ;
473
- }
453
+ report_autofix_syntax_error ( path, & transformed, & result. error . unwrap ( ) ) ;
474
454
return Err ( anyhow ! ( "Autofix introduced a syntax error" ) ) ;
475
455
}
476
456
}
@@ -493,25 +473,7 @@ This indicates a bug in `{}`. If you could open an issue at:
493
473
continue ;
494
474
}
495
475
496
- #[ allow( clippy:: print_stderr) ]
497
- {
498
- eprintln ! (
499
- r#"
500
- {}: Failed to converge after {} iterations.
501
-
502
- This indicates a bug in `{}`. If you could open an issue at:
503
-
504
- {}/issues/new?title=%5BInfinite%20loop%5D
505
-
506
- ...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
507
- "# ,
508
- "error" . red( ) . bold( ) ,
509
- MAX_ITERATIONS ,
510
- CARGO_PKG_NAME ,
511
- CARGO_PKG_REPOSITORY ,
512
- fs:: relativize_path( path) ,
513
- ) ;
514
- }
476
+ report_failed_to_converge_error ( path, & transformed) ;
515
477
}
516
478
517
479
return Ok ( FixerResult {
@@ -526,3 +488,68 @@ This indicates a bug in `{}`. If you could open an issue at:
526
488
} ) ;
527
489
}
528
490
}
491
+
492
+ #[ allow( clippy:: print_stderr) ]
493
+ fn report_failed_to_converge_error ( path : & Path , transformed : & str ) {
494
+ #[ cfg( debug_assertions) ]
495
+ {
496
+ eprintln ! (
497
+ "{}: Failed to converge after {} iterations in `{}`:---\n {}\n ---" ,
498
+ "debug error" . red( ) . bold( ) ,
499
+ MAX_ITERATIONS ,
500
+ fs:: relativize_path( path) ,
501
+ transformed,
502
+ ) ;
503
+ }
504
+ #[ cfg( not( debug_assertions) ) ]
505
+ {
506
+ eprintln ! (
507
+ r#"
508
+ {}: Failed to converge after {} iterations.
509
+
510
+ This indicates a bug in `{}`. If you could open an issue at:
511
+
512
+ {}/issues/new?title=%5BInfinite%20loop%5D
513
+
514
+ ...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
515
+ "# ,
516
+ "error" . red( ) . bold( ) ,
517
+ MAX_ITERATIONS ,
518
+ env!( "CARGO_PKG_NAME" ) ,
519
+ env!( "CARGO_PKG_REPOSITORY" ) ,
520
+ fs:: relativize_path( path) ,
521
+ ) ;
522
+ }
523
+ }
524
+
525
+ #[ allow( clippy:: print_stderr) ]
526
+ fn report_autofix_syntax_error ( path : & Path , transformed : & str , error : & ParseError ) {
527
+ #[ cfg( debug_assertions) ]
528
+ {
529
+ eprintln ! (
530
+ "{}: Autofix introduced a syntax error in `{}`:\n \n {}\n \n ---\n {}\n ---" ,
531
+ "debug error" . red( ) . bold( ) ,
532
+ fs:: relativize_path( path) ,
533
+ error,
534
+ transformed,
535
+ ) ;
536
+ }
537
+ #[ cfg( not( debug_assertions) ) ]
538
+ {
539
+ eprintln ! (
540
+ r#"
541
+ {}: Autofix introduced a syntax error. Reverting all changes.
542
+
543
+ This indicates a bug in `{}`. If you could open an issue at:
544
+
545
+ {}/issues/new?title=%5BAutofix%20error%5D
546
+
547
+ ...quoting the contents of `{}`, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!
548
+ "# ,
549
+ "error" . red( ) . bold( ) ,
550
+ env!( "CARGO_PKG_NAME" ) ,
551
+ env!( "CARGO_PKG_REPOSITORY" ) ,
552
+ fs:: relativize_path( path) ,
553
+ ) ;
554
+ }
555
+ }
0 commit comments