Skip to content

Commit 605f88a

Browse files
committed
Ignore --print/-Vv requests in clippy-driver
1 parent 169adfc commit 605f88a

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/driver.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@ use rustc_span::symbol::Symbol;
2222

2323
use std::env;
2424
use std::fs::read_to_string;
25-
use std::ops::Deref;
2625
use std::path::Path;
2726
use std::process::exit;
2827

2928
use anstream::println;
3029

3130
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3231
/// 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);
3934
while let Some(arg) = args.next() {
4035
let mut arg = arg.splitn(2, '=');
4136
if arg.next() != Some(find_arg) {
@@ -50,11 +45,15 @@ fn arg_value<'a, T: Deref<Target = str>>(
5045
None
5146
}
5247

48+
fn has_arg(args: &[String], find_arg: &str) -> bool {
49+
args.iter().any(|arg| find_arg == arg.split('=').next().unwrap())
50+
}
51+
5352
#[test]
5453
fn test_arg_value() {
55-
let args = &["--bar=bar", "--foobar", "123", "--foo"];
54+
let args = &["--bar=bar", "--foobar", "123", "--foo"].map(String::from);
5655

57-
assert_eq!(arg_value(&[] as &[&str], "--foobar", |_| true), None);
56+
assert_eq!(arg_value(&[], "--foobar", |_| true), None);
5857
assert_eq!(arg_value(args, "--bar", |_| false), None);
5958
assert_eq!(arg_value(args, "--bar", |_| true), Some("bar"));
6059
assert_eq!(arg_value(args, "--bar", |p| p == "bar"), Some("bar"));
@@ -65,6 +64,16 @@ fn test_arg_value() {
6564
assert_eq!(arg_value(args, "--foo", |_| true), None);
6665
}
6766

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+
6877
fn track_clippy_args(psess: &mut ParseSess, args_env_var: &Option<String>) {
6978
psess.env_depinfo.get_mut().insert((
7079
Symbol::intern("CLIPPY_ARGS"),
@@ -189,7 +198,7 @@ pub fn main() {
189198
let mut orig_args = rustc_driver::args::raw_args(&early_dcx)?;
190199

191200
let has_sysroot_arg = |args: &mut [String]| -> bool {
192-
if arg_value(args, "--sysroot", |_| true).is_some() {
201+
if has_arg(args, "--sysroot") {
193202
return true;
194203
}
195204
// 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() {
199208
if let Some(arg_file_path) = arg.strip_prefix('@') {
200209
if let Ok(arg_file) = read_to_string(arg_file_path) {
201210
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") {
203212
return true;
204213
}
205214
}
@@ -271,16 +280,18 @@ pub fn main() {
271280
.chain(vec!["--cfg".into(), "clippy".into()])
272281
.collect::<Vec<String>>();
273282

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
279284
let cap_lints_allow = arg_value(&orig_args, "--cap-lints", |val| val == "allow").is_some()
280285
&& arg_value(&orig_args, "--force-warn", |val| val.contains("clippy::")).is_none();
281-
let in_primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
282286

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;
284295
if clippy_enabled {
285296
args.extend(clippy_args);
286297
rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var })

0 commit comments

Comments
 (0)