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