11#![ feature( bool_to_option) ]
2+ #![ feature( command_access) ]
23#![ cfg_attr( feature = "deny-warnings" , deny( warnings) ) ]
34// warn on lints, that are included in `rust-lang/rust`s bootstrap
45#![ warn( rust_2018_idioms, unused_lifetimes) ]
@@ -63,12 +64,11 @@ struct ClippyCmd {
6364 unstable_options : bool ,
6465 cargo_subcommand : & ' static str ,
6566 args : Vec < String > ,
66- rustflags : Option < String > ,
6767 clippy_args : Option < String > ,
6868}
6969
7070impl ClippyCmd {
71- fn new < I > ( mut old_args : I , rustflags : Option < String > ) -> Self
71+ fn new < I > ( mut old_args : I ) -> Self
7272 where
7373 I : Iterator < Item = String > ,
7474 {
@@ -111,8 +111,6 @@ impl ClippyCmd {
111111 unstable_options,
112112 cargo_subcommand,
113113 args,
114- rustflags : has_args
115- . then ( || rustflags. map_or_else ( || clippy_args. clone ( ) , |flags| format ! ( "{} {}" , clippy_args, flags) ) ) ,
116114 clippy_args : has_args. then_some ( clippy_args) ,
117115 }
118116 }
@@ -153,7 +151,7 @@ impl ClippyCmd {
153151 . map ( |p| ( "CARGO_TARGET_DIR" , p) )
154152 }
155153
156- fn into_std_cmd ( self ) -> Command {
154+ fn into_std_cmd ( self , rustflags : Option < String > ) -> Command {
157155 let mut cmd = Command :: new ( "cargo" ) ;
158156
159157 cmd. env ( self . path_env ( ) , Self :: path ( ) )
@@ -163,9 +161,12 @@ impl ClippyCmd {
163161
164162 // HACK: pass Clippy args to the driver *also* through RUSTFLAGS.
165163 // This guarantees that new builds will be triggered when Clippy flags change.
166- if let ( Some ( clippy_args) , Some ( rustflags) ) = ( self . clippy_args , self . rustflags ) {
164+ if let Some ( clippy_args) = self . clippy_args {
165+ cmd. env (
166+ "RUSTFLAGS" ,
167+ rustflags. map_or ( clippy_args. clone ( ) , |flags| format ! ( "{} {}" , clippy_args, flags) ) ,
168+ ) ;
167169 cmd. env ( "CLIPPY_ARGS" , clippy_args) ;
168- cmd. env ( "RUSTFLAGS" , rustflags) ;
169170 }
170171
171172 cmd
@@ -176,9 +177,9 @@ fn process<I>(old_args: I) -> Result<(), i32>
176177where
177178 I : Iterator < Item = String > ,
178179{
179- let cmd = ClippyCmd :: new ( old_args, env :: var ( "RUSTFLAGS" ) . ok ( ) ) ;
180+ let cmd = ClippyCmd :: new ( old_args) ;
180181
181- let mut cmd = cmd. into_std_cmd ( ) ;
182+ let mut cmd = cmd. into_std_cmd ( env :: var ( "RUSTFLAGS" ) . ok ( ) ) ;
182183
183184 let exit_status = cmd
184185 . spawn ( )
@@ -196,20 +197,21 @@ where
196197#[ cfg( test) ]
197198mod tests {
198199 use super :: ClippyCmd ;
200+ use std:: ffi:: OsStr ;
199201
200202 #[ test]
201203 #[ should_panic]
202204 fn fix_without_unstable ( ) {
203205 let args = "cargo clippy --fix" . split_whitespace ( ) . map ( ToString :: to_string) ;
204- let _ = ClippyCmd :: new ( args, None ) ;
206+ let _ = ClippyCmd :: new ( args) ;
205207 }
206208
207209 #[ test]
208210 fn fix_unstable ( ) {
209211 let args = "cargo clippy --fix -Zunstable-options"
210212 . split_whitespace ( )
211213 . map ( ToString :: to_string) ;
212- let cmd = ClippyCmd :: new ( args, None ) ;
214+ let cmd = ClippyCmd :: new ( args) ;
213215
214216 assert_eq ! ( "fix" , cmd. cargo_subcommand) ;
215217 assert_eq ! ( "RUSTC_WORKSPACE_WRAPPER" , cmd. path_env( ) ) ;
@@ -221,7 +223,7 @@ mod tests {
221223 let args = "cargo clippy --fix -Zunstable-options"
222224 . split_whitespace ( )
223225 . map ( ToString :: to_string) ;
224- let cmd = ClippyCmd :: new ( args, None ) ;
226+ let cmd = ClippyCmd :: new ( args) ;
225227
226228 assert ! ( cmd. clippy_args. unwrap( ) . contains( "--no-deps" ) ) ;
227229 }
@@ -231,15 +233,15 @@ mod tests {
231233 let args = "cargo clippy --fix -Zunstable-options -- --no-deps"
232234 . split_whitespace ( )
233235 . map ( ToString :: to_string) ;
234- let cmd = ClippyCmd :: new ( args, None ) ;
236+ let cmd = ClippyCmd :: new ( args) ;
235237
236238 assert_eq ! ( 1 , cmd. clippy_args. unwrap( ) . matches( "--no-deps" ) . count( ) ) ;
237239 }
238240
239241 #[ test]
240242 fn check ( ) {
241243 let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
242- let cmd = ClippyCmd :: new ( args, None ) ;
244+ let cmd = ClippyCmd :: new ( args) ;
243245
244246 assert_eq ! ( "check" , cmd. cargo_subcommand) ;
245247 assert_eq ! ( "RUSTC_WRAPPER" , cmd. path_env( ) ) ;
@@ -250,7 +252,7 @@ mod tests {
250252 let args = "cargo clippy -Zunstable-options"
251253 . split_whitespace ( )
252254 . map ( ToString :: to_string) ;
253- let cmd = ClippyCmd :: new ( args, None ) ;
255+ let cmd = ClippyCmd :: new ( args) ;
254256
255257 assert_eq ! ( "check" , cmd. cargo_subcommand) ;
256258 assert_eq ! ( "RUSTC_WORKSPACE_WRAPPER" , cmd. path_env( ) ) ;
@@ -261,43 +263,53 @@ mod tests {
261263 let args = "cargo clippy -- -W clippy::as_conversions"
262264 . split_whitespace ( )
263265 . map ( ToString :: to_string) ;
266+ let cmd = ClippyCmd :: new ( args) ;
267+
264268 let rustflags = None ;
265- let cmd = ClippyCmd :: new ( args , rustflags) ;
269+ let cmd = cmd . into_std_cmd ( rustflags) ;
266270
267- assert_eq ! ( "-W clippy::as_conversions" , cmd. rustflags. unwrap( ) ) ;
271+ assert ! ( cmd
272+ . get_envs( )
273+ . any( |( key, val) | key == "RUSTFLAGS" && val == Some ( OsStr :: new( "-W clippy::as_conversions" ) ) ) ) ;
268274 }
269275
270276 #[ test]
271277 fn clippy_args_respect_existing_rustflags ( ) {
272278 let args = "cargo clippy -- -D clippy::await_holding_lock"
273279 . split_whitespace ( )
274280 . map ( ToString :: to_string) ;
281+ let cmd = ClippyCmd :: new ( args) ;
282+
275283 let rustflags = Some ( r#"--cfg feature="some_feat""# . into ( ) ) ;
276- let cmd = ClippyCmd :: new ( args , rustflags) ;
284+ let cmd = cmd . into_std_cmd ( rustflags) ;
277285
278- assert_eq ! (
279- r#"-D clippy::await_holding_lock --cfg feature="some_feat""# ,
280- cmd. rustflags. unwrap( )
281- ) ;
286+ assert ! ( cmd. get_envs( ) . any( |( key, val) | key == "RUSTFLAGS"
287+ && val == Some ( OsStr :: new( r#"-D clippy::await_holding_lock --cfg feature="some_feat""# ) ) ) ) ;
282288 }
283289
284290 #[ test]
285291 fn no_env_change_if_no_clippy_args ( ) {
286292 let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
293+ let cmd = ClippyCmd :: new ( args) ;
294+
287295 let rustflags = Some ( r#"--cfg feature="some_feat""# . into ( ) ) ;
288- let cmd = ClippyCmd :: new ( args , rustflags) ;
296+ let cmd = cmd . into_std_cmd ( rustflags) ;
289297
290- assert ! ( cmd. clippy_args. is_none( ) ) ;
291- assert ! ( cmd. rustflags. is_none( ) ) ;
298+ assert ! ( !cmd
299+ . get_envs( )
300+ . any( |( key, _) | key == "RUSTFLAGS" || key == "CLIPPY_ARGS" ) ) ;
292301 }
293302
294303 #[ test]
295304 fn no_env_change_if_no_clippy_args_nor_rustflags ( ) {
296305 let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
306+ let cmd = ClippyCmd :: new ( args) ;
307+
297308 let rustflags = None ;
298- let cmd = ClippyCmd :: new ( args , rustflags) ;
309+ let cmd = cmd . into_std_cmd ( rustflags) ;
299310
300- assert ! ( cmd. clippy_args. is_none( ) ) ;
301- assert ! ( cmd. rustflags. is_none( ) ) ;
311+ assert ! ( !cmd
312+ . get_envs( )
313+ . any( |( key, _) | key == "RUSTFLAGS" || key == "CLIPPY_ARGS" ) )
302314 }
303315}
0 commit comments