@@ -22,20 +22,15 @@ use rustc_span::symbol::Symbol;
22
22
23
23
use std:: env;
24
24
use std:: fs:: read_to_string;
25
- use std:: ops:: Deref ;
26
25
use std:: path:: Path ;
27
26
use std:: process:: exit;
28
27
29
28
use anstream:: println;
30
29
31
30
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
32
31
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
33
- fn arg_value < ' a , T : Deref < Target = str > > (
34
- args : & ' a [ T ] ,
35
- find_arg : & str ,
36
- pred : impl Fn ( & str ) -> bool ,
37
- ) -> Option < & ' a str > {
38
- let mut args = args. iter ( ) . map ( Deref :: deref) ;
32
+ fn arg_value < ' a > ( args : & ' a [ String ] , find_arg : & str , pred : impl Fn ( & str ) -> bool ) -> Option < & ' a str > {
33
+ let mut args = args. iter ( ) . map ( String :: as_str) ;
39
34
while let Some ( arg) = args. next ( ) {
40
35
let mut arg = arg. splitn ( 2 , '=' ) ;
41
36
if arg. next ( ) != Some ( find_arg) {
@@ -50,11 +45,15 @@ fn arg_value<'a, T: Deref<Target = str>>(
50
45
None
51
46
}
52
47
48
+ fn has_arg ( args : & [ String ] , find_arg : & str ) -> bool {
49
+ args. iter ( ) . any ( |arg| find_arg == arg. split ( '=' ) . next ( ) . unwrap ( ) )
50
+ }
51
+
53
52
#[ test]
54
53
fn test_arg_value ( ) {
55
- let args = & [ "--bar=bar" , "--foobar" , "123" , "--foo" ] ;
54
+ let args = & [ "--bar=bar" , "--foobar" , "123" , "--foo" ] . map ( String :: from ) ;
56
55
57
- assert_eq ! ( arg_value( & [ ] as & [ & str ] , "--foobar" , |_| true ) , None ) ;
56
+ assert_eq ! ( arg_value( & [ ] , "--foobar" , |_| true ) , None ) ;
58
57
assert_eq ! ( arg_value( args, "--bar" , |_| false ) , None ) ;
59
58
assert_eq ! ( arg_value( args, "--bar" , |_| true ) , Some ( "bar" ) ) ;
60
59
assert_eq ! ( arg_value( args, "--bar" , |p| p == "bar" ) , Some ( "bar" ) ) ;
@@ -65,6 +64,16 @@ fn test_arg_value() {
65
64
assert_eq ! ( arg_value( args, "--foo" , |_| true ) , None ) ;
66
65
}
67
66
67
+ #[ test]
68
+ fn test_has_arg ( ) {
69
+ let args = & [ "--foo=bar" , "-vV" , "--baz" ] . map ( String :: from) ;
70
+ assert ! ( has_arg( args, "--foo" ) ) ;
71
+ assert ! ( has_arg( args, "--baz" ) ) ;
72
+ assert ! ( has_arg( args, "-vV" ) ) ;
73
+
74
+ assert ! ( !has_arg( args, "--bar" ) ) ;
75
+ }
76
+
68
77
fn track_clippy_args ( psess : & mut ParseSess , args_env_var : & Option < String > ) {
69
78
psess. env_depinfo . get_mut ( ) . insert ( (
70
79
Symbol :: intern ( "CLIPPY_ARGS" ) ,
@@ -189,7 +198,7 @@ pub fn main() {
189
198
let mut orig_args = rustc_driver:: args:: raw_args ( & early_dcx) ?;
190
199
191
200
let has_sysroot_arg = |args : & mut [ String ] | -> bool {
192
- if arg_value ( args, "--sysroot" , |_| true ) . is_some ( ) {
201
+ if has_arg ( args, "--sysroot" ) {
193
202
return true ;
194
203
}
195
204
// https://doc.rust-lang.org/rustc/command-line-arguments.html#path-load-command-line-flags-from-a-path
@@ -199,7 +208,7 @@ pub fn main() {
199
208
if let Some ( arg_file_path) = arg. strip_prefix ( '@' ) {
200
209
if let Ok ( arg_file) = read_to_string ( arg_file_path) {
201
210
let split_arg_file: Vec < String > = arg_file. lines ( ) . map ( ToString :: to_string) . collect ( ) ;
202
- if arg_value ( & split_arg_file, "--sysroot" , |_| true ) . is_some ( ) {
211
+ if has_arg ( & split_arg_file, "--sysroot" ) {
203
212
return true ;
204
213
}
205
214
}
@@ -271,16 +280,18 @@ pub fn main() {
271
280
. chain ( vec ! [ "--cfg" . into( ) , "clippy" . into( ) ] )
272
281
. collect :: < Vec < String > > ( ) ;
273
282
274
- // We enable Clippy if one of the following conditions is met
275
- // - IF Clippy is run on its test suite OR
276
- // - IF Clippy is run on the main crate, not on deps (`!cap_lints_allow`) THEN
277
- // - IF `--no-deps` is not set (`!no_deps`) OR
278
- // - IF `--no-deps` is set and Clippy is run on the specified primary package
283
+ // If no Clippy lints will be run we do not need to run Clippy
279
284
let cap_lints_allow = arg_value ( & orig_args, "--cap-lints" , |val| val == "allow" ) . is_some ( )
280
285
&& arg_value ( & orig_args, "--force-warn" , |val| val. contains ( "clippy::" ) ) . is_none ( ) ;
281
- let in_primary_package = env:: var ( "CARGO_PRIMARY_PACKAGE" ) . is_ok ( ) ;
282
286
283
- let clippy_enabled = !cap_lints_allow && ( !no_deps || in_primary_package) ;
287
+ // If `--no-deps` is enabled only lint the primary pacakge
288
+ let relevant_package = !no_deps || env:: var ( "CARGO_PRIMARY_PACKAGE" ) . is_ok ( ) ;
289
+
290
+ // Do not run Clippy for Cargo's info queries so that invalid CLIPPY_ARGS are not cached
291
+ // https://github.com/rust-lang/cargo/issues/14385
292
+ let info_query = has_arg ( & orig_args, "-vV" ) || has_arg ( & orig_args, "--print" ) ;
293
+
294
+ let clippy_enabled = !cap_lints_allow && relevant_package && !info_query;
284
295
if clippy_enabled {
285
296
args. extend ( clippy_args) ;
286
297
rustc_driver:: RunCompiler :: new ( & args, & mut ClippyCallbacks { clippy_args_var } )
0 commit comments