Skip to content

Warn about types not meeting MSRV #15296

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

Merged
merged 1 commit into from
Jul 17, 2025
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
12 changes: 11 additions & 1 deletion clippy_lints/src/incompatible_msrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]`
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/incompatible_msrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 deletions tests/ui/incompatible_msrv.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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);
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -37,48 +49,48 @@ 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);
| ^^^^^^^^^^^^^^^^^^^
|
= 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