]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/enum_intrinsics_non_enums.rs
Rollup merge of #107656 - jonhoo:bump-rust-installer, r=Mark-Simulacrum
[rust.git] / compiler / rustc_lint / src / enum_intrinsics_non_enums.rs
1 use crate::{
2     context::LintContext,
3     lints::{EnumIntrinsicsMemDiscriminate, EnumIntrinsicsMemVariant},
4     LateContext, LateLintPass,
5 };
6 use rustc_hir as hir;
7 use rustc_middle::ty::{visit::TypeVisitable, Ty};
8 use rustc_span::{symbol::sym, Span};
9
10 declare_lint! {
11     /// The `enum_intrinsics_non_enums` lint detects calls to
12     /// intrinsic functions that require an enum ([`core::mem::discriminant`],
13     /// [`core::mem::variant_count`]), but are called with a non-enum type.
14     ///
15     /// [`core::mem::discriminant`]: https://doc.rust-lang.org/core/mem/fn.discriminant.html
16     /// [`core::mem::variant_count`]: https://doc.rust-lang.org/core/mem/fn.variant_count.html
17     ///
18     /// ### Example
19     ///
20     /// ```rust,compile_fail
21     /// #![deny(enum_intrinsics_non_enums)]
22     /// core::mem::discriminant::<i32>(&123);
23     /// ```
24     ///
25     /// {{produces}}
26     ///
27     /// ### Explanation
28     ///
29     /// In order to accept any enum, the `mem::discriminant` and
30     /// `mem::variant_count` functions are generic over a type `T`.
31     /// This makes it technically possible for `T` to be a non-enum,
32     /// in which case the return value is unspecified.
33     ///
34     /// This lint prevents such incorrect usage of these functions.
35     ENUM_INTRINSICS_NON_ENUMS,
36     Deny,
37     "detects calls to `core::mem::discriminant` and `core::mem::variant_count` with non-enum types"
38 }
39
40 declare_lint_pass!(EnumIntrinsicsNonEnums => [ENUM_INTRINSICS_NON_ENUMS]);
41
42 /// Returns `true` if we know for sure that the given type is not an enum. Note that for cases where
43 /// the type is generic, we can't be certain if it will be an enum so we have to assume that it is.
44 fn is_non_enum(t: Ty<'_>) -> bool {
45     !t.is_enum() && !t.needs_subst()
46 }
47
48 fn enforce_mem_discriminant(
49     cx: &LateContext<'_>,
50     func_expr: &hir::Expr<'_>,
51     expr_span: Span,
52     args_span: Span,
53 ) {
54     let ty_param = cx.typeck_results().node_substs(func_expr.hir_id).type_at(0);
55     if is_non_enum(ty_param) {
56         cx.emit_spanned_lint(
57             ENUM_INTRINSICS_NON_ENUMS,
58             expr_span,
59             EnumIntrinsicsMemDiscriminate { ty_param, note: args_span },
60         );
61     }
62 }
63
64 fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, span: Span) {
65     let ty_param = cx.typeck_results().node_substs(func_expr.hir_id).type_at(0);
66     if is_non_enum(ty_param) {
67         cx.emit_spanned_lint(
68             ENUM_INTRINSICS_NON_ENUMS,
69             span,
70             EnumIntrinsicsMemVariant { ty_param },
71         );
72     }
73 }
74
75 impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums {
76     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
77         let hir::ExprKind::Call(func, args) = &expr.kind else { return };
78         let hir::ExprKind::Path(qpath) = &func.kind else { return };
79         let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() else { return };
80         let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return };
81         match name {
82             sym::mem_discriminant => enforce_mem_discriminant(cx, func, expr.span, args[0].span),
83             sym::mem_variant_count => enforce_mem_variant_count(cx, func, expr.span),
84             _ => {}
85         }
86     }
87 }