1
1
#![ feature( bool_to_option) ]
2
+ #![ feature( command_access) ]
2
3
#![ cfg_attr( feature = "deny-warnings" , deny( warnings) ) ]
3
4
// warn on lints, that are included in `rust-lang/rust`s bootstrap
4
5
#![ warn( rust_2018_idioms, unused_lifetimes) ]
@@ -63,12 +64,11 @@ struct ClippyCmd {
63
64
unstable_options : bool ,
64
65
cargo_subcommand : & ' static str ,
65
66
args : Vec < String > ,
66
- rustflags : Option < String > ,
67
67
clippy_args : Option < String > ,
68
68
}
69
69
70
70
impl ClippyCmd {
71
- fn new < I > ( mut old_args : I , rustflags : Option < String > ) -> Self
71
+ fn new < I > ( mut old_args : I ) -> Self
72
72
where
73
73
I : Iterator < Item = String > ,
74
74
{
@@ -111,8 +111,6 @@ impl ClippyCmd {
111
111
unstable_options,
112
112
cargo_subcommand,
113
113
args,
114
- rustflags : has_args
115
- . then ( || rustflags. map_or_else ( || clippy_args. clone ( ) , |flags| format ! ( "{} {}" , clippy_args, flags) ) ) ,
116
114
clippy_args : has_args. then_some ( clippy_args) ,
117
115
}
118
116
}
@@ -153,7 +151,7 @@ impl ClippyCmd {
153
151
. map ( |p| ( "CARGO_TARGET_DIR" , p) )
154
152
}
155
153
156
- fn into_std_cmd ( self ) -> Command {
154
+ fn into_std_cmd ( self , rustflags : Option < String > ) -> Command {
157
155
let mut cmd = Command :: new ( "cargo" ) ;
158
156
159
157
cmd. env ( self . path_env ( ) , Self :: path ( ) )
@@ -163,9 +161,12 @@ impl ClippyCmd {
163
161
164
162
// HACK: pass Clippy args to the driver *also* through RUSTFLAGS.
165
163
// 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
+ ) ;
167
169
cmd. env ( "CLIPPY_ARGS" , clippy_args) ;
168
- cmd. env ( "RUSTFLAGS" , rustflags) ;
169
170
}
170
171
171
172
cmd
@@ -176,9 +177,9 @@ fn process<I>(old_args: I) -> Result<(), i32>
176
177
where
177
178
I : Iterator < Item = String > ,
178
179
{
179
- let cmd = ClippyCmd :: new ( old_args, env :: var ( "RUSTFLAGS" ) . ok ( ) ) ;
180
+ let cmd = ClippyCmd :: new ( old_args) ;
180
181
181
- let mut cmd = cmd. into_std_cmd ( ) ;
182
+ let mut cmd = cmd. into_std_cmd ( env :: var ( "RUSTFLAGS" ) . ok ( ) ) ;
182
183
183
184
let exit_status = cmd
184
185
. spawn ( )
@@ -196,20 +197,21 @@ where
196
197
#[ cfg( test) ]
197
198
mod tests {
198
199
use super :: ClippyCmd ;
200
+ use std:: ffi:: OsStr ;
199
201
200
202
#[ test]
201
203
#[ should_panic]
202
204
fn fix_without_unstable ( ) {
203
205
let args = "cargo clippy --fix" . split_whitespace ( ) . map ( ToString :: to_string) ;
204
- let _ = ClippyCmd :: new ( args, None ) ;
206
+ let _ = ClippyCmd :: new ( args) ;
205
207
}
206
208
207
209
#[ test]
208
210
fn fix_unstable ( ) {
209
211
let args = "cargo clippy --fix -Zunstable-options"
210
212
. split_whitespace ( )
211
213
. map ( ToString :: to_string) ;
212
- let cmd = ClippyCmd :: new ( args, None ) ;
214
+ let cmd = ClippyCmd :: new ( args) ;
213
215
214
216
assert_eq ! ( "fix" , cmd. cargo_subcommand) ;
215
217
assert_eq ! ( "RUSTC_WORKSPACE_WRAPPER" , cmd. path_env( ) ) ;
@@ -221,7 +223,7 @@ mod tests {
221
223
let args = "cargo clippy --fix -Zunstable-options"
222
224
. split_whitespace ( )
223
225
. map ( ToString :: to_string) ;
224
- let cmd = ClippyCmd :: new ( args, None ) ;
226
+ let cmd = ClippyCmd :: new ( args) ;
225
227
226
228
assert ! ( cmd. clippy_args. unwrap( ) . contains( "--no-deps" ) ) ;
227
229
}
@@ -231,15 +233,15 @@ mod tests {
231
233
let args = "cargo clippy --fix -Zunstable-options -- --no-deps"
232
234
. split_whitespace ( )
233
235
. map ( ToString :: to_string) ;
234
- let cmd = ClippyCmd :: new ( args, None ) ;
236
+ let cmd = ClippyCmd :: new ( args) ;
235
237
236
238
assert_eq ! ( 1 , cmd. clippy_args. unwrap( ) . matches( "--no-deps" ) . count( ) ) ;
237
239
}
238
240
239
241
#[ test]
240
242
fn check ( ) {
241
243
let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
242
- let cmd = ClippyCmd :: new ( args, None ) ;
244
+ let cmd = ClippyCmd :: new ( args) ;
243
245
244
246
assert_eq ! ( "check" , cmd. cargo_subcommand) ;
245
247
assert_eq ! ( "RUSTC_WRAPPER" , cmd. path_env( ) ) ;
@@ -250,7 +252,7 @@ mod tests {
250
252
let args = "cargo clippy -Zunstable-options"
251
253
. split_whitespace ( )
252
254
. map ( ToString :: to_string) ;
253
- let cmd = ClippyCmd :: new ( args, None ) ;
255
+ let cmd = ClippyCmd :: new ( args) ;
254
256
255
257
assert_eq ! ( "check" , cmd. cargo_subcommand) ;
256
258
assert_eq ! ( "RUSTC_WORKSPACE_WRAPPER" , cmd. path_env( ) ) ;
@@ -261,43 +263,53 @@ mod tests {
261
263
let args = "cargo clippy -- -W clippy::as_conversions"
262
264
. split_whitespace ( )
263
265
. map ( ToString :: to_string) ;
266
+ let cmd = ClippyCmd :: new ( args) ;
267
+
264
268
let rustflags = None ;
265
- let cmd = ClippyCmd :: new ( args , rustflags) ;
269
+ let cmd = cmd . into_std_cmd ( rustflags) ;
266
270
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" ) ) ) ) ;
268
274
}
269
275
270
276
#[ test]
271
277
fn clippy_args_respect_existing_rustflags ( ) {
272
278
let args = "cargo clippy -- -D clippy::await_holding_lock"
273
279
. split_whitespace ( )
274
280
. map ( ToString :: to_string) ;
281
+ let cmd = ClippyCmd :: new ( args) ;
282
+
275
283
let rustflags = Some ( r#"--cfg feature="some_feat""# . into ( ) ) ;
276
- let cmd = ClippyCmd :: new ( args , rustflags) ;
284
+ let cmd = cmd . into_std_cmd ( rustflags) ;
277
285
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""# ) ) ) ) ;
282
288
}
283
289
284
290
#[ test]
285
291
fn no_env_change_if_no_clippy_args ( ) {
286
292
let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
293
+ let cmd = ClippyCmd :: new ( args) ;
294
+
287
295
let rustflags = Some ( r#"--cfg feature="some_feat""# . into ( ) ) ;
288
- let cmd = ClippyCmd :: new ( args , rustflags) ;
296
+ let cmd = cmd . into_std_cmd ( rustflags) ;
289
297
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" ) ) ;
292
301
}
293
302
294
303
#[ test]
295
304
fn no_env_change_if_no_clippy_args_nor_rustflags ( ) {
296
305
let args = "cargo clippy" . split_whitespace ( ) . map ( ToString :: to_string) ;
306
+ let cmd = ClippyCmd :: new ( args) ;
307
+
297
308
let rustflags = None ;
298
- let cmd = ClippyCmd :: new ( args , rustflags) ;
309
+ let cmd = cmd . into_std_cmd ( rustflags) ;
299
310
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" ) )
302
314
}
303
315
}
0 commit comments