| 
 | 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 | +}  | 
0 commit comments