clippy_utils/
msrvs.rs

1use rustc_ast::Attribute;
2use rustc_ast::attr::AttributeExt;
3
4use rustc_attr_parsing::{RustcVersion, parse_version};
5use rustc_lint::LateContext;
6use rustc_session::Session;
7use rustc_span::{Symbol, sym};
8use serde::Deserialize;
9use smallvec::SmallVec;
10use std::iter::once;
11use std::sync::atomic::{AtomicBool, Ordering};
12
13macro_rules! msrv_aliases {
14    ($($major:literal,$minor:literal,$patch:literal {
15        $($name:ident),* $(,)?
16    })*) => {
17        $($(
18        pub const $name: RustcVersion = RustcVersion { major: $major, minor :$minor, patch: $patch };
19        )*)*
20    };
21}
22
23// names may refer to stabilized feature flags or library items
24msrv_aliases! {
25    1,87,0 { OS_STR_DISPLAY, INT_MIDPOINT }
26    1,85,0 { UINT_FLOAT_MIDPOINT }
27    1,84,0 { CONST_OPTION_AS_SLICE }
28    1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY, CONST_MUT_REFS, CONST_UNWRAP }
29    1,82,0 { IS_NONE_OR, REPEAT_N, RAW_REF_OP }
30    1,81,0 { LINT_REASONS_STABILIZATION, ERROR_IN_CORE, EXPLICIT_SELF_TYPE_ELISION }
31    1,80,0 { BOX_INTO_ITER, LAZY_CELL }
32    1,77,0 { C_STR_LITERALS }
33    1,76,0 { PTR_FROM_REF, OPTION_RESULT_INSPECT }
34    1,75,0 { OPTION_AS_SLICE }
35    1,74,0 { REPR_RUST, IO_ERROR_OTHER }
36    1,73,0 { DIV_CEIL }
37    1,71,0 { TUPLE_ARRAY_CONVERSIONS, BUILD_HASHER_HASH_ONE }
38    1,70,0 { OPTION_RESULT_IS_VARIANT_AND, BINARY_HEAP_RETAIN }
39    1,68,0 { PATH_MAIN_SEPARATOR_STR }
40    1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
41    1,63,0 { CLONE_INTO }
42    1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN }
43    1,59,0 { THREAD_LOCAL_CONST_INIT }
44    1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
45    1,57,0 { MAP_WHILE }
46    1,56,0 { CONST_FN_UNION }
47    1,55,0 { SEEK_REWIND }
48    1,54,0 { INTO_KEYS }
49    1,53,0 { OR_PATTERNS, INTEGER_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN, ARRAY_INTO_ITERATOR }
50    1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST }
51    1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS }
52    1,50,0 { BOOL_THEN, CLAMP, SLICE_FILL }
53    1,47,0 { TAU, IS_ASCII_DIGIT_CONST, ARRAY_IMPL_ANY_LEN, SATURATING_SUB_CONST }
54    1,46,0 { CONST_IF_MATCH }
55    1,45,0 { STR_STRIP_PREFIX }
56    1,43,0 { LOG2_10, LOG10_2, NUMERIC_ASSOCIATED_CONSTANTS }
57    1,42,0 { MATCHES_MACRO, SLICE_PATTERNS, PTR_SLICE_RAW_PARTS }
58    1,41,0 { RE_REBALANCING_COHERENCE, RESULT_MAP_OR_ELSE }
59    1,40,0 { MEM_TAKE, NON_EXHAUSTIVE, OPTION_AS_DEREF }
60    1,38,0 { POINTER_CAST, REM_EUCLID }
61    1,37,0 { TYPE_ALIAS_ENUM_VARIANTS }
62    1,36,0 { ITERATOR_COPIED }
63    1,35,0 { OPTION_COPIED, RANGE_CONTAINS }
64    1,34,0 { TRY_FROM }
65    1,33,0 { UNDERSCORE_IMPORTS }
66    1,31,0 { OPTION_REPLACE }
67    1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
68    1,29,0 { ITER_FLATTEN }
69    1,28,0 { FROM_BOOL, REPEAT_WITH }
70    1,27,0 { ITERATOR_TRY_FOLD }
71    1,26,0 { RANGE_INCLUSIVE, STRING_RETAIN }
72    1,24,0 { IS_ASCII_DIGIT }
73    1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
74    1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
75    1,16,0 { STR_REPEAT }
76    1,15,0 { MAYBE_BOUND_IN_WHERE }
77    1,13,0 { QUESTION_MARK_OPERATOR }
78}
79
80/// `#[clippy::msrv]` attributes are rarely used outside of Clippy's test suite, as a basic
81/// optimization we can skip traversing the HIR in [`Msrv::meets`] if we never saw an MSRV attribute
82/// during the early lint passes
83static SEEN_MSRV_ATTR: AtomicBool = AtomicBool::new(false);
84
85/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in late
86/// lint passes, use [`MsrvStack`] for early passes
87#[derive(Copy, Clone, Debug, Default)]
88pub struct Msrv(Option<RustcVersion>);
89
90impl<'de> Deserialize<'de> for Msrv {
91    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
92    where
93        D: serde::Deserializer<'de>,
94    {
95        let v = String::deserialize(deserializer)?;
96        parse_version(Symbol::intern(&v))
97            .map(|v| Self(Some(v)))
98            .ok_or_else(|| serde::de::Error::custom("not a valid Rust version"))
99    }
100}
101
102impl Msrv {
103    /// Returns the MSRV at the current node
104    ///
105    /// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
106    /// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
107    pub fn current(self, cx: &LateContext<'_>) -> Option<RustcVersion> {
108        if SEEN_MSRV_ATTR.load(Ordering::Relaxed) {
109            let start = cx.last_node_with_lint_attrs;
110            if let Some(msrv_attr) = once(start)
111                .chain(cx.tcx.hir_parent_id_iter(start))
112                .find_map(|id| parse_attrs(cx.tcx.sess, cx.tcx.hir_attrs(id)))
113            {
114                return Some(msrv_attr);
115            }
116        }
117
118        self.0
119    }
120
121    /// Checks if a required version from [this module](self) is met at the current node
122    ///
123    /// If the crate being linted uses an `#[clippy::msrv]` attribute this will search the parent
124    /// nodes for that attribute, prefer to run this check after cheaper pattern matching operations
125    pub fn meets(self, cx: &LateContext<'_>, required: RustcVersion) -> bool {
126        self.current(cx).is_none_or(|msrv| msrv >= required)
127    }
128
129    pub fn read_cargo(&mut self, sess: &Session) {
130        let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION")
131            .ok()
132            .and_then(|v| parse_version(Symbol::intern(&v)));
133
134        match (self.0, cargo_msrv) {
135            (None, Some(cargo_msrv)) => self.0 = Some(cargo_msrv),
136            (Some(clippy_msrv), Some(cargo_msrv)) => {
137                if clippy_msrv != cargo_msrv {
138                    sess.dcx().warn(format!(
139                        "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{clippy_msrv}` from `clippy.toml`"
140                    ));
141                }
142            },
143            _ => {},
144        }
145    }
146}
147
148/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` in early
149/// lint passes, use [`Msrv`] for late passes
150#[derive(Debug, Clone)]
151pub struct MsrvStack {
152    stack: SmallVec<[RustcVersion; 2]>,
153}
154
155impl MsrvStack {
156    pub fn new(initial: Msrv) -> Self {
157        Self {
158            stack: SmallVec::from_iter(initial.0),
159        }
160    }
161
162    pub fn current(&self) -> Option<RustcVersion> {
163        self.stack.last().copied()
164    }
165
166    pub fn meets(&self, required: RustcVersion) -> bool {
167        self.current().is_none_or(|msrv| msrv >= required)
168    }
169
170    pub fn check_attributes(&mut self, sess: &Session, attrs: &[Attribute]) {
171        if let Some(version) = parse_attrs(sess, attrs) {
172            SEEN_MSRV_ATTR.store(true, Ordering::Relaxed);
173            self.stack.push(version);
174        }
175    }
176
177    pub fn check_attributes_post(&mut self, sess: &Session, attrs: &[Attribute]) {
178        if parse_attrs(sess, attrs).is_some() {
179            self.stack.pop();
180        }
181    }
182}
183
184fn parse_attrs(sess: &Session, attrs: &[impl AttributeExt]) -> Option<RustcVersion> {
185    let sym_msrv = Symbol::intern("msrv");
186    let mut msrv_attrs = attrs.iter().filter(|attr| attr.path_matches(&[sym::clippy, sym_msrv]));
187
188    if let Some(msrv_attr) = msrv_attrs.next() {
189        if let Some(duplicate) = msrv_attrs.next_back() {
190            sess.dcx()
191                .struct_span_err(duplicate.span(), "`clippy::msrv` is defined multiple times")
192                .with_span_note(msrv_attr.span(), "first definition found here")
193                .emit();
194        }
195
196        if let Some(msrv) = msrv_attr.value_str() {
197            if let Some(version) = parse_version(msrv) {
198                return Some(version);
199            }
200
201            sess.dcx()
202                .span_err(msrv_attr.span(), format!("`{msrv}` is not a valid Rust version"));
203        } else {
204            sess.dcx().span_err(msrv_attr.span(), "bad clippy attribute");
205        }
206    }
207
208    None
209}