Skip to content

Commit 81737bf

Browse files
committed
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
1 parent 6b762ee commit 81737bf

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

clippy_lints/src/lib.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern crate clippy_utils;
5252
use clippy_utils::parse_msrv;
5353
use rustc_data_structures::fx::FxHashSet;
5454
use rustc_lint::LintId;
55+
use rustc_semver::RustcVersion;
5556
use rustc_session::Session;
5657

5758
/// Macro used to declare a Clippy lint.
@@ -450,6 +451,39 @@ pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Se
450451
store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv }));
451452
}
452453

454+
fn read_msrv(conf: &Conf, sess: &Session) -> Option<RustcVersion> {
455+
let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION")
456+
.ok()
457+
.and_then(|v| parse_msrv(&v, None, None));
458+
let clippy_msrv = conf.msrv.as_ref().and_then(|s| {
459+
parse_msrv(s, None, None).or_else(|| {
460+
sess.err(&format!(
461+
"error reading Clippy's configuration file. `{}` is not a valid Rust version",
462+
s
463+
));
464+
None
465+
})
466+
});
467+
468+
if let Some(cargo_msrv) = cargo_msrv {
469+
if let Some(clippy_msrv) = clippy_msrv {
470+
// if both files have an msrv, let's compare them and emit a warning if they differ
471+
if clippy_msrv != cargo_msrv {
472+
sess.warn(&format!(
473+
"the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{}` from `clippy.toml`",
474+
clippy_msrv
475+
));
476+
}
477+
478+
Some(clippy_msrv)
479+
} else {
480+
Some(cargo_msrv)
481+
}
482+
} else {
483+
clippy_msrv
484+
}
485+
}
486+
453487
#[doc(hidden)]
454488
pub fn read_conf(sess: &Session) -> Conf {
455489
let file_name = match utils::conf::lookup_conf_file() {
@@ -465,12 +499,11 @@ pub fn read_conf(sess: &Session) -> Conf {
465499
let TryConf { conf, errors } = utils::conf::read(&file_name);
466500
// all conf errors are non-fatal, we just use the default conf in case of error
467501
for error in errors {
468-
sess.struct_err(&format!(
502+
sess.err(&format!(
469503
"error reading Clippy's configuration file `{}`: {}",
470504
file_name.display(),
471505
format_error(error)
472-
))
473-
.emit();
506+
));
474507
}
475508

476509
conf
@@ -577,16 +610,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
577610
store.register_late_pass(|| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions));
578611
store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports));
579612

580-
let msrv = conf.msrv.as_ref().and_then(|s| {
581-
parse_msrv(s, None, None).or_else(|| {
582-
sess.err(&format!(
583-
"error reading Clippy's configuration file. `{}` is not a valid Rust version",
584-
s
585-
));
586-
None
587-
})
588-
});
589-
613+
let msrv = read_msrv(conf, sess);
590614
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
591615
let allow_expect_in_tests = conf.allow_expect_in_tests;
592616
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;

0 commit comments

Comments
 (0)