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};
9 use std::process::{exit, Command};
11 #[allow(clippy::print_stdout)]
13 println!(env!("CARGO_PKG_VERSION"));
17 exit(rustc_driver::run(move || {
20 if std::env::args().any(|a| a == "--version" || a == "-V") {
25 let sys_root = option_env!("SYSROOT")
27 .or_else(|| std::env::var("SYSROOT").ok())
29 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
30 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
31 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
39 .and_then(|out| String::from_utf8(out.stdout).ok())
40 .map(|s| s.trim().to_owned())
42 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
44 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
45 // We're invoking the compiler programmatically, so we ignore this/
46 let mut orig_args: Vec<String> = env::args().collect();
47 if orig_args.len() <= 1 {
48 std::process::exit(1);
50 if orig_args[1] == "rustc" {
51 // we still want to be able to invoke it normally though
54 // this conditional check for the --sysroot flag is there so users can call
55 // `clippy_driver` directly
56 // without having to pass --sysroot or anything
57 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
63 .chain(Some("--sysroot".to_owned()))
64 .chain(Some(sys_root))
68 // this check ensures that dependencies are built but not linted and the final
70 // linted but not built
71 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
72 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
75 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
76 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
79 .split("__CLIPPY_HACKERY__")
80 .filter(|s| !s.is_empty())
86 let mut controller = CompileController::basic();
88 controller.after_parse.callback = Box::new(move |state| {
89 let mut registry = rustc_plugin::registry::Registry::new(
95 "at this compilation stage \
96 the crate must be parsed",
100 registry.args_hidden = Some(Vec::new());
102 let conf = clippy_lints::read_conf(®istry);
103 clippy_lints::register_plugins(&mut registry, &conf);
105 let rustc_plugin::registry::Registry {
113 let sess = &state.session;
114 let mut ls = sess.lint_store.borrow_mut();
115 for pass in early_lint_passes {
116 ls.register_early_pass(Some(sess), true, pass);
118 for pass in late_lint_passes {
119 ls.register_late_pass(Some(sess), true, pass);
122 for (name, (to, deprecated_name)) in lint_groups {
123 ls.register_group(Some(sess), true, name, deprecated_name, to);
125 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
127 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
128 sess.plugin_attributes.borrow_mut().extend(attributes);
131 controller.compilation_done.stop = Compilation::Stop;
134 rustc_driver::run_compiler(&args, Box::new(controller), None, None)