| 
 | 1 | +use clippy_utils::diagnostics::span_lint;  | 
 | 2 | +use rustc_hir::intravisit::FnKind;  | 
 | 3 | +use rustc_hir::{Body, ExprKind, FnDecl, FnRetTy, TyKind, UnOp};  | 
 | 4 | +use rustc_hir_analysis::hir_ty_to_ty;  | 
 | 5 | +use rustc_lint::{LateContext, LateLintPass};  | 
 | 6 | +use rustc_session::declare_lint_pass;  | 
 | 7 | + | 
 | 8 | +declare_clippy_lint! {  | 
 | 9 | +    /// ### What it does  | 
 | 10 | +    /// It detects references to uninhabited types, such as `!` and  | 
 | 11 | +    /// warns when those are either dereferenced or returned from a function.  | 
 | 12 | +    ///  | 
 | 13 | +    /// ### Why is this bad?  | 
 | 14 | +    /// Dereferencing a reference to an uninhabited type would create  | 
 | 15 | +    /// an instance of such a type, which cannot exist. This constitutes  | 
 | 16 | +    /// undefined behaviour. Such a reference could have been created  | 
 | 17 | +    /// by `unsafe` code.  | 
 | 18 | +    ///  | 
 | 19 | +    /// ### Example  | 
 | 20 | +    /// The following function can return a reference to an uninhabited type  | 
 | 21 | +    /// (`Infallible`) because it uses `unsafe` code to create it. However,  | 
 | 22 | +    /// the user of such a function could dereference the return value and  | 
 | 23 | +    /// trigger an undefined behaviour from safe code.  | 
 | 24 | +    ///  | 
 | 25 | +    /// ```no_run  | 
 | 26 | +    /// fn create_ref() -> &'static std::convert::Infallible {  | 
 | 27 | +    ///     unsafe { std::mem::transmute(&()) }  | 
 | 28 | +    /// }  | 
 | 29 | +    /// ```  | 
 | 30 | +    #[clippy::version = "1.76.0"]  | 
 | 31 | +    pub UNINHABITED_REFERENCE,  | 
 | 32 | +    suspicious,  | 
 | 33 | +    "reference to uninhabited type"  | 
 | 34 | +}  | 
 | 35 | + | 
 | 36 | +declare_lint_pass!(UninhabitedReference => [UNINHABITED_REFERENCE]);  | 
 | 37 | + | 
 | 38 | +impl LateLintPass<'_> for UninhabitedReference {  | 
 | 39 | +    fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {  | 
 | 40 | +        if expr.span.from_expansion() {  | 
 | 41 | +            return;  | 
 | 42 | +        }  | 
 | 43 | + | 
 | 44 | +        if let ExprKind::Unary(UnOp::Deref, _) = expr.kind {  | 
 | 45 | +            let ty = cx.typeck_results().expr_ty_adjusted(expr);  | 
 | 46 | +            if ty.is_privately_uninhabited(cx.tcx, cx.param_env) {  | 
 | 47 | +                span_lint(  | 
 | 48 | +                    cx,  | 
 | 49 | +                    UNINHABITED_REFERENCE,  | 
 | 50 | +                    expr.span,  | 
 | 51 | +                    "dereferencing a reference to an uninhabited type is undefined behavior",  | 
 | 52 | +                );  | 
 | 53 | +            }  | 
 | 54 | +        }  | 
 | 55 | +    }  | 
 | 56 | + | 
 | 57 | +    fn check_fn(  | 
 | 58 | +        &mut self,  | 
 | 59 | +        cx: &LateContext<'_>,  | 
 | 60 | +        kind: FnKind<'_>,  | 
 | 61 | +        fndecl: &'_ FnDecl<'_>,  | 
 | 62 | +        _: &'_ Body<'_>,  | 
 | 63 | +        span: rustc_span::Span,  | 
 | 64 | +        _: rustc_span::def_id::LocalDefId,  | 
 | 65 | +    ) {  | 
 | 66 | +        if span.from_expansion() || matches!(kind, FnKind::Closure) {  | 
 | 67 | +            return;  | 
 | 68 | +        }  | 
 | 69 | +        if let FnRetTy::Return(hir_ty) = fndecl.output  | 
 | 70 | +            && let TyKind::Ref(_, mut_ty) = hir_ty.kind  | 
 | 71 | +            && hir_ty_to_ty(cx.tcx, mut_ty.ty).is_privately_uninhabited(cx.tcx, cx.param_env)  | 
 | 72 | +        {  | 
 | 73 | +            span_lint(  | 
 | 74 | +                cx,  | 
 | 75 | +                UNINHABITED_REFERENCE,  | 
 | 76 | +                hir_ty.span,  | 
 | 77 | +                "dereferencing a reference to an uninhabited type would be undefined behavior",  | 
 | 78 | +            );  | 
 | 79 | +        }  | 
 | 80 | +    }  | 
 | 81 | +}  | 
0 commit comments