X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Ftools%2Fclippy%2Fsrc%2Fdriver.rs;h=ee2a3ad20d3e5b0d77b0819ddd44bd2271c376ec;hb=ddf055ad07b93d27c826e737b674b65ab760e901;hp=f24d3507823ecaf9d8ae0e32e9124bdbb0ffb320;hpb=51287f264ca92285108d7866901a2d29eee49725;p=rust.git diff --git a/src/tools/clippy/src/driver.rs b/src/tools/clippy/src/driver.rs index f24d3507823..ee2a3ad20d3 100644 --- a/src/tools/clippy/src/driver.rs +++ b/src/tools/clippy/src/driver.rs @@ -1,4 +1,5 @@ #![feature(rustc_private)] +#![feature(let_chains)] #![feature(once_cell)] #![cfg_attr(feature = "deny-warnings", deny(warnings))] // warn on lints, that are included in `rust-lang/rust`s bootstrap @@ -71,6 +72,32 @@ fn track_clippy_args(parse_sess: &mut ParseSess, args_env_var: &Option) )); } +/// Track files that may be accessed at runtime in `file_depinfo` so that cargo will re-run clippy +/// when any of them are modified +fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option) { + let file_depinfo = parse_sess.file_depinfo.get_mut(); + + // Used by `clippy::cargo` lints and to determine the MSRV. `cargo clippy` executes `clippy-driver` + // with the current directory set to `CARGO_MANIFEST_DIR` so a relative path is fine + if Path::new("Cargo.toml").exists() { + file_depinfo.insert(Symbol::intern("Cargo.toml")); + } + + // `clippy.toml` + if let Some(path) = conf_path_string { + file_depinfo.insert(Symbol::intern(&path)); + } + + // During development track the `clippy-driver` executable so that cargo will re-run clippy whenever + // it is rebuilt + if cfg!(debug_assertions) + && let Ok(current_exe) = env::current_exe() + && let Some(current_exe) = current_exe.to_str() + { + file_depinfo.insert(Symbol::intern(current_exe)); + } +} + struct DefaultCallbacks; impl rustc_driver::Callbacks for DefaultCallbacks {} @@ -97,10 +124,18 @@ impl rustc_driver::Callbacks for ClippyCallbacks { // JUSTIFICATION: necessary in clippy driver to set `mir_opt_level` #[allow(rustc::bad_opt_access)] fn config(&mut self, config: &mut interface::Config) { + let conf_path = clippy_lints::lookup_conf_file(); + let conf_path_string = if let Ok(Some(path)) = &conf_path { + path.to_str().map(String::from) + } else { + None + }; + let previous = config.register_lints.take(); let clippy_args_var = self.clippy_args_var.take(); config.parse_sess_created = Some(Box::new(move |parse_sess| { track_clippy_args(parse_sess, &clippy_args_var); + track_files(parse_sess, conf_path_string); })); config.register_lints = Some(Box::new(move |sess, lint_store| { // technically we're ~guaranteed that this is none but might as well call anything that @@ -109,7 +144,7 @@ fn config(&mut self, config: &mut interface::Config) { (previous)(sess, lint_store); } - let conf = clippy_lints::read_conf(sess); + let conf = clippy_lints::read_conf(sess, &conf_path); clippy_lints::register_plugins(lint_store, sess, &conf); clippy_lints::register_pre_expansion_lints(lint_store, sess, &conf); clippy_lints::register_renamed(lint_store); @@ -217,6 +252,13 @@ pub fn main() { exit(rustc_driver::catch_with_exit_code(move || { let mut orig_args: Vec = env::args().collect(); + let sys_root_env = std::env::var("SYSROOT").ok(); + let pass_sysroot_env_if_given = |args: &mut Vec, sys_root_env| { + if let Some(sys_root) = sys_root_env { + args.extend(vec!["--sysroot".into(), sys_root]); + }; + }; + // make "clippy-driver --rustc" work like a subcommand that passes further args to "rustc" // for example `clippy-driver --rustc --version` will print the rustc version that clippy-driver // uses @@ -224,7 +266,10 @@ pub fn main() { orig_args.remove(pos); orig_args[0] = "rustc".to_string(); - return rustc_driver::RunCompiler::new(&orig_args, &mut DefaultCallbacks).run(); + let mut args: Vec = orig_args.clone(); + pass_sysroot_env_if_given(&mut args, sys_root_env); + + return rustc_driver::RunCompiler::new(&args, &mut DefaultCallbacks).run(); } if orig_args.iter().any(|a| a == "--version" || a == "-V") { @@ -247,6 +292,9 @@ pub fn main() { exit(0); } + let mut args: Vec = orig_args.clone(); + pass_sysroot_env_if_given(&mut args, sys_root_env); + let mut no_deps = false; let clippy_args_var = env::var("CLIPPY_ARGS").ok(); let clippy_args = clippy_args_var @@ -275,11 +323,10 @@ pub fn main() { let clippy_enabled = !cap_lints_allow && (!no_deps || in_primary_package); if clippy_enabled { - let mut args: Vec = orig_args.clone(); args.extend(clippy_args); rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run() } else { - rustc_driver::RunCompiler::new(&orig_args, &mut RustcCallbacks { clippy_args_var }).run() + rustc_driver::RunCompiler::new(&args, &mut RustcCallbacks { clippy_args_var }).run() } })) }