@@ -50,7 +50,7 @@ use log::{debug, trace, warn};
5050use rustfix:: diagnostics:: Diagnostic ;
5151use rustfix:: { self , CodeFix } ;
5252
53- use crate :: core:: Workspace ;
53+ use crate :: core:: { Edition , Workspace } ;
5454use crate :: ops:: { self , CompileOptions } ;
5555use crate :: util:: diagnostic_server:: { Message , RustfixDiagnosticServer } ;
5656use crate :: util:: errors:: CargoResult ;
@@ -203,7 +203,7 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
203203 Err ( _) => return Ok ( false ) ,
204204 } ;
205205
206- let args = FixArgs :: get ( ) ;
206+ let args = FixArgs :: get ( ) ? ;
207207 trace ! ( "cargo-fix as rustc got file {:?}" , args. file) ;
208208
209209 let rustc = args. rustc . as_ref ( ) . expect ( "fix wrapper rustc was not set" ) ;
@@ -583,15 +583,15 @@ struct FixArgs {
583583 file : Option < PathBuf > ,
584584 prepare_for_edition : PrepareFor ,
585585 idioms : bool ,
586- enabled_edition : Option < String > ,
586+ enabled_edition : Option < Edition > ,
587587 other : Vec < OsString > ,
588588 rustc : Option < PathBuf > ,
589589 format_args : Vec < String > ,
590590}
591591
592592enum PrepareFor {
593593 Next ,
594- Edition ( String ) ,
594+ Edition ( Edition ) ,
595595 None ,
596596}
597597
@@ -602,7 +602,7 @@ impl Default for PrepareFor {
602602}
603603
604604impl FixArgs {
605- fn get ( ) -> FixArgs {
605+ fn get ( ) -> Result < FixArgs , Error > {
606606 let mut ret = FixArgs :: default ( ) ;
607607
608608 ret. rustc = env:: args_os ( ) . nth ( 1 ) . map ( PathBuf :: from) ;
@@ -615,7 +615,7 @@ impl FixArgs {
615615 }
616616 if let Some ( s) = path. to_str ( ) {
617617 if let Some ( edition) = s. strip_prefix ( "--edition=" ) {
618- ret. enabled_edition = Some ( edition. to_string ( ) ) ;
618+ ret. enabled_edition = Some ( edition. parse ( ) ? ) ;
619619 continue ;
620620 }
621621 if s. starts_with ( "--error-format=" ) || s. starts_with ( "--json=" ) {
@@ -628,13 +628,13 @@ impl FixArgs {
628628 ret. other . push ( path. into ( ) ) ;
629629 }
630630 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 ( ) ? ) ;
632632 } else if env:: var ( EDITION_ENV ) . is_ok ( ) {
633633 ret. prepare_for_edition = PrepareFor :: Next ;
634634 }
635635
636636 ret. idioms = env:: var ( IDIOMS_ENV ) . is_ok ( ) ;
637- ret
637+ Ok ( ret)
638638 }
639639
640640 fn apply ( & self , cmd : & mut Command ) {
@@ -643,9 +643,9 @@ impl FixArgs {
643643 }
644644
645645 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 {
649649 cmd. arg ( "-Wrust-2018-idioms" ) ;
650650 }
651651 }
@@ -667,7 +667,7 @@ impl FixArgs {
667667 Some ( s) => s,
668668 None => return Ok ( ( ) ) ,
669669 } ;
670- let enabled = match & self . enabled_edition {
670+ let enabled = match self . enabled_edition {
671671 Some ( s) => s,
672672 None => return Ok ( ( ) ) ,
673673 } ;
@@ -688,23 +688,19 @@ impl FixArgs {
688688 process:: exit ( 1 ) ;
689689 }
690690
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 {
693693 PrepareFor :: Edition ( s) => Some ( s) ,
694694 PrepareFor :: Next => Some ( self . next_edition ( ) ) ,
695695 PrepareFor :: None => None ,
696696 }
697697 }
698698
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 ,
708704 }
709705 }
710706}
0 commit comments