Skip to content

Commit e62e27b

Browse files
authored
Warn about types not meeting MSRV (#15296)
For example, the `Duration` type from the standard library was only introduced in Rust 1.3.0. changelog: [`incompatible_msrv`]: recognize types exceeding MSRV as well r? Jarcho @rustbot label +C-bug +I-false-negative
2 parents ed176b7 + 11bfeca commit e62e27b

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

clippy_lints/src/incompatible_msrv.rs

Lines changed: 11 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,16 @@ 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+
// `CStr` and `CString` have been moved around but have been available since Rust 1.0.0
194+
&& !matches!(cx.tcx.get_diagnostic_name(ty_def_id), Some(sym::cstr_type | sym::cstring_type))
195+
{
196+
self.emit_lint_if_under_msrv(cx, ty_def_id, hir_ty.hir_id, hir_ty.span);
197+
}
198+
}
189199
}
190200

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

tests/ui/incompatible_msrv.rs

Lines changed: 18 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));
@@ -77,6 +89,12 @@ fn issue14212() {
7789
//~^ incompatible_msrv
7890
}
7991

92+
#[clippy::msrv = "1.0.0"]
93+
fn cstr_and_cstring_ok() {
94+
let _: Option<&'static std::ffi::CStr> = None;
95+
let _: Option<std::ffi::CString> = None;
96+
}
97+
8098
fn local_msrv_change_suggestion() {
8199
let _ = std::iter::repeat_n((), 5);
82100
//~^ incompatible_msrv

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:99: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:104: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:109: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:122: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:127: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)