Skip to content

Commit

Permalink
try reading rust-version from Cargo.toml
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hellow554 committed May 9, 2022
1 parent 95f8b26 commit 08ea04b
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<RustcVersion> {
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::<Value>(&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() {
Expand All @@ -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
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 08ea04b

Please sign in to comment.