Auto merge of #8774 - hellow554:cargo-rust-version, r=flip1995

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

changelog: respect `rust-version` from `Cargo.toml`

closes #8746
closes #7765
This commit is contained in:
bors 2022-06-28 07:27:08 +00:00
commit b776fb8294
38 changed files with 345 additions and 16 deletions

View File

@ -31,6 +31,7 @@ termize = "0.1"
compiletest_rs = { version = "0.8", features = ["tmp"] }
tester = "0.9"
regex = "1.5"
toml = "0.5"
# This is used by the `collect-metadata` alias.
filetime = "0.2"

View File

@ -214,6 +214,14 @@ specifying the minimum supported Rust version (MSRV) in the clippy configuration
msrv = "1.30.0"
```
Alternatively, the [`rust-version` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field)
in the `Cargo.toml` can be used.
```toml
# Cargo.toml
rust-version = "1.30"
```
The MSRV can also be specified as an inner attribute, like below.
```rust

View File

@ -53,6 +53,7 @@
use clippy_utils::parse_msrv;
use rustc_data_structures::fx::FxHashSet;
use rustc_lint::LintId;
use rustc_semver::RustcVersion;
use rustc_session::Session;
/// Macro used to declare a Clippy lint.
@ -452,6 +453,39 @@ 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 cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION")
.ok()
.and_then(|v| parse_msrv(&v, None, None));
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_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 `{}` from `clippy.toml`",
clippy_msrv
));
}
Some(clippy_msrv)
} else {
Some(cargo_msrv)
}
} else {
clippy_msrv
}
}
#[doc(hidden)]
pub fn read_conf(sess: &Session) -> Conf {
let file_name = match utils::conf::lookup_conf_file() {
@ -467,12 +501,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(),
format_error(error)
))
.emit();
));
}
conf
@ -579,16 +612,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;
let allow_expect_in_tests = conf.allow_expect_in_tests;
let allow_unwrap_in_tests = conf.allow_unwrap_in_tests;

View File

@ -130,7 +130,7 @@ fn base_config(test_dir: &str) -> compiletest::Config {
let mut config = compiletest::Config {
edition: Some("2021".into()),
mode: TestMode::Ui,
..compiletest::Config::default()
..Default::default()
};
if let Ok(filters) = env::var("TESTNAME") {
@ -286,6 +286,24 @@ fn run_tests(
}
env::set_current_dir(&src_path)?;
let cargo_toml_path = case.path().join("Cargo.toml");
let cargo_content = fs::read(&cargo_toml_path)?;
let cargo_parsed: toml::Value = toml::from_str(
std::str::from_utf8(&cargo_content).expect("`Cargo.toml` is not a valid utf-8 file!"),
)
.expect("Can't parse `Cargo.toml`");
let _g = VarGuard::set("CARGO_MANIFEST_DIR", case.path());
let _h = VarGuard::set(
"CARGO_PKG_RUST_VERSION",
cargo_parsed
.get("package")
.and_then(|p| p.get("rust-version"))
.and_then(toml::Value::as_str)
.unwrap_or(""),
);
for file in fs::read_dir(&src_path)? {
let file = file?;
if file.file_type()?.is_dir() {

View File

@ -0,0 +1,8 @@
[package]
name = "fail-both-diff"
version = "0.1.0"
rust-version = "1.56"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.59"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,16 @@
warning: the MSRV in `clippy.toml` and `Cargo.toml` differ; using `1.59.0` from `clippy.toml`
error: unnecessary structure name repetition
--> $DIR/main.rs:6:21
|
LL | pub fn bar() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
note: the lint level is defined here
--> $DIR/main.rs:1:9
|
LL | #![deny(clippy::use_self)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error; 1 warning emitted

View File

@ -0,0 +1,8 @@
[package]
name = "fail-both-same"
version = "0.1.0"
rust-version = "1.57.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.57"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,14 @@
error: unnecessary structure name repetition
--> $DIR/main.rs:6:21
|
LL | pub fn bar() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
note: the lint level is defined here
--> $DIR/main.rs:1:9
|
LL | #![deny(clippy::use_self)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -0,0 +1,8 @@
[package]
name = "fail-cargo"
version = "0.1.0"
rust-version = "1.56.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,14 @@
error: unnecessary structure name repetition
--> $DIR/main.rs:6:21
|
LL | pub fn bar() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
note: the lint level is defined here
--> $DIR/main.rs:1:9
|
LL | #![deny(clippy::use_self)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -0,0 +1,7 @@
[package]
name = "fail-clippy"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.58"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,14 @@
error: unnecessary structure name repetition
--> $DIR/main.rs:6:21
|
LL | pub fn bar() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
note: the lint level is defined here
--> $DIR/main.rs:1:9
|
LL | #![deny(clippy::use_self)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -0,0 +1,8 @@
[package]
name = "fail-file-attr"
version = "0.1.0"
rust-version = "1.13"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.13.0"

View File

@ -0,0 +1,16 @@
// FIXME: this should produce a warning, because the attribute says 1.58 and the cargo.toml file
// says 1.13
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.58.0"]
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,14 @@
error: unnecessary structure name repetition
--> $DIR/main.rs:11:21
|
LL | pub fn bar() -> Foo {
| ^^^ help: use the applicable keyword: `Self`
|
note: the lint level is defined here
--> $DIR/main.rs:6:9
|
LL | #![deny(clippy::use_self)]
| ^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -0,0 +1,8 @@
[package]
name = "fail-both-same"
version = "0.1.0"
rust-version = "1.13.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.13"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,8 @@
[package]
name = "fail-cargo"
version = "0.1.0"
rust-version = "1.13.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,7 @@
[package]
name = "fail-clippy"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.13"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,8 @@
[package]
name = "fail-file-attr"
version = "0.1.0"
rust-version = "1.59"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,13 @@
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.13.0"]
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,8 @@
[package]
name = "warn-both-diff"
version = "0.1.0"
rust-version = "1.56.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
msrv = "1.13"

View File

@ -0,0 +1,11 @@
#![deny(clippy::use_self)]
pub struct Foo;
impl Foo {
pub fn bar() -> Foo {
Foo
}
}
fn main() {}

View File

@ -0,0 +1,4 @@
warning: the MSRV in `clippy.toml` and `Cargo.toml` differ; using `1.13.0` from `clippy.toml`
warning: 1 warning emitted

View File

@ -1,2 +1,2 @@
Using config file `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/.clippy.toml`
Warning: `$SRC_DIR/tests/ui-cargo/multiple_config_files/warn/clippy.toml` will be ignored.
Using config file `$SRC_DIR/.clippy.toml`
Warning: `$SRC_DIR/clippy.toml` will be ignored.