Skip to content

Commit 36793aa

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

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-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

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
use std::collections::hash_map::Entry;
15+
16+
declare_clippy_lint! {
17+
/// ### What it does
18+
///
19+
/// ### Why is this bad?
20+
///
21+
/// ### Example
22+
/// ```no_run
23+
/// // example code where clippy issues a warning
24+
/// ```
25+
/// Use instead:
26+
/// ```no_run
27+
/// // example code which does not raise clippy warning
28+
/// ```
29+
#[clippy::version = "1.77.0"]
30+
pub INCOMPATIBLE_MSRV,
31+
suspicious,
32+
"default lint description"
33+
}
34+
35+
pub struct IncompatibleMsrv {
36+
msrv: Msrv,
37+
is_above_msrv: FxHashMap<DefId, RustcVersion>,
38+
}
39+
40+
impl_lint_pass!(IncompatibleMsrv => [INCOMPATIBLE_MSRV]);
41+
42+
impl IncompatibleMsrv {
43+
pub fn new(msrv: Msrv) -> Self {
44+
Self {
45+
msrv,
46+
is_above_msrv: FxHashMap::default(),
47+
}
48+
}
49+
50+
fn get_def_id_version(&mut self, tcx: TyCtxt<'_>, def_id: DefId) -> RustcVersion {
51+
match self.is_above_msrv.entry(def_id) {
52+
Entry::Occupied(o) => *o.get(),
53+
Entry::Vacant(v) => {
54+
let version = tcx
55+
.lookup_stability(def_id)
56+
.and_then(|stability| match stability.level {
57+
StabilityLevel::Stable {
58+
since: StableSince::Version(version),
59+
..
60+
} => Some(RustcVersion::new(
61+
version.major as _,
62+
version.minor as _,
63+
version.patch as _,
64+
)),
65+
_ => None,
66+
})
67+
.unwrap_or(RustcVersion::new(1, 0, 0));
68+
*v.insert(version)
69+
},
70+
}
71+
}
72+
73+
fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, mut def_id: DefId, span: Span) {
74+
loop {
75+
if def_id.is_local() {
76+
// We don't check local items since their MSRV is supposed to always be valid.
77+
return;
78+
}
79+
let version = self.get_def_id_version(cx.tcx, def_id);
80+
if !self.msrv.meets(version) {
81+
self.emit_lint_for(cx, span, version);
82+
} else {
83+
return;
84+
}
85+
match cx.tcx.opt_parent(def_id) {
86+
Some(parent_def_id) => def_id = parent_def_id,
87+
None => return,
88+
}
89+
}
90+
}
91+
92+
fn emit_lint_for(&self, cx: &LateContext<'_>, span: Span, version: RustcVersion) {
93+
span_lint(
94+
cx,
95+
INCOMPATIBLE_MSRV,
96+
span,
97+
&format!("MSRV is `{}` but this item is stable since `{version}`", self.msrv),
98+
);
99+
}
100+
}
101+
102+
impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
103+
fn enter_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
104+
self.msrv.enter_lint_attrs(cx.tcx.sess, attrs);
105+
}
106+
107+
fn exit_lint_attrs(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
108+
self.msrv.exit_lint_attrs(cx.tcx.sess, attrs);
109+
}
110+
111+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
112+
if self.msrv.current().is_none() {
113+
// If there is no MSRV, then no need to check anything...
114+
return;
115+
}
116+
match expr.kind {
117+
ExprKind::MethodCall(_, _, _, span) => {
118+
if let Some(method_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
119+
self.emit_lint_if_under_msrv(cx, method_did, span);
120+
}
121+
},
122+
ExprKind::Call(call, [_]) => {
123+
if let ExprKind::Path(qpath) = call.kind
124+
&& let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id()
125+
{
126+
self.emit_lint_if_under_msrv(cx, path_def_id, call.span);
127+
}
128+
},
129+
_ => {},
130+
}
131+
}
132+
}

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)