Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try reading rust-version from Cargo.toml #8774

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
hellow554 marked this conversation as resolved.
Show resolved Hide resolved
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -450,6 +451,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 @@ -465,12 +499,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 @@ -577,16 +610,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.59"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.57"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.58"
11 changes: 11 additions & 0 deletions tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this doesn't produce a warning. That is not optimal, but I wouldn't try to address this in this PR.

Can you add a FIXME, that this should produce some kind of warning, that it overrides the value from one of the toml files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe abandon that attribute in the long run? ;)

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

pub struct Foo;

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

fn main() {}
Loading