Skip to content

Commit 8612d03

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 919cf50 commit 8612d03

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
@@ -54,6 +54,7 @@ extern crate clippy_utils;
5454
use clippy_utils::parse_msrv;
5555
use rustc_data_structures::fx::FxHashSet;
5656
use rustc_lint::LintId;
57+
use rustc_semver::RustcVersion;
5758
use rustc_session::Session;
5859

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

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

474507
conf
@@ -575,16 +608,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
575608
store.register_late_pass(|| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions));
576609
store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports));
577610

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

0 commit comments

Comments
 (0)