Skip to content

Commit e898015

Browse files
committed
Revert "Pass Clippy args also trough RUSTFLAGS"
1 parent 4911ab1 commit e898015

File tree

5 files changed

+47
-171
lines changed

5 files changed

+47
-171
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ publish = false
2020

2121
[[bin]]
2222
name = "cargo-clippy"
23+
test = false
2324
path = "src/main.rs"
2425

2526
[[bin]]

README.md

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

212213
### Specifying the minimum supported Rust version
213214

src/driver.rs

+29-87
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![feature(rustc_private)]
22
#![feature(once_cell)]
3-
#![feature(bool_to_option)]
43
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
54
// warn on lints, that are included in `rust-lang/rust`s bootstrap
65
#![warn(rust_2018_idioms, unused_lifetimes)]
@@ -20,7 +19,6 @@ use rustc_tools_util::VersionInfo;
2019

2120
use std::borrow::Cow;
2221
use std::env;
23-
use std::iter;
2422
use std::lazy::SyncLazy;
2523
use std::ops::Deref;
2624
use std::panic;
@@ -49,6 +47,20 @@ fn arg_value<'a, T: Deref<Target = str>>(
4947
None
5048
}
5149

50+
#[test]
51+
fn test_arg_value() {
52+
let args = &["--bar=bar", "--foobar", "123", "--foo"];
53+
54+
assert_eq!(arg_value(&[] as &[&str], "--foobar", |_| true), None);
55+
assert_eq!(arg_value(args, "--bar", |_| false), None);
56+
assert_eq!(arg_value(args, "--bar", |_| true), Some("bar"));
57+
assert_eq!(arg_value(args, "--bar", |p| p == "bar"), Some("bar"));
58+
assert_eq!(arg_value(args, "--bar", |p| p == "foo"), None);
59+
assert_eq!(arg_value(args, "--foobar", |p| p == "foo"), None);
60+
assert_eq!(arg_value(args, "--foobar", |p| p == "123"), Some("123"));
61+
assert_eq!(arg_value(args, "--foo", |_| true), None);
62+
}
63+
5264
struct DefaultCallbacks;
5365
impl rustc_driver::Callbacks for DefaultCallbacks {}
5466

@@ -170,28 +182,6 @@ fn toolchain_path(home: Option<String>, toolchain: Option<String>) -> Option<Pat
170182
})
171183
}
172184

