Skip to content

Commit 03e72d5

Browse files
author
hyd-dev
committed
Let Cargo track CLIPPY_ARGS
1 parent 43d19f6 commit 03e72d5

File tree

3 files changed

+117
-41
lines changed

3 files changed

+117
-41
lines changed

README.md

Lines changed: 0 additions & 1 deletion
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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
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;
19-
use std::env;
23+
use std::env::{self, VarError};
2024
use std::lazy::SyncLazy;
2125
use std::ops::Deref;
2226
use std::panic;
@@ -59,20 +63,51 @@ 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+
struct ClippyArgsCallbacks {
77+
clippy_args_var: Option<String>,
78+
}
79+
80+
impl rustc_driver::Callbacks for ClippyArgsCallbacks {
81+
fn config(&mut self, config: &mut interface::Config) {
82+
let previous = config.register_lints.take();
83+
let clippy_args_var = self.clippy_args_var.take();
84+
config.register_lints = Some(Box::new(move |sess, lint_store| {
85+
if let Some(ref previous) = previous {
86+
(previous)(sess, lint_store);
87+
}
88+
89+
track_clippy_args(sess, &clippy_args_var);
90+
}));
91+
}
92+
}
93+
94+
struct ClippyCallbacks {
95+
clippy_args_var: Option<String>,
96+
}
97+
6698
impl rustc_driver::Callbacks for ClippyCallbacks {
6799
fn config(&mut self, config: &mut interface::Config) {
68100
let previous = config.register_lints.take();
101+
let clippy_args_var = self.clippy_args_var.take();
69102
config.register_lints = Some(Box::new(move |sess, mut lint_store| {
70103
// technically we're ~guaranteed that this is none but might as well call anything that
71104
// is there already. Certainly it can't hurt.
72105
if let Some(previous) = &previous {
73106
(previous)(sess, lint_store);
74107
}
75108

109+
track_clippy_args(sess, &clippy_args_var);
110+
76111
let conf = clippy_lints::read_conf(&[], &sess);
77112
clippy_lints::register_plugins(&mut lint_store, &sess, &conf);
78113
clippy_lints::register_pre_expansion_lints(&mut lint_store);
@@ -277,7 +312,15 @@ pub fn main() {
277312
};
278313

279314
let mut no_deps = false;
280-
let clippy_args = env::var("CLIPPY_ARGS")
315+
let clippy_args_var = env::var("CLIPPY_ARGS").map_or_else(
316+
|e| match e {
317+
VarError::NotPresent => None,
318+
VarError::NotUnicode(s) => panic!("CLIPPY_ARGS is not valid Unicode: {:?}", s),
319+
},
320+
Some,
321+
);
322+
let clippy_args = clippy_args_var
323+
.as_deref()
281324
.unwrap_or_default()
282325
.split("__CLIPPY_HACKERY__")
283326
.filter_map(|s| match s {
@@ -305,11 +348,10 @@ pub fn main() {
305348
args.extend(clippy_args);
306349
}
307350

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()
351+
if clippy_enabled {
352+
rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run()
353+
} else {
354+
rustc_driver::RunCompiler::new(&args, &mut ClippyArgsCallbacks { clippy_args_var }).run()
355+
}
314356
}))
315357
}

tests/dogfood.rs

Lines changed: 66 additions & 31 deletions
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;
@@ -49,17 +49,6 @@ fn dogfood_clippy() {
4949
#[test]
5050
fn dogfood_subprojects() {
5151
fn test_no_deps_ignores_path_deps_in_workspaces() {
52-
fn clean(cwd: &Path, target_dir: &Path) {
53-
Command::new("cargo")
54-
.current_dir(cwd)
55-
.env("CARGO_TARGET_DIR", target_dir)
56-
.arg("clean")
57-
.args(&["-p", "subcrate"])
58-
.args(&["-p", "path_dep"])
59-
.output()
60-
.unwrap();
61-
}
62-
6352
if cargo::is_rustc_test_suite() {
6453
return;
6554
}
@@ -68,7 +57,14 @@ fn dogfood_subprojects() {
6857
let cwd = root.join("clippy_workspace_tests");
6958

7059
// Make sure we start with a clean state
71-
clean(&cwd, &target_dir);
60+
Command::new("cargo")
61+
.current_dir(&cwd)
62+
.env("CARGO_TARGET_DIR", &target_dir)
63+
.arg("clean")
64+
.args(&["-p", "subcrate"])
65+
.args(&["-p", "path_dep"])
66+
.output()
67+
.unwrap();
7268

7369
// `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
7470
// Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
@@ -90,26 +86,65 @@ fn dogfood_subprojects() {
9086

9187
assert!(output.status.success());
9288

93-
// Make sure we start with a clean state
94-
clean(&cwd, &target_dir);
89+
let lint_path_dep = || {
90+
// Test that without the `--no-deps` argument, `path_dep` is linted.
91+
let output = Command::new(&*CLIPPY_PATH)
92+
.current_dir(&cwd)
93+
.env("CLIPPY_DOGFOOD", "1")
94+
.env("CARGO_INCREMENTAL", "0")
95+
.arg("clippy")
96+
.args(&["-p", "subcrate"])
97+
.arg("--")
98+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
99+
.args(&["--cfg", r#"feature="primary_package_test""#])
100+
.output()
101+
.unwrap();
102+
println!("status: {}", output.status);
103+
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
104+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
105+
106+
assert!(!output.status.success());
107+
assert!(
108+
String::from_utf8(output.stderr)
109+
.unwrap()
110+
.contains("error: empty `loop {}` wastes CPU cycles")
111+
);
112+
};
113+
114+
// Make sure Cargo is aware of the removal of `--no-deps`.
115+
lint_path_dep();
116+
117+
let successful_build = || {
118+
let output = Command::new(&*CLIPPY_PATH)
119+
.current_dir(&cwd)
120+
.env("CLIPPY_DOGFOOD", "1")
121+
.env("CARGO_INCREMENTAL", "0")
122+
.arg("clippy")
123+
.args(&["-p", "subcrate"])
124+
.arg("--")
125+
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
126+
.output()
127+
.unwrap();
128+
println!("status: {}", output.status);
129+
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
130+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
95131

96-
// Test that without the `--no-deps` argument, `path_dep` is linted.
97-
let output = Command::new(&*CLIPPY_PATH)
98-
.current_dir(&cwd)
99-
.env("CLIPPY_DOGFOOD", "1")
100-
.env("CARGO_INCREMENTAL", "0")
101-
.arg("clippy")
102-
.args(&["-p", "subcrate"])
103-
.arg("--")
104-
.arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
105-
.args(&["--cfg", r#"feature="primary_package_test""#])
106-
.output()
107-
.unwrap();
108-
println!("status: {}", output.status);
109-
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
110-
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
132+
assert!(output.status.success());
133+
134+
output
135+
};
136+
137+
// Trigger a sucessful build, so Cargo would like to cache the build result.
138+
successful_build();
139+
140+
// Make sure there's no spurious rebuild when nothing changes.
141+
let stderr = String::from_utf8(successful_build().stderr).unwrap();
142+
assert!(!stderr.contains("Compiling"));
143+
assert!(!stderr.contains("Checking"));
144+
assert!(stderr.contains("Finished"));
111145

112-
assert!(!output.status.success());
146+
// Make sure Cargo is aware of the new `--cfg` flag.
147+
lint_path_dep();
113148
}
114149

115150
// run clippy on remaining subprojects and fail the test if lint warnings are reported

0 commit comments

Comments
 (0)