From 08ea04bfa04e99155707057faab863205925ddb8 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Mon, 2 May 2022 11:31:39 +0200 Subject: [PATCH] try reading rust-version from Cargo.toml Cargo.toml can contain a field `rust-version`, that acts like a MSRV of clippy.toml file: https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field This will try to read that field and use it, if the clippy.toml config has no `msrv` entry --- clippy_lints/src/lib.rs | 64 ++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 24cb4ec2677f..e00d9d8571db 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -53,7 +53,11 @@ extern crate clippy_utils; use clippy_utils::parse_msrv; use rustc_data_structures::fx::FxHashSet; use rustc_lint::LintId; +use rustc_semver::RustcVersion; use rustc_session::Session; +use std::fs::File; +use std::io::Read; +use toml::Value; /// Macro used to declare a Clippy lint. /// @@ -443,6 +447,50 @@ pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Se store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv })); } +fn read_msrv(conf: &Conf, sess: &Session) -> Option { + let clippy_msrv = conf.msrv.as_ref().and_then(|s| { + parse_msrv(s, None, None).or_else(|| { + sess.err(&format!( + "error reading Clippy's configuration file. `{}` is not a valid Rust version", + s + )); + None + }) + }); + + let cargo_msrv = if_chain! { + if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR"); + if let Ok(mut file) = File::open(format!("{manifest_dir}/Cargo.toml")); + let mut cargo_content_str = String::new(); + if let Ok(_) = file.read_to_string(&mut cargo_content_str); + if let Ok(cargo_content) = toml::from_str::(&cargo_content_str); + if let Some(package) = cargo_content.get("package"); + if let Some(rust_version) = package.get("rust-version"); + if let Some(rust_version_str) = rust_version.as_str(); + then { + parse_msrv(rust_version_str, None, None) + } else { + None + } + }; + + if let Some(cargo_msrv) = cargo_msrv { + if let Some(clippy_msrv) = clippy_msrv { + // if both files have an msrv, let's compare them and emit a warning if they differ + if clippy_msrv != cargo_msrv { + sess.warn(&format!( + "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{}`", + clippy_msrv + )); + return Some(clippy_msrv); + } + } + Some(cargo_msrv) + } else { + clippy_msrv + } +} + #[doc(hidden)] pub fn read_conf(sess: &Session) -> Conf { let file_name = match utils::conf::lookup_conf_file() { @@ -458,12 +506,11 @@ pub fn read_conf(sess: &Session) -> Conf { let TryConf { conf, errors } = utils::conf::read(&file_name); // all conf errors are non-fatal, we just use the default conf in case of error for error in errors { - sess.struct_err(&format!( + sess.err(&format!( "error reading Clippy's configuration file `{}`: {}", file_name.display(), error - )) - .emit(); + )); } conf @@ -571,16 +618,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions)); store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports)); - let msrv = conf.msrv.as_ref().and_then(|s| { - parse_msrv(s, None, None).or_else(|| { - sess.err(&format!( - "error reading Clippy's configuration file. `{}` is not a valid Rust version", - s - )); - None - }) - }); - + let msrv = read_msrv(&conf, sess); let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; store.register_late_pass(move || Box::new(approx_const::ApproxConstant::new(msrv))); store.register_late_pass(move || Box::new(methods::Methods::new(avoid_breaking_exported_api, msrv)));