Skip to content

Commit 411d9c7

Browse files
authored
Merge pull request #2585 from bjgill/use-check-manifest-handling
Use cargo check's manifest handling
2 parents e34a855 + 8db845c commit 411d9c7

File tree

2 files changed

+4
-125
lines changed

2 files changed

+4
-125
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ path = "src/driver.rs"
3939
# begin automatic update
4040
clippy_lints = { version = "0.0.190", path = "clippy_lints" }
4141
# end automatic update
42-
cargo_metadata = "0.5"
4342
regex = "0.2"
4443
semver = "0.9"
4544

4645
[dev-dependencies]
46+
cargo_metadata = "0.5"
4747
compiletest_rs = "0.3.7"
4848
lazy_static = "1.0"
4949
serde_derive = "1.0"

src/main.rs

Lines changed: 3 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,16 @@
33
#![feature(rustc_private)]
44
#![allow(unknown_lints, missing_docs_in_private_items)]
55

6-
use std::collections::HashMap;
7-
use std::process;
8-
use std::io::{self, Write};
9-
10-
extern crate cargo_metadata;
11-
12-
use std::path::{Path, PathBuf};
13-
146
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
157
168
Usage:
179
cargo clippy [options] [--] [<opts>...]
1810
1911
Common options:
2012
-h, --help Print this message
21-
--features Features to compile for the package
2213
-V, --version Print version info and exit
23-
--all Run over all packages in the current workspace
2414
25-
Other options are the same as `cargo rustc`.
15+
Other options are the same as `cargo check`.
2616
2717
To allow or deny a lint from the command line you can use `cargo clippy --`
2818
with:
@@ -59,119 +49,8 @@ pub fn main() {
5949
return;
6050
}
6151

62-
let mut manifest_path_arg = std::env::args()
63-
.skip(2)
64-
.skip_while(|val| !val.starts_with("--manifest-path"));
65-
let manifest_path_arg = manifest_path_arg.next().and_then(|val| {
66-
if val == "--manifest-path" {
67-
manifest_path_arg.next()
68-
} else if val.starts_with("--manifest-path=") {
69-
Some(val["--manifest-path=".len()..].to_owned())
70-
} else {
71-
None
72-
}
73-
});
74-
75-
let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
76-
metadata
77-
} else {
78-
println!(
79-
"{:?}",
80-
cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref))
81-
);
82-
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
83-
process::exit(101);
84-
};
85-
86-
let manifest_path = manifest_path_arg.map(|arg| {
87-
PathBuf::from(arg)
88-
.canonicalize()
89-
.expect("manifest path could not be canonicalized")
90-
});
91-
92-
let packages = if std::env::args().any(|a| a == "--all") {
93-
metadata.packages
94-
} else {
95-
let package_index = {
96-
if let Some(manifest_path) = manifest_path {
97-
metadata.packages.iter().position(|package| {
98-
let package_manifest_path = Path::new(&package.manifest_path)
99-
.canonicalize()
100-
.expect("package manifest path could not be canonicalized");
101-
package_manifest_path == manifest_path
102-
})
103-
} else {
104-
let package_manifest_paths: HashMap<_, _> = metadata
105-
.packages
106-
.iter()
107-
.enumerate()
108-
.map(|(i, package)| {
109-
let package_manifest_path = Path::new(&package.manifest_path)
110-
.parent()
111-
.expect("could not find parent directory of package manifest")
112-
.canonicalize()
113-
.expect("package directory cannot be canonicalized");
114-
(package_manifest_path, i)
115-
})
116-
.collect();
117-
118-
let current_dir = std::env::current_dir()
119-
.expect("CARGO_MANIFEST_DIR not set")
120-
.canonicalize()
121-
.expect("manifest directory cannot be canonicalized");
122-
123-
let mut current_path: &Path = &current_dir;
124-
125-
// This gets the most-recent parent (the one that takes the fewest `cd ..`s to
126-
// reach).
127-
loop {
128-
if let Some(&package_index) = package_manifest_paths.get(current_path) {
129-
break Some(package_index);
130-
} else {
131-
// We'll never reach the filesystem root, because to get to this point in the
132-
// code
133-
// the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
134-
// unwrap the current path's parent.
135-
current_path = current_path
136-
.parent()
137-
.unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
138-
}
139-
}
140-
}
141-
}.expect("could not find matching package");
142-
143-
vec![metadata.packages.remove(package_index)]
144-
};
145-
146-
for package in packages {
147-
let manifest_path = package.manifest_path;
148-
149-
for target in package.targets {
150-
let args = std::env::args()
151-
.skip(2)
152-
.filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
153-
154-
let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
155-
if let Some(first) = target.kind.get(0) {
156-
if target.kind.len() > 1 || first.ends_with("lib") {
157-
println!("lib: {}", target.name);
158-
if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
159-
std::process::exit(code);
160-
}
161-
} else if ["bin", "example", "test", "bench"].contains(&&**first) {
162-
println!("{}: {}", first, target.name);
163-
if let Err(code) = process(
164-
vec![format!("--{}", first), target.name]
165-
.into_iter()
166-
.chain(args),
167-
) {
168-
std::process::exit(code);
169-
}
170-
}
171-
} else {
172-
panic!("badly formatted cargo metadata: target::kind is an empty array");
173-
}
174-
}
52+
if let Err(code) = process(std::env::args().skip(2)) {
53+
std::process::exit(code);
17554
}
17655
}
17756

0 commit comments

Comments
 (0)