diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2025-07-17 12:31:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-17 12:31:15 +0000 |
| commit | e62e27bf5bbae5d0ba596ae43356a7c9c988a067 (patch) | |
| tree | fe1d210122257d10c05ac307c2bb247ac4ec2c12 | |
| parent | ed176b7b8889d6ae77c5be54dc48bcb1bbbf9195 (diff) | |
| parent | 11bfeca960046069b5a994ae00fd150818ecb3b8 (diff) | |
| download | rust-e62e27bf5bbae5d0ba596ae43356a7c9c988a067.tar.gz rust-e62e27bf5bbae5d0ba596ae43356a7c9c988a067.zip | |
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
| -rw-r--r-- | clippy_lints/src/incompatible_msrv.rs | 12 | ||||
| -rw-r--r-- | tests/ui/incompatible_msrv.rs | 18 | ||||
| -rw-r--r-- | tests/ui/incompatible_msrv.stderr | 32 |
3 files changed, 51 insertions, 11 deletions
diff --git a/clippy_lints/src/incompatible_msrv.rs b/clippy_lints/src/incompatible_msrv.rs index a31168fc746..addb1e7d755 100644 --- a/clippy_lints/src/incompatible_msrv.rs +++ b/clippy_lints/src/incompatible_msrv.rs @@ -4,7 +4,7 @@ use clippy_utils::is_in_test; use clippy_utils::msrvs::Msrv; use rustc_attr_data_structures::{RustcVersion, Stability, StableSince}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::{Expr, ExprKind, HirId, QPath}; +use rustc_hir::{self as hir, AmbigArg, Expr, ExprKind, HirId, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::TyCtxt; use rustc_session::impl_lint_pass; @@ -186,6 +186,16 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv { _ => {}, } } + + fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &'tcx hir::Ty<'tcx, AmbigArg>) { + if let hir::TyKind::Path(qpath @ (QPath::Resolved(..) | QPath::TypeRelative(..))) = hir_ty.kind + && let Some(ty_def_id) = cx.qpath_res(&qpath, hir_ty.hir_id).opt_def_id() + // `CStr` and `CString` have been moved around but have been available since Rust 1.0.0 + && !matches!(cx.tcx.get_diagnostic_name(ty_def_id), Some(sym::cstr_type | sym::cstring_type)) + { + self.emit_lint_if_under_msrv(cx, ty_def_id, hir_ty.hir_id, hir_ty.span); + } + } } /// Heuristic checking if the node `hir_id` is under a `#[cfg()]` or `#[cfg_attr()]` diff --git a/tests/ui/incompatible_msrv.rs b/tests/ui/incompatible_msrv.rs index 37e55e9e18a..1f9069c7c1c 100644 --- a/tests/ui/incompatible_msrv.rs +++ b/tests/ui/incompatible_msrv.rs @@ -26,6 +26,18 @@ fn foo() { //~^ incompatible_msrv } +#[clippy::msrv = "1.2.0"] +static NO_BODY_BAD_MSRV: Option<Duration> = None; +//~^ incompatible_msrv + +static NO_BODY_GOOD_MSRV: Option<Duration> = None; + +#[clippy::msrv = "1.2.0"] +fn bad_type_msrv() { + let _: Option<Duration> = None; + //~^ incompatible_msrv +} + #[test] fn test() { sleep(Duration::new(1, 0)); @@ -77,6 +89,12 @@ fn issue14212() { //~^ incompatible_msrv } +#[clippy::msrv = "1.0.0"] +fn cstr_and_cstring_ok() { + let _: Option<&'static std::ffi::CStr> = None; + let _: Option<std::ffi::CString> = None; +} + fn local_msrv_change_suggestion() { let _ = std::iter::repeat_n((), 5); //~^ incompatible_msrv diff --git a/tests/ui/incompatible_msrv.stderr b/tests/ui/incompatible_msrv.stderr index ed5ad5e1660..fff1b956561 100644 --- a/tests/ui/incompatible_msrv.stderr +++ b/tests/ui/incompatible_msrv.stderr @@ -19,14 +19,26 @@ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is LL | sleep(Duration::new(1, 0)); | ^^^^^ +error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0` + --> tests/ui/incompatible_msrv.rs:30:33 + | +LL | static NO_BODY_BAD_MSRV: Option<Duration> = None; + | ^^^^^^^^ + +error: current MSRV (Minimum Supported Rust Version) is `1.2.0` but this item is stable since `1.3.0` + --> tests/ui/incompatible_msrv.rs:37:19 + | +LL | let _: Option<Duration> = None; + | ^^^^^^^^ + error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0` - --> tests/ui/incompatible_msrv.rs:49:17 + --> tests/ui/incompatible_msrv.rs:61:17 | LL | let _ = core::iter::once_with(|| 0); | ^^^^^^^^^^^^^^^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0` - --> tests/ui/incompatible_msrv.rs:56:21 + --> tests/ui/incompatible_msrv.rs:68:21 | LL | let _ = core::iter::once_with(|| $msg); | ^^^^^^^^^^^^^^^^^^^^^ @@ -37,25 +49,25 @@ LL | my_panic!("foo"); = note: this error originates in the macro `my_panic` (in Nightly builds, run with -Z macro-backtrace for more info) error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.43.0` - --> tests/ui/incompatible_msrv.rs:63:13 + --> tests/ui/incompatible_msrv.rs:75:13 | LL | assert!(core::iter::once_with(|| 0).next().is_some()); | ^^^^^^^^^^^^^^^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.80.0` but this item is stable since `1.82.0` - --> tests/ui/incompatible_msrv.rs:76:13 + --> tests/ui/incompatible_msrv.rs:88:13 | LL | let _ = std::iter::repeat_n((), 5); | ^^^^^^^^^^^^^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0` - --> tests/ui/incompatible_msrv.rs:81:13 + --> tests/ui/incompatible_msrv.rs:99:13 | LL | let _ = std::iter::repeat_n((), 5); | ^^^^^^^^^^^^^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0` - --> tests/ui/incompatible_msrv.rs:86:17 + --> tests/ui/incompatible_msrv.rs:104:17 | LL | let _ = std::iter::repeat_n((), 5); | ^^^^^^^^^^^^^^^^^^^ @@ -63,22 +75,22 @@ LL | let _ = std::iter::repeat_n((), 5); = note: you may want to conditionally increase the MSRV considered by Clippy using the `clippy::msrv` attribute error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.82.0` - --> tests/ui/incompatible_msrv.rs:91:17 + --> tests/ui/incompatible_msrv.rs:109:17 | LL | let _ = std::iter::repeat_n((), 5); | ^^^^^^^^^^^^^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.78.0` but this item is stable since `1.84.0` - --> tests/ui/incompatible_msrv.rs:104:7 + --> tests/ui/incompatible_msrv.rs:122:7 | LL | r.isqrt() | ^^^^^^^ error: current MSRV (Minimum Supported Rust Version) is `1.3.0` but this item is stable since `1.85.0` - --> tests/ui/incompatible_msrv.rs:109:13 + --> tests/ui/incompatible_msrv.rs:127:13 | LL | let _ = std::io::ErrorKind::CrossesDevices; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 12 previous errors +error: aborting due to 14 previous errors |
