@@ -63,6 +63,7 @@ const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
63
63
const PREPARE_FOR_ENV : & str = "__CARGO_FIX_PREPARE_FOR" ;
64
64
const EDITION_ENV : & str = "__CARGO_FIX_EDITION" ;
65
65
const IDIOMS_ENV : & str = "__CARGO_FIX_IDIOMS" ;
66
+ const CLIPPY_FIX_ARGS : & str = "__CARGO_FIX_CLIPPY_ARGS" ;
66
67
67
68
pub struct FixOptions < ' a > {
68
69
pub edition : bool ,
@@ -73,6 +74,7 @@ pub struct FixOptions<'a> {
73
74
pub allow_no_vcs : bool ,
74
75
pub allow_staged : bool ,
75
76
pub broken_code : bool ,
77
+ pub clippy_args : Option < Vec < String > > ,
76
78
}
77
79
78
80
pub fn fix ( ws : & Workspace < ' _ > , opts : & mut FixOptions < ' _ > ) -> CargoResult < ( ) > {
@@ -99,12 +101,38 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> {
99
101
wrapper. env ( IDIOMS_ENV , "1" ) ;
100
102
}
101
103
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
+
102
117
* opts
103
118
. compile_opts
104
119
. build_config
105
120
. rustfix_diagnostic_server
106
121
. 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) ;
108
136
109
137
ops:: compile ( ws, & opts. compile_opts ) ?;
110
138
Ok ( ( ) )
@@ -193,18 +221,10 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
193
221
trace ! ( "cargo-fix as rustc got file {:?}" , args. file) ;
194
222
let rustc = args. rustc . as_ref ( ) . expect ( "fix wrapper rustc was not set" ) ;
195
223
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.
202
224
let mut fixes = FixedCrate :: default ( ) ;
203
225
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) ?;
208
228
}
209
229
210
230
// 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> {
253
273
}
254
274
255
275
// This final fall-through handles multiple cases;
256
- // - Non-primary crates, which need to be built.
257
276
// - If the fix failed, show the original warnings and suggestions.
258
277
// - If `--broken-code`, show the error messages.
259
278
// - If the fix succeeded, show any remaining warnings.
@@ -563,8 +582,8 @@ struct FixArgs {
563
582
idioms : bool ,
564
583
enabled_edition : Option < String > ,
565
584
other : Vec < OsString > ,
566
- primary_package : bool ,
567
585
rustc : Option < PathBuf > ,
586
+ clippy_args : Vec < String > ,
568
587
}
569
588
570
589
enum PrepareFor {
@@ -582,7 +601,14 @@ impl Default for PrepareFor {
582
601
impl FixArgs {
583
602
fn get ( ) -> FixArgs {
584
603
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
+
586
612
for arg in env:: args_os ( ) . skip ( 2 ) {
587
613
let path = PathBuf :: from ( arg) ;
588
614
if path. extension ( ) . and_then ( |s| s. to_str ( ) ) == Some ( "rs" ) && path. exists ( ) {
@@ -608,26 +634,30 @@ impl FixArgs {
608
634
} else if env:: var ( EDITION_ENV ) . is_ok ( ) {
609
635
ret. prepare_for_edition = PrepareFor :: Next ;
610
636
}
637
+
611
638
ret. idioms = env:: var ( IDIOMS_ENV ) . is_ok ( ) ;
612
- ret. primary_package = env:: var ( "CARGO_PRIMARY_PACKAGE" ) . is_ok ( ) ;
613
639
ret
614
640
}
615
641
616
642
fn apply ( & self , cmd : & mut Command ) {
617
643
if let Some ( path) = & self . file {
618
644
cmd. arg ( path) ;
619
645
}
646
+
647
+ if !self . clippy_args . is_empty ( ) {
648
+ cmd. args ( & self . clippy_args ) ;
649
+ }
650
+
620
651
cmd. args ( & self . other ) . arg ( "--cap-lints=warn" ) ;
621
652
if let Some ( edition) = & self . enabled_edition {
622
653
cmd. arg ( "--edition" ) . arg ( edition) ;
623
- if self . idioms && self . primary_package && edition == "2018" {
654
+ if self . idioms && edition == "2018" {
624
655
cmd. arg ( "-Wrust-2018-idioms" ) ;
625
656
}
626
657
}
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) ) ;
631
661
}
632
662
}
633
663
0 commit comments