@@ -50,7 +50,7 @@ use log::{debug, trace, warn};
50
50
use rustfix:: diagnostics:: Diagnostic ;
51
51
use rustfix:: { self , CodeFix } ;
52
52
53
- use crate :: core:: Workspace ;
53
+ use crate :: core:: { Edition , Workspace } ;
54
54
use crate :: ops:: { self , CompileOptions } ;
55
55
use crate :: util:: diagnostic_server:: { Message , RustfixDiagnosticServer } ;
56
56
use crate :: util:: errors:: CargoResult ;
@@ -203,7 +203,7 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
203
203
Err ( _) => return Ok ( false ) ,
204
204
} ;
205
205
206
- let args = FixArgs :: get ( ) ;
206
+ let args = FixArgs :: get ( ) ? ;
207
207
trace ! ( "cargo-fix as rustc got file {:?}" , args. file) ;
208
208
209
209
let rustc = args. rustc . as_ref ( ) . expect ( "fix wrapper rustc was not set" ) ;
@@ -583,15 +583,15 @@ struct FixArgs {
583
583
file : Option < PathBuf > ,
584
584
prepare_for_edition : PrepareFor ,
585
585
idioms : bool ,
586
- enabled_edition : Option < String > ,
586
+ enabled_edition : Option < Edition > ,
587
587
other : Vec < OsString > ,
588
588
rustc : Option < PathBuf > ,
589
589
format_args : Vec < String > ,
590
590
}
591
591
592
592
enum PrepareFor {
593
593
Next ,
594
- Edition ( String ) ,
594
+ Edition ( Edition ) ,
595
595
None ,
596
596
}
597
597
@@ -602,7 +602,7 @@ impl Default for PrepareFor {
602
602
}
603
603
604
604
impl FixArgs {
605
- fn get ( ) -> FixArgs {
605
+ fn get ( ) -> Result < FixArgs , Error > {
606
606
let mut ret = FixArgs :: default ( ) ;
607
607
608
608
ret. rustc = env:: args_os ( ) . nth ( 1 ) . map ( PathBuf :: from) ;
@@ -615,7 +615,7 @@ impl FixArgs {
615
615
}
616
616
if let Some ( s) = path. to_str ( ) {
617
617
if let Some ( edition) = s. strip_prefix ( "--edition=" ) {
618
- ret. enabled_edition = Some ( edition. to_string ( ) ) ;
618
+ ret. enabled_edition = Some ( edition. parse ( ) ? ) ;
619
619
continue ;
620
620
}
621
621
if s. starts_with ( "--error-format=" ) || s. starts_with ( "--json=" ) {
@@ -628,13 +628,13 @@ impl FixArgs {
628
628
ret. other . push ( path. into ( ) ) ;
629
629
}
630
630
if let Ok ( s) = env:: var ( PREPARE_FOR_ENV ) {
631
- ret. prepare_for_edition = PrepareFor :: Edition ( s) ;
631
+ ret. prepare_for_edition = PrepareFor :: Edition ( s. parse ( ) ? ) ;
632
632
} else if env:: var ( EDITION_ENV ) . is_ok ( ) {
633
633
ret. prepare_for_edition = PrepareFor :: Next ;
634
634
}
635
635
636
636
ret. idioms = env:: var ( IDIOMS_ENV ) . is_ok ( ) ;
637
- ret
637
+ Ok ( ret)
638
638
}
639
639
640
640
fn apply ( & self , cmd : & mut Command ) {
@@ -643,9 +643,9 @@ impl FixArgs {
643
643
}
644
644
645
645
cmd. args ( & self . other ) . arg ( "--cap-lints=warn" ) ;
646
- if let Some ( edition) = & self . enabled_edition {
647
- cmd. arg ( "--edition" ) . arg ( edition) ;
648
- if self . idioms && edition == "2018" {
646
+ if let Some ( edition) = self . enabled_edition {
647
+ cmd. arg ( "--edition" ) . arg ( edition. to_string ( ) ) ;
648
+ if self . idioms && edition >= Edition :: Edition2018 {
649
649
cmd. arg ( "-Wrust-2018-idioms" ) ;
650
650
}
651
651
}
@@ -667,7 +667,7 @@ impl FixArgs {
667
667
Some ( s) => s,
668
668
None => return Ok ( ( ) ) ,
669
669
} ;
670
- let enabled = match & self . enabled_edition {
670
+ let enabled = match self . enabled_edition {
671
671
Some ( s) => s,
672
672
None => return Ok ( ( ) ) ,
673
673
} ;
@@ -688,23 +688,19 @@ impl FixArgs {
688
688
process:: exit ( 1 ) ;
689
689
}
690
690
691
- fn prepare_for_edition_resolve ( & self ) -> Option < & str > {
692
- match & self . prepare_for_edition {
691
+ fn prepare_for_edition_resolve ( & self ) -> Option < Edition > {
692
+ match self . prepare_for_edition {
693
693
PrepareFor :: Edition ( s) => Some ( s) ,
694
694
PrepareFor :: Next => Some ( self . next_edition ( ) ) ,
695
695
PrepareFor :: None => None ,
696
696
}
697
697
}
698
698
699
- fn next_edition ( & self ) -> & str {
700
- match self . enabled_edition . as_deref ( ) {
701
- // 2015 -> 2018,
702
- None | Some ( "2015" ) => "2018" ,
703
-
704
- // This'll probably be wrong in 2020, but that's future Cargo's
705
- // problem. Eventually though we'll just add more editions here as
706
- // necessary.
707
- _ => "2018" ,
699
+ fn next_edition ( & self ) -> Edition {
700
+ match self . enabled_edition {
701
+ None | Some ( Edition :: Edition2015 ) => Edition :: Edition2018 ,
702
+ Some ( Edition :: Edition2018 ) => Edition :: Edition2018 , // TODO: Change to 2021 when rustc is ready for it.
703
+ Some ( Edition :: Edition2021 ) => Edition :: Edition2021 ,
708
704
}
709
705
}
710
706
}
0 commit comments