Skip to content

Commit e1c1ac1

Browse files
authored
configuration option to lint incompatible_msrv in test code (rust-lang#14279)
fixes rust-lang#14277 changelog: [`incompatible_msrv`]: add config option [`check-incompatible-msrv-in-tests`] to enable in `#[test]` and `#[cfg(test)]` code.
2 parents 715d3f9 + 325bfef commit e1c1ac1

File tree

10 files changed

+87
-1
lines changed

10 files changed

+87
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6343,6 +6343,7 @@ Released 2018-09-13
63436343
[`avoid-breaking-exported-api`]: https://doc.rust-lang.org/clippy/lint_configuration.html#avoid-breaking-exported-api
63446344
[`await-holding-invalid-types`]: https://doc.rust-lang.org/clippy/lint_configuration.html#await-holding-invalid-types
63456345
[`cargo-ignore-publish`]: https://doc.rust-lang.org/clippy/lint_configuration.html#cargo-ignore-publish
6346+
[`check-incompatible-msrv-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-incompatible-msrv-in-tests
63466347
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items
63476348
[`cognitive-complexity-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#cognitive-complexity-threshold
63486349
[`disallowed-macros`]: https://doc.rust-lang.org/clippy/lint_configuration.html#disallowed-macros

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ For internal testing only, ignores the current `publish` settings in the Cargo m
415415
* [`cargo_common_metadata`](https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata)
416416

417417

418+
## `check-incompatible-msrv-in-tests`
419+
Whether to check MSRV compatibility in `#[test]` and `#[cfg(test)]` code.
420+
421+
**Default Value:** `false`
422+
423+
---
424+
**Affected lints:**
425+
* [`incompatible_msrv`](https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv)
426+
427+
418428
## `check-private-items`
419429
Whether to also run the listed lints on private items.
420430

clippy_config/src/conf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ define_Conf! {
464464
/// For internal testing only, ignores the current `publish` settings in the Cargo manifest.
465465
#[lints(cargo_common_metadata)]
466466
cargo_ignore_publish: bool = false,
467+
/// Whether to check MSRV compatibility in `#[test]` and `#[cfg(test)]` code.
468+
#[lints(incompatible_msrv)]
469+
check_incompatible_msrv_in_tests: bool = false,
467470
/// Whether to also run the listed lints on private items.
468471
#[lints(missing_errors_doc, missing_panics_doc, missing_safety_doc, unnecessary_safety_doc)]
469472
check_private_items: bool = false,

clippy_lints/src/incompatible_msrv.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare_clippy_lint! {
4242
pub struct IncompatibleMsrv {
4343
msrv: Msrv,
4444
is_above_msrv: FxHashMap<DefId, RustcVersion>,
45+
check_in_tests: bool,
4546
}
4647

4748
impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);
@@ -51,6 +52,7 @@ impl IncompatibleMsrv {
5152
Self {
5253
msrv: conf.msrv.clone(),
5354
is_above_msrv: FxHashMap::default(),
55+
check_in_tests: conf.check_incompatible_msrv_in_tests,
5456
}
5557
}
5658

@@ -87,7 +89,7 @@ impl IncompatibleMsrv {
8789
return;
8890
}
8991
let version = self.get_def_id_version(cx.tcx, def_id);
90-
if self.msrv.meets(version) || is_in_test(cx.tcx, node) {
92+
if self.msrv.meets(version) || (!self.check_in_tests && is_in_test(cx.tcx, node)) {
9193
return;
9294
}
9395
if let ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) = span.ctxt().outer_expn_data().kind {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
2+
--> tests/ui-toml/check_incompatible_msrv_in_tests/check_incompatible_msrv_in_tests.rs:14:5
3+
|
4+
LL | sleep(Duration::new(1, 0))
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::incompatible-msrv` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`
9+
10+
error: aborting due to 1 previous error
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
2+
--> tests/ui-toml/check_incompatible_msrv_in_tests/check_incompatible_msrv_in_tests.rs:14:5
3+
|
4+
LL | sleep(Duration::new(1, 0))
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::incompatible-msrv` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::incompatible_msrv)]`
9+
10+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
11+
--> tests/ui-toml/check_incompatible_msrv_in_tests/check_incompatible_msrv_in_tests.rs:20:5
12+
|
13+
LL | sleep(Duration::new(1, 0));
14+
| ^^^^^
15+
16+
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.4.0`
17+
--> tests/ui-toml/check_incompatible_msrv_in_tests/check_incompatible_msrv_in_tests.rs:28:9
18+
|
19+
LL | sleep(Duration::new(1, 0));
20+
| ^^^^^
21+
22+
error: aborting due to 3 previous errors
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@compile-flags: --test
2+
//@revisions: default enabled
3+
//@[enabled] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/check_incompatible_msrv_in_tests/enabled
4+
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/check_incompatible_msrv_in_tests/default
5+
6+
#![warn(clippy::incompatible_msrv)]
7+
#![feature(custom_inner_attributes)]
8+
#![clippy::msrv = "1.3.0"]
9+
10+
use std::thread::sleep;
11+
use std::time::Duration;
12+
13+
fn main() {
14+
sleep(Duration::new(1, 0))
15+
//~^ incompatible_msrv
16+
}
17+
18+
#[test]
19+
fn test() {
20+
sleep(Duration::new(1, 0));
21+
//~[enabled]^ incompatible_msrv
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
fn helper() {
28+
sleep(Duration::new(1, 0));
29+
//~[enabled]^ incompatible_msrv
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# default config has check-incompatible-msrv-in-tests as false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
check-incompatible-msrv-in-tests = true

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
3131
await-holding-invalid-types
3232
blacklisted-names
3333
cargo-ignore-publish
34+
check-incompatible-msrv-in-tests
3435
check-private-items
3536
cognitive-complexity-threshold
3637
cyclomatic-complexity-threshold
@@ -122,6 +123,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
122123
await-holding-invalid-types
123124
blacklisted-names
124125
cargo-ignore-publish
126+
check-incompatible-msrv-in-tests
125127
check-private-items
126128
cognitive-complexity-threshold
127129
cyclomatic-complexity-threshold
@@ -213,6 +215,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
213215
await-holding-invalid-types
214216
blacklisted-names
215217
cargo-ignore-publish
218+
check-incompatible-msrv-in-tests
216219
check-private-items
217220
cognitive-complexity-threshold
218221
cyclomatic-complexity-threshold

0 commit comments

Comments
 (0)