2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![allow(unknown_lints, missing_docs_in_private_items)]
6 use rustc_driver::{self, driver::CompileController, Compilation};
8 use std::process::{exit, Command};
10 #[allow(print_stdout)]
12 println!(env!("CARGO_PKG_VERSION"));
18 if std::env::args().any(|a| a == "--version" || a == "-V") {
23 let sys_root = option_env!("SYSROOT")
25 .or_else(|| std::env::var("SYSROOT").ok())
27 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
28 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
29 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
37 .and_then(|out| String::from_utf8(out.stdout).ok())
38 .map(|s| s.trim().to_owned())
40 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
42 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
43 // We're invoking the compiler programmatically, so we ignore this/
44 let mut orig_args: Vec<String> = env::args().collect();
45 if orig_args.len() <= 1 {
46 std::process::exit(1);
48 if orig_args[1] == "rustc" {
49 // we still want to be able to invoke it normally though
52 // this conditional check for the --sysroot flag is there so users can call
53 // `clippy_driver` directly
54 // without having to pass --sysroot or anything
55 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
61 .chain(Some("--sysroot".to_owned()))
62 .chain(Some(sys_root))
66 // this check ensures that dependencies are built but not linted and the final
68 // linted but not built
69 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
70 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
73 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
74 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
77 .split("__CLIPPY_HACKERY__")
78 .filter(|s| !s.is_empty())
84 let mut controller = CompileController::basic();
86 controller.after_parse.callback = Box::new(move |state| {
87 let mut registry = rustc_plugin::registry::Registry::new(
93 "at this compilation stage \
94 the crate must be parsed",
98 registry.args_hidden = Some(Vec::new());
99 clippy_lints::register_plugins(&mut registry);
101 let rustc_plugin::registry::Registry {
109 let sess = &state.session;
110 let mut ls = sess.lint_store.borrow_mut();
111 for pass in early_lint_passes {
112 ls.register_early_pass(Some(sess), true, pass);
114 for pass in late_lint_passes {
115 ls.register_late_pass(Some(sess), true, pass);
118 for (name, to) in lint_groups {
119 ls.register_group(Some(sess), true, name, to);
121 clippy_lints::register_pre_expansion_lints(sess, &mut ls);
123 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
124 sess.plugin_attributes.borrow_mut().extend(attributes);
127 controller.compilation_done.stop = Compilation::Stop;
129 if rustc_driver::run_compiler(&args, Box::new(controller), None, None)