@@ -63,6 +63,7 @@ const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
6363const PREPARE_FOR_ENV : & str = "__CARGO_FIX_PREPARE_FOR" ;
6464const EDITION_ENV : & str = "__CARGO_FIX_EDITION" ;
6565const IDIOMS_ENV : & str = "__CARGO_FIX_IDIOMS" ;
66+ const CLIPPY_FIX_ARGS : & str = "__CARGO_FIX_CLIPPY_ARGS" ;
6667
6768pub struct FixOptions < ' a > {
6869 pub edition : bool ,
@@ -73,6 +74,7 @@ pub struct FixOptions<'a> {
7374 pub allow_no_vcs : bool ,
7475 pub allow_staged : bool ,
7576 pub broken_code : bool ,
77+ pub clippy_args : Option < Vec < String > > ,
7678}
7779
7880pub fn fix ( ws : & Workspace < ' _ > , opts : & mut FixOptions < ' _ > ) -> CargoResult < ( ) > {
@@ -99,12 +101,38 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> {
99101 wrapper. env ( IDIOMS_ENV , "1" ) ;
100102 }
101103
104+ if opts. clippy_args . is_some ( ) {
105+ if let Err ( e) = util:: process ( "clippy-driver" ) . arg ( "-V" ) . exec_with_output ( ) {
106+ eprintln ! ( "Warning: clippy-driver not found: {:?}" , e) ;
107+ }
108+
109+ let clippy_args = opts
110+ . clippy_args
111+ . as_ref ( )
112+ . map_or_else ( String :: new, |args| serde_json:: to_string ( & args) . unwrap ( ) ) ;
113+
114+ wrapper. env ( CLIPPY_FIX_ARGS , clippy_args) ;
115+ }
116+
102117 * opts
103118 . compile_opts
104119 . build_config
105120 . rustfix_diagnostic_server
106121 . borrow_mut ( ) = Some ( RustfixDiagnosticServer :: new ( ) ?) ;
107- opts. compile_opts . build_config . rustc_wrapper = Some ( wrapper) ;
122+
123+ if let Some ( server) = opts
124+ . compile_opts
125+ . build_config
126+ . rustfix_diagnostic_server
127+ . borrow ( )
128+ . as_ref ( )
129+ {
130+ server. configure ( & mut wrapper) ;
131+ }
132+
133+ // primary crates are compiled using a cargo subprocess to do extra work of applying fixes and
134+ // repeating build until there are no more changes to be applied
135+ opts. compile_opts . build_config . primary_unit_rustc = Some ( wrapper) ;
108136
109137 ops:: compile ( ws, & opts. compile_opts ) ?;
110138 Ok ( ( ) )
@@ -193,18 +221,10 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
193221 trace ! ( "cargo-fix as rustc got file {:?}" , args. file) ;
194222 let rustc = args. rustc . as_ref ( ) . expect ( "fix wrapper rustc was not set" ) ;
195223
196- // Our goal is to fix only the crates that the end user is interested in.
197- // That's very likely to only mean the crates in the workspace the user is
198- // working on, not random crates.io crates.
199- //
200- // The master cargo process tells us whether or not this is a "primary"
201- // crate via the CARGO_PRIMARY_PACKAGE environment variable.
202224 let mut fixes = FixedCrate :: default ( ) ;
203225 if let Some ( path) = & args. file {
204- if args. primary_package {
205- trace ! ( "start rustfixing {:?}" , path) ;
206- fixes = rustfix_crate ( & lock_addr, rustc. as_ref ( ) , path, & args) ?;
207- }
226+ trace ! ( "start rustfixing {:?}" , path) ;
227+ fixes = rustfix_crate ( & lock_addr, rustc. as_ref ( ) , path, & args) ?;
208228 }
209229
210230 // Ok now we have our final goal of testing out the changes that we applied.
@@ -253,7 +273,6 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
253273 }
254274
255275 // This final fall-through handles multiple cases;
256- // - Non-primary crates, which need to be built.
257276 // - If the fix failed, show the original warnings and suggestions.
258277 // - If `--broken-code`, show the error messages.
259278 // - If the fix succeeded, show any remaining warnings.
@@ -563,8 +582,8 @@ struct FixArgs {
563582 idioms : bool ,
564583 enabled_edition : Option < String > ,
565584 other : Vec < OsString > ,
566- primary_package : bool ,
567585 rustc : Option < PathBuf > ,
586+ clippy_args : Vec < String > ,
568587}
569588
570589enum PrepareFor {
@@ -582,7 +601,14 @@ impl Default for PrepareFor {
582601impl FixArgs {
583602 fn get ( ) -> FixArgs {
584603 let mut ret = FixArgs :: default ( ) ;
585- ret. rustc = env:: args_os ( ) . nth ( 1 ) . map ( PathBuf :: from) ;
604+
605+ if let Ok ( clippy_args) = env:: var ( CLIPPY_FIX_ARGS ) {
606+ ret. clippy_args = serde_json:: from_str ( & clippy_args) . unwrap ( ) ;
607+ ret. rustc = Some ( util:: config:: clippy_driver ( ) ) ;
608+ } else {
609+ ret. rustc = env:: args_os ( ) . nth ( 1 ) . map ( PathBuf :: from) ;
610+ }
611+
586612 for arg in env:: args_os ( ) . skip ( 2 ) {
587613 let path = PathBuf :: from ( arg) ;
588614 if path. extension ( ) . and_then ( |s| s. to_str ( ) ) == Some ( "rs" ) && path. exists ( ) {
@@ -608,26 +634,30 @@ impl FixArgs {
608634 } else if env:: var ( EDITION_ENV ) . is_ok ( ) {
609635 ret. prepare_for_edition = PrepareFor :: Next ;
610636 }
637+
611638 ret. idioms = env:: var ( IDIOMS_ENV ) . is_ok ( ) ;
612- ret. primary_package = env:: var ( "CARGO_PRIMARY_PACKAGE" ) . is_ok ( ) ;
613639 ret
614640 }
615641
616642 fn apply ( & self , cmd : & mut Command ) {
617643 if let Some ( path) = & self . file {
618644 cmd. arg ( path) ;
619645 }
646+
647+ if !self . clippy_args . is_empty ( ) {
648+ cmd. args ( & self . clippy_args ) ;
649+ }
650+
620651 cmd. args ( & self . other ) . arg ( "--cap-lints=warn" ) ;
621652 if let Some ( edition) = & self . enabled_edition {
622653 cmd. arg ( "--edition" ) . arg ( edition) ;
623- if self . idioms && self . primary_package && edition == "2018" {
654+ if self . idioms && edition == "2018" {
624655 cmd. arg ( "-Wrust-2018-idioms" ) ;
625656 }
626657 }
627- if self . primary_package {
628- if let Some ( edition) = self . prepare_for_edition_resolve ( ) {
629- cmd. arg ( "-W" ) . arg ( format ! ( "rust-{}-compatibility" , edition) ) ;
630- }
658+
659+ if let Some ( edition) = self . prepare_for_edition_resolve ( ) {
660+ cmd. arg ( "-W" ) . arg ( format ! ( "rust-{}-compatibility" , edition) ) ;
631661 }
632662 }
633663
0 commit comments