2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
5 #![allow(clippy::missing_docs_in_private_items)]
7 // FIXME: switch to something more ergonomic here, once available.
8 // (currently there is no way to opt into sysroot crates w/o `extern crate`)
9 #[allow(unused_extern_crates)]
10 extern crate rustc_driver;
11 #[allow(unused_extern_crates)]
12 extern crate rustc_plugin;
13 use self::rustc_driver::{driver::CompileController, Compilation};
15 use std::convert::TryInto;
17 use std::process::{exit, Command};
20 println!(env!("CARGO_PKG_VERSION"));
24 rustc_driver::init_rustc_env_logger();
26 rustc_driver::run(move || {
29 if std::env::args().any(|a| a == "--version" || a == "-V") {
34 let sys_root = option_env!("SYSROOT")
36 .or_else(|| std::env::var("SYSROOT").ok())
38 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
39 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
40 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
48 .and_then(|out| String::from_utf8(out.stdout).ok())
49 .map(|s| s.trim().to_owned())
51 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
53 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
54 // We're invoking the compiler programmatically, so we ignore this/
55 let mut orig_args: Vec<String> = env::args().collect();
56 if orig_args.len() <= 1 {
57 std::process::exit(1);
59 if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
60 // we still want to be able to invoke it normally though
63 // this conditional check for the --sysroot flag is there so users can call
64 // `clippy_driver` directly
65 // without having to pass --sysroot or anything
66 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
72 .chain(Some("--sysroot".to_owned()))
73 .chain(Some(sys_root))
77 // this check ensures that dependencies are built but not linted and the final
79 // linted but not built
80 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");
84 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
85 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
86 args.extend(extra_args.split("__CLIPPY_HACKERY__").filter_map(|s| {
96 let mut controller = CompileController::basic();
98 controller.after_parse.callback = Box::new(move |state| {
99 let mut registry = rustc_plugin::registry::Registry::new(
105 "at this compilation stage \
106 the crate must be parsed",
110 registry.args_hidden = Some(Vec::new());
112 let conf = clippy_lints::read_conf(®istry);
113 clippy_lints::register_plugins(&mut registry, &conf);
115 let rustc_plugin::registry::Registry {
123 let sess = &state.session;
124 let mut ls = sess.lint_store.borrow_mut();
125 for pass in early_lint_passes {
126 ls.register_early_pass(Some(sess), true, pass);
128 for pass in late_lint_passes {
129 ls.register_late_pass(Some(sess), true, pass);
132 for (name, (to, deprecated_name)) in lint_groups {
133 ls.register_group(Some(sess), true, name, deprecated_name, to);
135 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
136 clippy_lints::register_renamed(&mut ls);
138 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
139 sess.plugin_attributes.borrow_mut().extend(attributes);
142 controller.compilation_done.stop = Compilation::Stop;
145 rustc_driver::run_compiler(&args, Box::new(controller), None, None)
148 .expect("exit code too large"),