Skip to content

Commit d0d5232

Browse files
committed
Auto merge of #6834 - hyd-dev:clippy-args, r=phansch,flip1995,oli-obk
Let Cargo track CLIPPY_ARGS This PR makes `clippy-driver` emit `CLIPPY_ARGS` in its `dep-info` output. Just like #6441, this allows this workflow to work: ```shell cargo clippy # warning: empty `loop {}` wastes CPU cycles cargo clippy -- -A clippy::empty_loop # no warnings emitted ``` But without rebuilding all dependencies. cc https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/CLIPPY_ARGS.20is.20not.20tracked.20by.20Cargo/near/228599088 changelog: Cargo now re-runs Clippy if arguments after `--` provided to `cargo clippy` are changed.
2 parents e6c643f + 3cd5f44 commit d0d5232

File tree

3 files changed

+114
-42
lines changed

3 files changed

+114
-42
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ the lint(s) you are interested in:
202202
```terminal
203203
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
204204
```
205-
Note that if you've run clippy before, this may only take effect after you've modified a file or ran `cargo clean`.
206205

207206
### Specifying the minimum supported Rust version
208207

src/driver.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
extern crate rustc_driver;
1212
extern crate rustc_errors;
1313
extern crate rustc_interface;
14+
extern crate rustc_session;
15+
extern crate rustc_span;
1416

1517
use rustc_interface::interface;
18+
use rustc_session::Session;
19+
use rustc_span::symbol::Symbol;
1620
use rustc_tools_util::VersionInfo;
1721

1822
use std::borrow::Cow;
@@ -59,20 +63,53 @@ fn test_arg_value() {
5963
assert_eq!(arg_value(args, "--foo", |_| true), None);
6064
}
6165

66+
fn track_clippy_args(sess: &Session, args_env_var: &Option<String>) {
67+
sess.parse_sess.env_depinfo.borrow_mut().insert((
68+
Symbol::intern("CLIPPY_ARGS"),
69+
args_env_var.as_deref().map(Symbol::intern),
70+
));
71+
}
72+
6273
struct DefaultCallbacks;
6374
impl rustc_driver::Callbacks for DefaultCallbacks {}
6475

