Skip to content

Commit ceb67af

Browse files
committed
clippy-driver: more robust test to see if we're clippy-enabled
Rather than looking for a fixed --emit arg set, just check to see if we're emitting metadata at all. This makes it more robust to being invoked by tools other than cargo (or if cargo changes its invocation). Issue #3663
1 parent e3270c6 commit ceb67af

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ path = "src/main.rs"
3434

3535
[[bin]]
3636
name = "clippy-driver"
37-
test = false
3837
path = "src/driver.rs"
3938

4039
[dependencies]

src/driver.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,46 @@ fn show_version() {
2020
println!(env!("CARGO_PKG_VERSION"));
2121
}
2222

23+
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
24+
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
25+
fn arg_value<'a>(
26+
args: impl IntoIterator<Item = &'a String>,
27+
find_arg: &str,
28+
pred: impl Fn(&str) -> bool,
29+
) -> Option<&'a str> {
30+
let mut args = args.into_iter().map(String::as_str);
31+
32+
while let Some(arg) = args.next() {
33+
let arg: Vec<_> = arg.splitn(2, '=').collect();
34+
if arg.get(0) != Some(&find_arg) {
35+
continue;
36+
}
37+
38+
let value = arg.get(1).cloned().or_else(|| args.next());
39+
if value.as_ref().map_or(false, |p| pred(p)) {
40+
return value;
41+
}
42+
}
43+
None
44+
}
45+
46+
#[test]
47+
fn test_arg_value() {
48+
let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
49+
.iter()
50+
.map(|s| s.to_string())
51+
.collect();
52+
53+
assert_eq!(arg_value(None, "--foobar", |_| true), None);
54+
assert_eq!(arg_value(&args, "--bar", |_| false), None);
55+
assert_eq!(arg_value(&args, "--bar", |_| true), Some("bar"));
56+
assert_eq!(arg_value(&args, "--bar", |p| p == "bar"), Some("bar"));
57+
assert_eq!(arg_value(&args, "--bar", |p| p == "foo"), None);
58+
assert_eq!(arg_value(&args, "--foobar", |p| p == "foo"), None);
59+
assert_eq!(arg_value(&args, "--foobar", |p| p == "123"), Some("123"));
60+
assert_eq!(arg_value(&args, "--foo", |_| true), None);
61+
}
62+
2363
pub fn main() {
2464
rustc_driver::init_rustc_env_logger();
2565
exit(
@@ -78,7 +118,7 @@ pub fn main() {
78118
// crate is
79119
// linted but not built
80120
let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
81-
|| orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
121+
|| arg_value(&orig_args, "--emit", |val| val.split(',').any(|e| e == "metadata")).is_some();
82122

83123
if clippy_enabled {
84124
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);

0 commit comments

Comments
 (0)