Skip to content

Commit a8233a8

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 95f8b26 commit a8233a8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

clippy_lints/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern crate rustc_typeck;
5050
#[macro_use]
5151
extern crate clippy_utils;
5252

53+
use cargo_metadata::MetadataCommand;
5354
use clippy_utils::parse_msrv;
5455
use rustc_data_structures::fx::FxHashSet;
5556
use rustc_lint::LintId;
@@ -455,7 +456,7 @@ pub fn read_conf(sess: &Session) -> Conf {
455456
},
456457
};
457458

458-
let TryConf { conf, errors } = utils::conf::read(&file_name);
459+
let TryConf { mut conf, errors } = utils::conf::read(&file_name);
459460
// all conf errors are non-fatal, we just use the default conf in case of error
460461
for error in errors {
461462
sess.struct_err(&format!(
@@ -466,6 +467,23 @@ pub fn read_conf(sess: &Session) -> Conf {
466467
.emit();
467468
}
468469

470+
if conf.msrv.is_none() {
471+
// let's try to get msrv from `Cargo.toml`s field `rust-version`
472+
match MetadataCommand::new().no_deps().exec() {
473+
Ok(metadata) => {
474+
conf.msrv = metadata
475+
.packages
476+
.get(0)
477+
.and_then(|x| x.rust_version.as_ref())
478+
.and_then(|r| r.comparators.get(0))
479+
.map(|v| format!("{}.{}.{}", v.major, v.minor.unwrap_or(0), v.patch.unwrap_or(0)));
480+
},
481+
Err(e) => {
482+
sess.struct_err(&format!("could not read cargo metadata: {}", e)).emit();
483+
},
484+
}
485+
}
486+
469487
conf
470488
}
471489

0 commit comments

Comments
 (0)