Skip to content

Commit 921e9dc

Browse files
committed
Warn about types not meeting MSRV
For example, the `Duration` type from the standard library was only introduced in Rust 1.3.0.
1 parent ed176b7 commit 921e9dc

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::is_in_test;
44
use clippy_utils::msrvs::Msrv;
55
use rustc_attr_data_structures::{RustcVersion, Stability, StableSince};
66
use rustc_data_structures::fx::FxHashMap;
7-
use rustc_hir::{Expr, ExprKind, HirId, QPath};
7+
use rustc_hir::{self as hir, AmbigArg, Expr, ExprKind, HirId, QPath};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::impl_lint_pass;
@@ -186,6 +186,14 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
186186
_ => {},
187187
}
188188
}
189+
190+
fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
191+
if let hir::TyKind::Path(qpath @ (QPath::Resolved(..) | QPath::TypeRelative(..))) = hir_ty.kind
192+
&& let Some(ty_def_id) = cx.qpath_res(&qpath, hir_ty.hir_id).opt_def_id()
193+
{
194+
self.emit_lint_if_under_msrv(cx, ty_def_id, hir_ty.hir_id, hir_ty.span);
195+
}
196+
}
189197
}
190198

191199
/// Heuristic checking if the node `hir_id` is under a `#[cfg()]` or `#[cfg_attr()]`

tests/ui/incompatible_msrv.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ fn foo() {
2626
//~^ incompatible_msrv
2727
}
2828

29+
#[clippy::msrv = "1.2.0"]
30+
static NO_BODY_BAD_MSRV: Option<Duration> = None;
31+
//~^ incompatible_msrv
32+
33+
static NO_BODY_GOOD_MSRV: Option<Duration> = None;
34+
35+
#[clippy::msrv = "1.2.0"]
36+
fn bad_type_msrv() {
37+
let _: Option<Duration> = None;
38+
//~^ incompatible_msrv
39+
}
40+
2941
#[test]
3042
fn test() {
3143
sleep(Duration::new(1, 0));

tests/ui/incompatible_msrv.stderr

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is
1919
LL | sleep(Duration::new(1, 0));
2020
| ^^^^^
2121

22+
error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0`
23+
--> tests/ui/incompatible_msrv.rs:30:33
24+
|
25+
LL | static NO_BODY_BAD_MSRV: Option<Duration> = None;
26+
| ^^^^^^^^
27+
28+
error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0`
29+
--> tests/ui/incompatible_msrv.rs:37:19
30+
|
31+
LL | let _: Option<Duration> = None;
32+
| ^^^^^^^^
33+
2234
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
23-
--> tests/ui/incompatible_msrv.rs:49:17
35+
--> tests/ui/incompatible_msrv.rs:61:17
2436
|
2537
LL | let _ = core::iter::once_with(|| 0);
2638
| ^^^^^^^^^^^^^^^^^^^^^
2739

2840
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
29-
--> tests/ui/incompatible_msrv.rs:56:21
41+
--> tests/ui/incompatible_msrv.rs:68:21
3042
|
3143
LL | let _ = core::iter::once_with(|| $msg);
3244
| ^^^^^^^^^^^^^^^^^^^^^
@@ -37,48 +49,48 @@ LL | my_panic!("foo");
3749
= note: this error originates in the macro `my_panic` (in Nightly builds, run with -Z macro-backtrace for more info)
3850

3951
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0`
40-
--> tests/ui/incompatible_msrv.rs:63:13
52+
--> tests/ui/incompatible_msrv.rs:75:13
4153
|
4254
LL | assert!(core::iter::once_with(|| 0).next().is_some());
4355
| ^^^^^^^^^^^^^^^^^^^^^
4456

4557
error: current MSRV (Minimum Supported Rust Version) is `1.80.0` but this item is stable since `1.82.0`
46-
--> tests/ui/incompatible_msrv.rs:76:13
58+
--> tests/ui/incompatible_msrv.rs:88:13
4759
|
4860
LL | let _ = std::iter::repeat_n((), 5);
4961
| ^^^^^^^^^^^^^^^^^^^
5062

5163
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
52-
--> tests/ui/incompatible_msrv.rs:81:13
64+
--> tests/ui/incompatible_msrv.rs:93:13
5365
|
5466
LL | let _ = std::iter::repeat_n((), 5);
5567
| ^^^^^^^^^^^^^^^^^^^
5668

5769
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
58-
--> tests/ui/incompatible_msrv.rs:86:17
70+
--> tests/ui/incompatible_msrv.rs:98:17
5971
|
6072
LL | let _ = std::iter::repeat_n((), 5);
6173
| ^^^^^^^^^^^^^^^^^^^
6274
|
6375
= note: you may want to conditionally increase the MSRV considered by Clippy using the `clippy::msrv` attribute
6476

6577
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0`
66-
--> tests/ui/incompatible_msrv.rs:91:17
78+
--> tests/ui/incompatible_msrv.rs:103:17
6779
|
6880
LL | let _ = std::iter::repeat_n((), 5);
6981
| ^^^^^^^^^^^^^^^^^^^
7082

7183
error: current MSRV (Minimum Supported Rust Version) is `1.78.0` but this item is stable since `1.84.0`
72-
--> tests/ui/incompatible_msrv.rs:104:7
84+
--> tests/ui/incompatible_msrv.rs:116:7
7385
|
7486
LL | r.isqrt()
7587
| ^^^^^^^
7688

7789
error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.85.0`
78-
--> tests/ui/incompatible_msrv.rs:109:13
90+
--> tests/ui/incompatible_msrv.rs:121:13
7991
|
8092
LL | let _ = std::io::ErrorKind::CrossesDevices;
8193
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8294

83-
error: aborting due to 12 previous errors
95+
error: aborting due to 14 previous errors
8496

0 commit comments

Comments
 (0)