Skip to content

Commit bc821a7

Browse files
Warn if an item coming from more recent version than MSRV is used
1 parent 37947ff commit bc821a7

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5198,6 +5198,7 @@ Released 2018-09-13
51985198
[`implied_bounds_in_impls`]: https://rust-lang.github.io/rust-clippy/master/index.html#implied_bounds_in_impls
51995199
[`impossible_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#impossible_comparisons
52005200
[`imprecise_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#imprecise_flops
5201+
[`incompatible_msrv`]: https://rust-lang.github.io/rust-clippy/master/index.html#incompatible_msrv
52015202
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
52025203
[`inconsistent_struct_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_struct_constructor
52035204
[`incorrect_clone_impl_on_copy_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type

clippy_config/src/msrvs.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_semver::RustcVersion;
33
use rustc_session::Session;
44
use rustc_span::{sym, Symbol};
55
use serde::Deserialize;
6+
use std::fmt;
67

78
macro_rules! msrv_aliases {
89
($($major:literal,$minor:literal,$patch:literal {
@@ -58,6 +59,16 @@ pub struct Msrv {
5859
stack: Vec<RustcVersion>,
5960
}
6061

62+
impl fmt::Display for Msrv {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
if let Some(msrv) = self.current() {
65+
write!(f, "{msrv}")
66+
} else {
67+
f.write_str("1.0.0")
68+
}
69+
}
70+
}
71+
6172
impl<'de> Deserialize<'de> for Msrv {
6273
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
6374
where

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
212212
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
213213
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
214214
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
215+
crate::incompatible_msrv::INCOMPATIBLE_MSRV_INFO,
215216
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
216217
crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO,
217218
crate::indexing_slicing::INDEXING_SLICING_INFO,

clippy_lints/src/incompatible_msrv.rs

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use clippy_config::msrvs::Msrv;
2+
use clippy_utils::diagnostics::span_lint;
3+
use rustc_ast::Attribute;
4+
use rustc_attr::{StabilityLevel, StableSince};
5+
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_hir::{Expr, ExprKind};
7+
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::ty::TyCtxt;
9+
use rustc_semver::RustcVersion;
10+
use rustc_session::impl_lint_pass;
11+
use rustc_span::def_id::DefId;
12+
use rustc_span::Span;
13+
14+
declare_clippy_lint! {
15+
/// ### What it does
16+
///
17+
/// This lint checks that no function newer than the defined MSRV (minimum
18+
/// supported rust version) is used in the crate.
19+
///
20+
/// ### Why is this bad?
21+
///
22+
/// It would prevent this project to be actually used with the specified MSRV.
23+
///
24+
/// ### Example
25+
/// ```no_run
26+
/// // MSRV of 1.3.0
27+
/// use std::thread::sleep;
28+
/// use std::time::Duration;
29+
///
30+
/// // Sleep was defined in `1.4.0`.
31+
/// sleep(Duration::new(1, 0));
32+
/// ```
33+
///
34+
/// To fix this problem, either increase your MSRV or use another item
35+
/// available in your current MSRV.
36+
#[clippy::version = "1.77.0"]
37+
pub INCOMPATIBLE_MSRV,
38+
suspicious,
39+
"ensures that all items used in the crate are available for the current MSRV"
40+
}
41+
42+
pub struct IncompatibleMsrv {
43+
msrv: Msrv,
44+
is_above_msrv: FxHashMap<DefId, RustcVersion>,
45+
}
46+
47+
impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);
48+
49+
impl IncompatibleMsrv {
50+
pub fn new(msrv: Msrv) -> Self {
51+
Self {
52+
msrv,
53+
is_above_msrv: FxHashMap::default(),
54+
}
55+
}
56+
57+
#[allow(clippy::cast_lossless)]
58+
fn get_def_id_version(&mut self, tcx: TyCtxt<'_>, def_id: DefId) -> RustcVersion {
59+
if let Some(version) = self.is_above_msrv.get(&def_id) {
60+
return *version;
61+
}
62+
let version = if let Some(version) = tcx
63+
.lookup_stability(def_id)
64+
.and_then(|stability| match stability.level {
65+
StabilityLevel::Stable {
66+
since: StableSince::Version(version),
67+
..
68+
} => Some(RustcVersion::new(
69+
version.major as _,
70+
version.minor as _,
71+
version.patch as _,
72+
)),
73+
_ => None,
74+
}) {
75+
version
76+
} else if let Some(parent_def_id) = tcx.opt_parent(def_id) {
77+
self.get_def_id_version(tcx, parent_def_id)
78+
} else {
79+
RustcVersion::new(1, 0, 0)
80+
};
81+
self.is_above_msrv.insert(def_id, version);
82+
version
83+
}
84+
85+
fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, def_id: DefId, span: Span) {
86+
if def_id.is_local() {
87+
// We don't check local items since their MSRV is supposed to always be valid.
88+
return;
89+
}
90+
let version = self.get_def_id_version(cx.tcx, def_id);
91+
if self.msrv.meets(version) {
92+
return;
93+
}
94+
self.emit_lint_for(cx, span, version);
95+
}
96+
97+
fn emit_lint_for(&self, cx: &LateContext<'_>, span: Span, version: RustcVersion) {
98+
span_lint(
99+
cx,
100+
INCOMPATIBLE_MSRV,
101+
span,
102+
&format!(
103+
"current MSRV is `{}` but this item is stable since `{version}`",
104+
self.msrv
105+
),
106+
);
107+
}
108+
}
109+
110+
impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
111+
fn enter_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
112+
self.msrv.enter_lint_attrs(cx.tcx.sess, attrs);
113+
}
114+
115+
fn exit_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
116+
self.msrv.exit_lint_attrs(cx.tcx.sess, attrs);
117+
}
118+
119+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
120+
if self.msrv.current().is_none() {
121+
// If there is no MSRV, then no need to check anything...
122+
return;
123+
}
124+
match expr.kind {
125+
ExprKind::MethodCall(_, _, _, span) => {
126+
if let Some(method_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
127+
self.emit_lint_if_under_msrv(cx, method_did, span);
128+
}
129+
},
130+
ExprKind::Call(call, [_]) => {
131+
if let ExprKind::Path(qpath) = call.kind
132+
&& let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id()
133+
{
134+
self.emit_lint_if_under_msrv(cx, path_def_id, call.span);
135+
}
136+
},
137+
_ => {},
138+
}
139+
}
140+
}

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern crate rustc_abi;
2626
extern crate rustc_arena;
2727
extern crate rustc_ast;
2828
extern crate rustc_ast_pretty;
29+
extern crate rustc_attr;
2930
extern crate rustc_data_structures;
3031
extern crate rustc_driver;
3132
extern crate rustc_errors;
@@ -153,6 +154,7 @@ mod implicit_return;
153154
mod implicit_saturating_add;
154155
mod implicit_saturating_sub;
155156
mod implied_bounds_in_impls;
157+
mod incompatible_msrv;
156158
mod inconsistent_struct_constructor;
157159
mod index_refutable_slice;
158160
mod indexing_slicing;
@@ -1092,6 +1094,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
10921094
store.register_late_pass(move |_| {
10931095
Box::new(thread_local_initializer_can_be_made_const::ThreadLocalInitializerCanBeMadeConst::new(msrv()))
10941096
});
1097+
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(msrv())));
10951098
// add lints here, do not remove this comment, it's used in `new_lint`
10961099
}
10971100

0 commit comments

Comments
 (0)