65-
struct ClippyCallbacks;
76+
/// This is different from `DefaultCallbacks` that it will inform Cargo to track the value of
77+
/// `CLIPPY_ARGS` environment variable.
78+
struct RustcCallbacks {
79+
clippy_args_var: Option<String>,
80+
}
81+
82+
impl rustc_driver::Callbacks for RustcCallbacks {
83+
fn config(&mut self, config: &mut interface::Config) {
84+
let previous = config.register_lints.take();
85+
let clippy_args_var = self.clippy_args_var.take();
86+
config.register_lints = Some(Box::new(move |sess, lint_store| {
87+
if let Some(ref previous) = previous {
88+
(previous)(sess, lint_store);
89+
}
90+
91+
track_clippy_args(sess, &clippy_args_var);
92+
}));
93+
}
94+
}
95+
96+
struct ClippyCallbacks {
97+
clippy_args_var: Option<String>,
98+
}
99+
66100
impl rustc_driver::Callbacks for ClippyCallbacks {
67101
fn config(&mut self, config: &mut interface::Config) {
68102
let previous = config.register_lints.take();
103+
let clippy_args_var = self.clippy_args_var.take();
69104
config.register_lints = Some(Box::new(move |sess, mut lint_store| {
70105
// technically we're ~guaranteed that this is none but might as well call anything that
71106
// is there already. Certainly it can't hurt.
72107
if let Some(previous) = &previous {
73108
(previous)(sess, lint_store);
74109
}
75110

111+
track_clippy_args(sess, &clippy_args_var);
112+
76113
let conf = clippy_lints::read_conf(&[], &sess);
77114
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
78115
clippy_lints::register_pre_expansion_lints(&mut lint_store);
@@ -277,7 +314,9 @@ pub fn main() {
277314
};
278315

279316
let mut no_deps = false;
280-
let clippy_args = env::var("CLIPPY_ARGS")
317+
let clippy_args_var = env::var("CLIPPY_ARGS").ok();
318+
let clippy_args = clippy_args_var
319+
.as_deref()
281320
.unwrap_or_default()
282321
.split("__CLIPPY_HACKERY__")
283322
.filter_map(|s| match s {
@@ -305,11 +344,10 @@ pub fn main() {
305344
args.extend(clippy_args);
306345
}
307346

308-
let mut clippy = ClippyCallbacks;
309-
let mut default = DefaultCallbacks;
310-
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
311-
if clippy_enabled { &mut clippy } else { &mut default };
312-
313-
rustc_driver::RunCompiler::new(&args, callbacks).run()
347+
if clippy_enabled {
348+
rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run()
349+
} else {
350+
rustc_driver::RunCompiler::new(&args, &mut RustcCallbacks { clippy_args_var }).run()
351+
}
314352
}))
315353
}

tests/dogfood.rs

+68-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![feature(once_cell)]
44

55
use std::lazy::SyncLazy;
6-
use std::path::{Path, PathBuf};
6+
use std::path::PathBuf;
77
use std::process::Command;
88

99
mod cargo;
@@ -45,40 +45,53 @@ fn dogfood_clippy() {
4545
assert!(output.status.success());
4646
}
4747

48-
#[test]
49-
fn dogfood_subprojects() {
50-
fn test_no_deps_ignores_path_deps_in_workspaces() {
51-
fn clean(cwd: &Path, target_dir: &Path) {
52-
Command::new("cargo")
53-
.current_dir(cwd)
54-
.env("CARGO_TARGET_DIR", target_dir)
55-
.arg("clean")
56-
.args(&["-p", "subcrate"])
57-
.args(&["-p", "path_dep"])
58-
.output()
59-
.unwrap();
60-
}
61-
62-
if cargo::is_rustc_test_suite() {
63-
return;
64-
}
65-
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
66-
let target_dir = root.join("target").join("dogfood");
67-
let cwd = root.join("clippy_workspace_tests");
48+
fn test_no_deps_ignores_path_deps_in_workspaces() {
49+
if cargo::is_rustc_test_suite() {
50+
return;
51+
}
52+
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
53+
let target_dir = root.join("target").join("dogfood");
54+
let cwd = root.join("clippy_workspace_tests");
55+
56+
// Make sure we start with a clean state
57+
Command::new("cargo")
58+
.current_dir(&cwd)
59+
.env("CARGO_TARGET_DIR", &target_dir)
60+
.arg("clean")
61+
.args(&["-p", "subcrate"])
62+
.args(&["-p", "path_dep"])
63+
.output()
64+
.unwrap();
65+
66+
// `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
67+
// Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
68+
let output = Command::new(&*CLIPPY_PATH)
69+
.current_dir(&cwd)
70+
.env("CLIPPY_DOGFOOD", "1")
71+
.env("CARGO_INCREMENTAL", "0")
72+
.arg("clippy")
73+
.args(&["-p", "subcrate"])
74+
.arg("--")
75+
.arg("--no-deps")
76+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
77+
.args(&["--cfg", r#"feature="primary_package_test""#])
78+
.output()
79+
.unwrap();
80+
println!("status: {}", output.status);
81+
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
82+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
6883

69-
// Make sure we start with a clean state
70-
clean(&cwd, &target_dir);
84+
assert!(output.status.success());
7185

72-
// `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
73-
// Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
86+
let lint_path_dep = || {
87+
// Test that without the `--no-deps` argument, `path_dep` is linted.
7488
let output = Command::new(&*CLIPPY_PATH)
7589
.current_dir(&cwd)
7690
.env("CLIPPY_DOGFOOD", "1")
7791
.env("CARGO_INCREMENTAL", "0")
7892
.arg("clippy")
7993
.args(&["-p", "subcrate"])
8094
.arg("--")
81-
.arg("--no-deps")
8295
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
8396
.args(&["--cfg", r#"feature="primary_package_test""#])
8497
.output()
@@ -87,12 +100,18 @@ fn dogfood_subprojects() {
87100
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
88101
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
89102

90-
assert!(output.status.success());
103+
assert!(!output.status.success());
104+
assert!(
105+
String::from_utf8(output.stderr)
106+
.unwrap()
107+
.contains("error: empty `loop {}` wastes CPU cycles")
108+
);
109+
};
91110

92-
// Make sure we start with a clean state
93-
clean(&cwd, &target_dir);
111+
// Make sure Cargo is aware of the removal of `--no-deps`.
112+
lint_path_dep();
94113

95-
// Test that without the `--no-deps` argument, `path_dep` is linted.
114+
let successful_build = || {
96115
let output = Command::new(&*CLIPPY_PATH)
97116
.current_dir(&cwd)
98117
.env("CLIPPY_DOGFOOD", "1")
@@ -101,16 +120,32 @@ fn dogfood_subprojects() {
101120
.args(&["-p", "subcrate"])
102121
.arg("--")
103122
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
104-
.args(&["--cfg", r#"feature="primary_package_test""#])
105123
.output()
106124
.unwrap();
107125
println!("status: {}", output.status);
108126
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
109127
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
110128

111-
assert!(!output.status.success());
112-
}
129+
assert!(output.status.success());
113130

131+
output
132+
};
133+
134+
// Trigger a sucessful build, so Cargo would like to cache the build result.
135+
successful_build();
136+
137+
// Make sure there's no spurious rebuild when nothing changes.
138+
let stderr = String::from_utf8(successful_build().stderr).unwrap();
139+
assert!(!stderr.contains("Compiling"));
140+
assert!(!stderr.contains("Checking"));
141+
assert!(stderr.contains("Finished"));
142+
143+
// Make sure Cargo is aware of the new `--cfg` flag.
144+
lint_path_dep();
145+
}
146+
147+
#[test]
148+
fn dogfood_subprojects() {
114149
// run clippy on remaining subprojects and fail the test if lint warnings are reported
115150
if cargo::is_rustc_test_suite() {
116151
return;

0 commit comments

Comments
 (0)