2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![feature(tool_lints)]
5 #![allow(unknown_lints, clippy::missing_docs_in_private_items)]
7 use rustc_driver::{self, driver::CompileController, Compilation};
10 use std::process::{exit, Command};
12 #[allow(clippy::print_stdout)]
14 println!(env!("CARGO_PKG_VERSION"));
18 exit(rustc_driver::run(move || {
21 if std::env::args().any(|a| a == "--version" || a == "-V") {
26 let sys_root = option_env!("SYSROOT")
28 .or_else(|| std::env::var("SYSROOT").ok())
30 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
31 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
32 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
40 .and_then(|out| String::from_utf8(out.stdout).ok())
41 .map(|s| s.trim().to_owned())
43 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
45 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
46 // We're invoking the compiler programmatically, so we ignore this/
47 let mut orig_args: Vec<String> = env::args().collect();
48 if orig_args.len() <= 1 {
49 std::process::exit(1);
51 if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
52 // we still want to be able to invoke it normally though
55 // this conditional check for the --sysroot flag is there so users can call
56 // `clippy_driver` directly
57 // without having to pass --sysroot or anything
58 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
64 .chain(Some("--sysroot".to_owned()))
65 .chain(Some(sys_root))
69 // this check ensures that dependencies are built but not linted and the final
71 // linted but not built
72 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
73 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
76 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
77 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
80 .split("__CLIPPY_HACKERY__")
81 .filter_map(|s| if s.is_empty() {
90 let mut controller = CompileController::basic();
92 controller.after_parse.callback = Box::new(move |state| {
93 let mut registry = rustc_plugin::registry::Registry::new(
99 "at this compilation stage \
100 the crate must be parsed",
104 registry.args_hidden = Some(Vec::new());
106 let conf = clippy_lints::read_conf(®istry);
107 clippy_lints::register_plugins(&mut registry, &conf);
109 let rustc_plugin::registry::Registry {
117 let sess = &state.session;
118 let mut ls = sess.lint_store.borrow_mut();
119 for pass in early_lint_passes {
120 ls.register_early_pass(Some(sess), true, pass);
122 for pass in late_lint_passes {
123 ls.register_late_pass(Some(sess), true, pass);
126 for (name, (to, deprecated_name)) in lint_groups {
127 ls.register_group(Some(sess), true, name, deprecated_name, to);
129 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
131 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
132 sess.plugin_attributes.borrow_mut().extend(attributes);
135 controller.compilation_done.stop = Compilation::Stop;
138 rustc_driver::run_compiler(&args, Box::new(controller), None, None)