Skip to content

Commit

Permalink
rework cargo.toml reading
Browse files Browse the repository at this point in the history
instead of using the `MetadataCommand`, we read the `Cargo.toml` as a
toml file and parse the `rust-version` field out of it.

Also moved the validation point of the clippy.toml rust version to an
earlier point, because we need it now.
  • Loading branch information
hellow554 committed May 5, 2022
1 parent 66b304e commit 2a8dc2f
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ extern crate rustc_typeck;
#[macro_use]
extern crate clippy_utils;

use cargo_metadata::MetadataCommand;
use clippy_utils::parse_msrv;
use rustc_data_structures::fx::FxHashSet;
use rustc_lint::LintId;
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 @@ -444,6 +446,23 @@ 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 cargo_toml_msrv() -> Option<String> {
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");
then {
rust_version.as_str().map(str::to_string)
} else {
None
}
}
}

#[doc(hidden)]
pub fn read_conf(sess: &Session) -> Conf {
let file_name = match utils::conf::lookup_conf_file() {
Expand All @@ -459,23 +478,35 @@ pub fn read_conf(sess: &Session) -> Conf {
let TryConf { mut 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();
));
}

if conf.msrv.is_none() {
// let's try to get msrv from `Cargo.toml`s field `rust-version`
if let Ok(metadata) = MetadataCommand::new().no_deps().exec() {
conf.msrv = metadata
.packages
.get(0)
.and_then(|x| x.rust_version.as_ref())
.and_then(|r| r.comparators.get(0))
.map(|v| format!("{}.{}.{}", v.major, v.minor.unwrap_or(0), v.patch.unwrap_or(0)));
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
})
});
if let Some(cargo_msrv) = cargo_toml_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
let cargo_msrv = parse_msrv(&cargo_msrv, None, None).unwrap(); // cargo already check this for us
if clippy_msrv != cargo_msrv {
sess.warn(&format!(
"The MSRV in `clippy.toml` and `Cargo.toml` differ. Using `{}`",
clippy_msrv
));
}
} else {
// assign msrv from cargo.toml if clippy.toml hasn't set one alreay
conf.msrv = Some(cargo_msrv);
}
}

Expand Down Expand Up @@ -584,16 +615,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 = conf.msrv.as_ref().and_then(|s| parse_msrv(s, None, None));
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 2a8dc2f

Please sign in to comment.