2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![allow(unknown_lints, missing_docs_in_private_items)]
6 extern crate clippy_lints;
9 extern crate rustc_codegen_utils;
10 extern crate rustc_driver;
11 extern crate rustc_errors;
12 extern crate rustc_plugin;
15 use rustc_driver::{driver::CompileController, Compilation};
16 use std::process::{exit, Command};
18 #[allow(print_stdout)]
20 println!(env!("CARGO_PKG_VERSION"));
26 if std::env::args().any(|a| a == "--version" || a == "-V") {
31 let sys_root = option_env!("SYSROOT")
33 .or_else(|| std::env::var("SYSROOT").ok())
35 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
36 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
37 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
45 .and_then(|out| String::from_utf8(out.stdout).ok())
46 .map(|s| s.trim().to_owned())
48 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
50 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
51 // We're invoking the compiler programmatically, so we ignore this/
52 let mut orig_args: Vec<String> = env::args().collect();
53 if orig_args.len() <= 1 {
54 std::process::exit(1);
56 if orig_args[1] == "rustc" {
57 // we still want to be able to invoke it normally though
60 // this conditional check for the --sysroot flag is there so users can call
61 // `clippy_driver` directly
62 // without having to pass --sysroot or anything
63 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
69 .chain(Some("--sysroot".to_owned()))
70 .chain(Some(sys_root))
74 // this check ensures that dependencies are built but not linted and the final
76 // linted but not built
77 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
78 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
81 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
82 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
85 .split("__CLIPPY_HACKERY__")
86 .filter(|s| !s.is_empty())
92 let mut controller = CompileController::basic();
94 controller.after_parse.callback = Box::new(move |state| {
95 let mut registry = rustc_plugin::registry::Registry::new(
101 "at this compilation stage \
102 the crate must be parsed",
106 registry.args_hidden = Some(Vec::new());
107 clippy_lints::register_plugins(&mut registry);
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) in lint_groups {
127 ls.register_group(Some(sess), true, name, to);
130 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
131 sess.plugin_attributes.borrow_mut().extend(attributes);
134 controller.compilation_done.stop = Compilation::Stop;
136 if rustc_driver::run_compiler(&args, Box::new(controller), None, None)