Skip to content

Commit

Permalink
Auto merge of #8774 - hellow554:cargo-rust-version, r=flip1995
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jun 28, 2022
2 parents 373bb57 + f0a1cd5 commit b776fb8
Show file tree
Hide file tree
Showing 38 changed files with 345 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -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"

Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
50 changes: 37 additions & 13 deletions clippy_lints/src/lib.rs
Expand Up @@ -53,6 +53,7 @@ extern crate clippy_utils;
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.
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 19 additions & 1 deletion tests/compile-test.rs
Expand Up @@ -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") {
Expand Down Expand Up @@ -286,6 +286,24 @@ fn run_ui_cargo() {
}

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() {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_diff/Cargo.toml
@@ -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]
@@ -0,0 +1 @@
msrv = "1.59"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr
@@ -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

8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_same/Cargo.toml
@@ -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]
@@ -0,0 +1 @@
msrv = "1.57"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr
@@ -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

8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_cargo/Cargo.toml
@@ -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]
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr
@@ -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

7 changes: 7 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_clippy/Cargo.toml
@@ -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]
1 change: 1 addition & 0 deletions tests/ui-cargo/cargo_rust_version/fail_clippy/clippy.toml
@@ -0,0 +1 @@
msrv = "1.58"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr
@@ -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

8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_file_attr/Cargo.toml
@@ -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]
@@ -0,0 +1 @@
msrv = "1.13.0"
16 changes: 16 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.rs
@@ -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() {}
14 changes: 14 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr
@@ -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

8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_both_same/Cargo.toml
@@ -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]
@@ -0,0 +1 @@
msrv = "1.13"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_both_same/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_cargo/Cargo.toml
@@ -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]
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_cargo/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_clippy/Cargo.toml
@@ -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]
1 change: 1 addition & 0 deletions tests/ui-cargo/cargo_rust_version/pass_clippy/clippy.toml
@@ -0,0 +1 @@
msrv = "1.13"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/pass_clippy/src/main.rs
@@ -0,0 +1,11 @@
#![deny(clippy::use_self)]

pub struct Foo;

impl Foo {
pub fn bar() -> Foo {
Foo
}
}

fn main() {}

0 comments on commit b776fb8

Please sign in to comment.