]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Update dependencies
[rust.git] / src / main.rs
1 // error-pattern:yummy
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![allow(unknown_lints, missing_docs_in_private_items)]
5
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
14 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
15
16 Usage:
17     cargo clippy [options] [--] [<opts>...]
18
19 Common options:
20     -h, --help               Print this message
21     --features               Features to compile for the package
22     -V, --version            Print version info and exit
23     --all                    Run over all packages in the current workspace
24
25 Other options are the same as `cargo rustc`.
26
27 To allow or deny a lint from the command line you can use `cargo clippy --`
28 with:
29
30     -W --warn OPT       Set lint warnings
31     -A --allow OPT      Set lint allowed
32     -D --deny OPT       Set lint denied
33     -F --forbid OPT     Set lint forbidden
34
35 The feature `cargo-clippy` is automatically defined for convenience. You can use
36 it to allow or deny lints from the code, eg.:
37
38     #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
39 "#;
40
41 #[allow(print_stdout)]
42 fn show_help() {
43     println!("{}", CARGO_CLIPPY_HELP);
44 }
45
46 #[allow(print_stdout)]
47 fn show_version() {
48     println!("{}", env!("CARGO_PKG_VERSION"));
49 }
50
51 pub fn main() {
52     // Check for version and help flags even when invoked as 'cargo-clippy'
53     if std::env::args().any(|a| a == "--help" || a == "-h") {
54         show_help();
55         return;
56     }
57     if std::env::args().any(|a| a == "--version" || a == "-V") {
58         show_version();
59         return;
60     }
61
62     let manifest_path_arg = std::env::args()
63         .skip(2)
64         .find(|val| val.starts_with("--manifest-path="))
65         .map(|val| val["--manifest-path=".len()..].to_owned());
66
67     let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
68         metadata
69     } else {
70         println!("{:?}", cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)));
71         let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
72         process::exit(101);
73     };
74
75     let manifest_path = manifest_path_arg.map(|arg| {
76         PathBuf::from(arg)
77             .canonicalize()
78             .expect("manifest path could not be canonicalized")
79     });
80
81     let packages = if std::env::args().any(|a| a == "--all") {
82         metadata.packages
83     } else {
84         let package_index = {
85             if let Some(manifest_path) = manifest_path {
86                 metadata.packages.iter().position(|package| {
87                     let package_manifest_path = Path::new(&package.manifest_path)
88                         .canonicalize()
89                         .expect("package manifest path could not be canonicalized");
90                     package_manifest_path == manifest_path
91                 })
92             } else {
93                 let package_manifest_paths: HashMap<_, _> = metadata
94                     .packages
95                     .iter()
96                     .enumerate()
97                     .map(|(i, package)| {
98                         let package_manifest_path = Path::new(&package.manifest_path)
99                             .parent()
100                             .expect("could not find parent directory of package manifest")
101                             .canonicalize()
102                             .expect("package directory cannot be canonicalized");
103                         (package_manifest_path, i)
104                     })
105                     .collect();
106
107                 let current_dir = std::env::current_dir()
108                     .expect("could not read current directory")
109                     .canonicalize()
110                     .expect("current directory cannot be canonicalized");
111
112                 let mut current_path: &Path = &current_dir;
113
114                 // This gets the most-recent parent (the one that takes the fewest `cd ..`s to
115                 // reach).
116                 loop {
117                     if let Some(&package_index) = package_manifest_paths.get(current_path) {
118                         break Some(package_index);
119                     } else {
120                         // We'll never reach the filesystem root, because to get to this point in the
121                         // code
122                         // the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
123                         // unwrap the current path's parent.
124                         current_path = current_path
125                             .parent()
126                             .unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
127                     }
128                 }
129             }
130         }.expect("could not find matching package");
131
132         vec![metadata.packages.remove(package_index)]
133     };
134
135     for package in packages {
136         let manifest_path = package.manifest_path;
137
138         for target in package.targets {
139             let args = std::env::args()
140                 .skip(2)
141                 .filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
142
143             let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
144             if let Some(first) = target.kind.get(0) {
145                 if target.kind.len() > 1 || first.ends_with("lib") {
146                     println!("lib: {}", target.name);
147                     if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
148                         std::process::exit(code);
149                     }
150                 } else if ["bin", "example", "test", "bench"].contains(&&**first) {
151                     println!("{}: {}", first, target.name);
152                     if let Err(code) = process(
153                         vec![format!("--{}", first), target.name]
154                             .into_iter()
155                             .chain(args),
156                     ) {
157                         std::process::exit(code);
158                     }
159                 }
160             } else {
161                 panic!("badly formatted cargo metadata: target::kind is an empty array");
162             }
163         }
164     }
165 }
166
167 fn process<I>(old_args: I) -> Result<(), i32>
168 where
169     I: Iterator<Item = String>,
170 {
171     let mut args = vec!["rustc".to_owned()];
172
173     let mut found_dashes = false;
174     for arg in old_args {
175         found_dashes |= arg == "--";
176         args.push(arg);
177     }
178     if !found_dashes {
179         args.push("--".to_owned());
180     }
181     args.push("--emit=metadata".to_owned());
182     args.push("--cfg".to_owned());
183     args.push(r#"feature="cargo-clippy""#.to_owned());
184
185     let mut path = std::env::current_exe()
186         .expect("current executable path invalid")
187         .with_file_name("clippy-driver");
188     if cfg!(windows) {
189         path.set_extension("exe");
190     }
191     let exit_status = std::process::Command::new("cargo")
192         .args(&args)
193         .env("RUSTC_WRAPPER", path)
194         .spawn()
195         .expect("could not run cargo")
196         .wait()
197         .expect("failed to wait for cargo?");
198
199     if exit_status.success() {
200         Ok(())
201     } else {
202         Err(exit_status.code().unwrap_or(-1))
203     }
204 }