173-
fn remove_clippy_args<'a, T, U, I>(args: &mut Vec<T>, clippy_args: I)
174-
where
175-
T: AsRef<str>,
176-
U: AsRef<str> + ?Sized + 'a,
177-
I: Iterator<Item = &'a U> + Clone,
178-
{
179-
let args_iter = clippy_args.map(AsRef::as_ref);
180-
let args_count = args_iter.clone().count();
181-
182-
if args_count > 0 {
183-
if let Some(start) = args.windows(args_count).enumerate().find_map(|(current, window)| {
184-
window
185-
.iter()
186-
.map(AsRef::as_ref)
187-
.eq(args_iter.clone())
188-
.then_some(current)
189-
}) {
190-
args.drain(start..start + args_count);
191-
}
192-
}
193-
}
194-
195185
#[allow(clippy::too_many_lines)]
196186
pub fn main() {
197187
rustc_driver::init_rustc_env_logger();
@@ -288,9 +278,20 @@ pub fn main() {
288278
args.extend(vec!["--sysroot".into(), sys_root]);
289279
};
290280

291-
let clippy_args = env::var("CLIPPY_ARGS").unwrap_or_default();
292-
let clippy_args = clippy_args.split_whitespace();
293-
let no_deps = clippy_args.clone().any(|flag| flag == "--no-deps");
281+
let mut no_deps = false;
282+
let clippy_args = env::var("CLIPPY_ARGS")
283+
.unwrap_or_default()
284+
.split("__CLIPPY_HACKERY__")
285+
.filter_map(|s| match s {
286+
"" => None,
287+
"--no-deps" => {
288+
no_deps = true;
289+
None
290+
},
291+
_ => Some(s.to_string()),
292+
})
293+
.chain(vec!["--cfg".into(), r#"feature="cargo-clippy""#.into()])
294+
.collect::<Vec<String>>();
294295

295296
// We enable Clippy if one of the following conditions is met
296297
// - IF Clippy is run on its test suite OR
@@ -303,11 +304,7 @@ pub fn main() {
303304

304305
let clippy_enabled = clippy_tests_set || (!cap_lints_allow && (!no_deps || in_primary_package));
305306
if clippy_enabled {
306-
remove_clippy_args(&mut args, iter::once("--no-deps"));
307-
args.extend(vec!["--cfg".into(), r#"feature="cargo-clippy""#.into()]);
308-
} else {
309-
// Remove all flags passed through RUSTFLAGS if Clippy is not enabled.
310-
remove_clippy_args(&mut args, clippy_args);
307+
args.extend(clippy_args);
311308
}
312309

313310
let mut clippy = ClippyCallbacks;
@@ -318,58 +315,3 @@ pub fn main() {
318315
rustc_driver::RunCompiler::new(&args, callbacks).run()
319316
}))
320317
}
321-
322-
#[cfg(test)]
323-
mod tests {
324-
use super::*;
325-
326-
#[test]
327-
fn test_arg_value() {
328-
let args = &["--bar=bar", "--foobar", "123", "--foo"];
329-
330-
assert_eq!(arg_value(&[] as &[&str], "--foobar", |_| true), None);
331-
assert_eq!(arg_value(args, "--bar", |_| false), None);
332-
assert_eq!(arg_value(args, "--bar", |_| true), Some("bar"));
333-
assert_eq!(arg_value(args, "--bar", |p| p == "bar"), Some("bar"));
334-
assert_eq!(arg_value(args, "--bar", |p| p == "foo"), None);
335-
assert_eq!(arg_value(args, "--foobar", |p| p == "foo"), None);
336-
assert_eq!(arg_value(args, "--foobar", |p| p == "123"), Some("123"));
337-
assert_eq!(arg_value(args, "--foo", |_| true), None);
338-
}
339-
340-
#[test]
341-
fn removes_clippy_args_from_start() {
342-
let mut args = vec!["-D", "clippy::await_holding_lock", "--cfg", r#"feature="some_feat""#];
343-
let clippy_args = ["-D", "clippy::await_holding_lock"].iter();
344-
345-
remove_clippy_args(&mut args, clippy_args);
346-
assert_eq!(args, &["--cfg", r#"feature="some_feat""#]);
347-
}
348-
349-
#[test]
350-
fn removes_clippy_args_from_end() {
351-
let mut args = vec!["-Zui-testing", "-A", "clippy::empty_loop", "--no-deps"];
352-
let clippy_args = ["-A", "clippy::empty_loop", "--no-deps"].iter();
353-
354-
remove_clippy_args(&mut args, clippy_args);
355-
assert_eq!(args, &["-Zui-testing"]);
356-
}
357-
358-
#[test]
359-
fn removes_clippy_args_from_middle() {
360-
let mut args = vec!["-Zui-testing", "-W", "clippy::filter_map", "-L", "serde"];
361-
let clippy_args = ["-W", "clippy::filter_map"].iter();
362-
363-
remove_clippy_args(&mut args, clippy_args);
364-
assert_eq!(args, &["-Zui-testing", "-L", "serde"]);
365-
}
366-
367-
#[test]
368-
fn no_clippy_args_to_remove() {
369-
let mut args = vec!["-Zui-testing", "-L", "serde"];
370-
let clippy_args: [&str; 0] = [];
371-
372-
remove_clippy_args(&mut args, clippy_args.iter());
373-
assert_eq!(args, &["-Zui-testing", "-L", "serde"]);
374-
}
375-
}

src/main.rs

+15-83
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(bool_to_option)]
2-
#![feature(command_access)]
31
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
42
// warn on lints, that are included in `rust-lang/rust`s bootstrap
53
#![warn(rust_2018_idioms, unused_lifetimes)]
@@ -64,7 +62,7 @@ struct ClippyCmd {
6462
unstable_options: bool,
6563
cargo_subcommand: &'static str,
6664
args: Vec<String>,
67-
clippy_args: Option<String>,
65+
clippy_args: Vec<String>,
6866
}
6967

7068
impl ClippyCmd {
@@ -101,17 +99,16 @@ impl ClippyCmd {
10199
args.insert(0, "+nightly".to_string());
102100
}
103101

104-
let mut clippy_args = old_args.collect::<Vec<String>>().join(" ");
105-
if cargo_subcommand == "fix" && !clippy_args.contains("--no-deps") {
106-
clippy_args = format!("{} --no-deps", clippy_args);
102+
let mut clippy_args: Vec<String> = old_args.collect();
103+
if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
104+
clippy_args.push("--no-deps".into());
107105
}
108106

109-
let has_args = !clippy_args.is_empty();
110107
ClippyCmd {
111108
unstable_options,
112109
cargo_subcommand,
113110
args,
114-
clippy_args: has_args.then_some(clippy_args),
111+
clippy_args,
115112
}
116113
}
117114

@@ -151,24 +148,20 @@ impl ClippyCmd {
151148
.map(|p| ("CARGO_TARGET_DIR", p))
152149
}
153150

154-
fn into_std_cmd(self, rustflags: Option<String>) -> Command {
151+
fn into_std_cmd(self) -> Command {
155152
let mut cmd = Command::new("cargo");
153+
let clippy_args: String = self
154+
.clippy_args
155+
.iter()
156+
.map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
157+
.collect();
156158

157159
cmd.env(self.path_env(), Self::path())
158160
.envs(ClippyCmd::target_dir())
161+
.env("CLIPPY_ARGS", clippy_args)
159162
.arg(self.cargo_subcommand)
160163
.args(&self.args);
161164

162-
// HACK: pass Clippy args to the driver *also* through RUSTFLAGS.
163-
// This guarantees that new builds will be triggered when Clippy flags change.
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-
);
169-
cmd.env("CLIPPY_ARGS", clippy_args);
170-
}
171-
172165
cmd
173166
}
174167
}
@@ -179,7 +172,7 @@ where
179172
{
180173
let cmd = ClippyCmd::new(old_args);
181174

182-
let mut cmd = cmd.into_std_cmd(env::var("RUSTFLAGS").ok());
175+
let mut cmd = cmd.into_std_cmd();
183176

184177
let exit_status = cmd
185178
.spawn()
@@ -197,7 +190,6 @@ where
197190
#[cfg(test)]
198191
mod tests {
199192
use super::ClippyCmd;
200-
use std::ffi::OsStr;
201193

202194
#[test]
203195
#[should_panic]
@@ -212,7 +204,6 @@ mod tests {
212204
.split_whitespace()
213205
.map(ToString::to_string);
214206
let cmd = ClippyCmd::new(args);
215-
216207
assert_eq!("fix", cmd.cargo_subcommand);
217208
assert_eq!("RUSTC_WORKSPACE_WRAPPER", cmd.path_env());
218209
assert!(cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
@@ -224,8 +215,7 @@ mod tests {
224215
.split_whitespace()
225216
.map(ToString::to_string);
226217
let cmd = ClippyCmd::new(args);
227-
228-
assert!(cmd.clippy_args.unwrap().contains("--no-deps"));
218+
assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
229219
}
230220

231221
#[test]
@@ -234,15 +224,13 @@ mod tests {
234224
.split_whitespace()
235225
.map(ToString::to_string);
236226
let cmd = ClippyCmd::new(args);
237-
238-
assert_eq!(1, cmd.clippy_args.unwrap().matches("--no-deps").count());
227+
assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
239228
}
240229

241230
#[test]
242231
fn check() {
243232
let args = "cargo clippy".split_whitespace().map(ToString::to_string);
244233
let cmd = ClippyCmd::new(args);
245-
246234
assert_eq!("check", cmd.cargo_subcommand);
247235
assert_eq!("RUSTC_WRAPPER", cmd.path_env());
248236
}
@@ -253,63 +241,7 @@ mod tests {
253241
.split_whitespace()
254242
.map(ToString::to_string);
255243
let cmd = ClippyCmd::new(args);
256-
257244
assert_eq!("check", cmd.cargo_subcommand);
258245
assert_eq!("RUSTC_WORKSPACE_WRAPPER", cmd.path_env());
259246
}
260-
261-
#[test]
262-
fn clippy_args_into_rustflags() {
263-
let args = "cargo clippy -- -W clippy::as_conversions"
264-
.split_whitespace()
265-
.map(ToString::to_string);
266-
let cmd = ClippyCmd::new(args);
267-
268-
let rustflags = None;
269-
let cmd = cmd.into_std_cmd(rustflags);
270-
271-
assert!(cmd
272-
.get_envs()
273-
.any(|(key, val)| key == "RUSTFLAGS" && val == Some(OsStr::new("-W clippy::as_conversions"))));
274-
}
275-
276-
#[test]
277-
fn clippy_args_respect_existing_rustflags() {
278-
let args = "cargo clippy -- -D clippy::await_holding_lock"
279-
.split_whitespace()
280-
.map(ToString::to_string);
281-
let cmd = ClippyCmd::new(args);
282-
283-
let rustflags = Some(r#"--cfg feature="some_feat""#.into());
284-
let cmd = cmd.into_std_cmd(rustflags);
285-
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""#))));
288-
}
289-
290-
#[test]
291-
fn no_env_change_if_no_clippy_args() {
292-
let args = "cargo clippy".split_whitespace().map(ToString::to_string);
293-
let cmd = ClippyCmd::new(args);
294-
295-
let rustflags = Some(r#"--cfg feature="some_feat""#.into());
296-
let cmd = cmd.into_std_cmd(rustflags);
297-
298-
assert!(!cmd
299-
.get_envs()
300-
.any(|(key, _)| key == "RUSTFLAGS" || key == "CLIPPY_ARGS"));
301-
}
302-
303-
#[test]
304-
fn no_env_change_if_no_clippy_args_nor_rustflags() {
305-
let args = "cargo clippy".split_whitespace().map(ToString::to_string);
306-
let cmd = ClippyCmd::new(args);
307-
308-
let rustflags = None;
309-
let cmd = cmd.into_std_cmd(rustflags);
310-
311-
assert!(!cmd
312-
.get_envs()
313-
.any(|(key, _)| key == "RUSTFLAGS" || key == "CLIPPY_ARGS"))
314-
}
315247
}

tests/dogfood.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn dogfood_clippy() {
2323
.current_dir(root_dir)
2424
.env("CLIPPY_DOGFOOD", "1")
2525
.env("CARGO_INCREMENTAL", "0")
26-
.arg("clippy")
26+
.arg("clippy-preview")
2727
.arg("--all-targets")
2828
.arg("--all-features")
2929
.arg("--")

0 commit comments

Comments
 (0)