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"));
16 exit(rustc_driver::run(move || {
19 if std::env::args().any(|a| a == "--version" || a == "-V") {
24 let sys_root = option_env!("SYSROOT")
26 .or_else(|| std::env::var("SYSROOT").ok())
28 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
29 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
30 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
38 .and_then(|out| String::from_utf8(out.stdout).ok())
39 .map(|s| s.trim().to_owned())
41 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
43 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
44 // We're invoking the compiler programmatically, so we ignore this/
45 let mut orig_args: Vec<String> = env::args().collect();
46 if orig_args.len() <= 1 {
47 std::process::exit(1);
49 if orig_args[1] == "rustc" {
50 // we still want to be able to invoke it normally though
53 // this conditional check for the --sysroot flag is there so users can call
54 // `clippy_driver` directly
55 // without having to pass --sysroot or anything
56 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
62 .chain(Some("--sysroot".to_owned()))
63 .chain(Some(sys_root))
67 // this check ensures that dependencies are built but not linted and the final
69 // linted but not built
70 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
71 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
74 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
75 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
78 .split("__CLIPPY_HACKERY__")
79 .filter(|s| !s.is_empty())
85 let mut controller = CompileController::basic();
87 controller.after_parse.callback = Box::new(move |state| {
88 let mut registry = rustc_plugin::registry::Registry::new(
94 "at this compilation stage \
95 the crate must be parsed",
99 registry.args_hidden = Some(Vec::new());
101 let conf = clippy_lints::read_conf(®istry);
102 clippy_lints::register_plugins(&mut registry, &conf);
104 let rustc_plugin::registry::Registry {
112 let sess = &state.session;
113 let mut ls = sess.lint_store.borrow_mut();
114 for pass in early_lint_passes {
115 ls.register_early_pass(Some(sess), true, pass);
117 for pass in late_lint_passes {
118 ls.register_late_pass(Some(sess), true, pass);
121 for (name, to) in lint_groups {
122 ls.register_group(Some(sess), true, name, to);
124 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
126 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
127 sess.plugin_attributes.borrow_mut().extend(attributes);
130 controller.compilation_done.stop = Compilation::Stop;
133 rustc_driver::run_compiler(&args, Box::new(controller), None, None)