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