1 use crate::{context::LintContext, LateContext, LateLintPass};
3 use rustc_middle::ty::{fold::TypeFoldable, Ty};
4 use rustc_span::{symbol::sym, Span};
7 /// The `enum_intrinsics_non_enums` lint detects calls to
8 /// intrinsic functions that require an enum ([`core::mem::discriminant`],
9 /// [`core::mem::variant_count`]), but are called with a non-enum type.
11 /// [`core::mem::discriminant`]: https://doc.rust-lang.org/core/mem/fn.discriminant.html
12 /// [`core::mem::variant_count`]: https://doc.rust-lang.org/core/mem/fn.variant_count.html
16 /// ```rust,compile_fail
17 /// #![deny(enum_intrinsics_non_enums)]
18 /// core::mem::discriminant::<i32>(&123);
25 /// In order to accept any enum, the `mem::discriminant` and
26 /// `mem::variant_count` functions are generic over a type `T`.
27 /// This makes it technically possible for `T` to be a non-enum,
28 /// in which case the return value is unspecified.
30 /// This lint prevents such incorrect usage of these functions.
31 ENUM_INTRINSICS_NON_ENUMS,
33 "detects calls to `core::mem::discriminant` and `core::mem::variant_count` with non-enum types"
36 declare_lint_pass!(EnumIntrinsicsNonEnums => [ENUM_INTRINSICS_NON_ENUMS]);
38 /// Returns `true` if we know for sure that the given type is not an enum. Note that for cases where
39 /// the type is generic, we can't be certain if it will be an enum so we have to assume that it is.
40 fn is_non_enum(t: Ty<'_>) -> bool {
41 !t.is_enum() && !t.potentially_needs_subst()
44 fn enforce_mem_discriminant(
46 func_expr: &hir::Expr<'_>,
50 let ty_param = cx.typeck_results().node_substs(func_expr.hir_id).type_at(0);
51 if is_non_enum(ty_param) {
52 cx.struct_span_lint(ENUM_INTRINSICS_NON_ENUMS, expr_span, |builder| {
55 "the return value of `mem::discriminant` is \
56 unspecified when called with a non-enum type",
61 "the argument to `discriminant` should be a \
62 reference to an enum, but it was passed \
63 a reference to a `{}`, which is not an enum.",
72 fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, span: Span) {
73 let ty_param = cx.typeck_results().node_substs(func_expr.hir_id).type_at(0);
74 if is_non_enum(ty_param) {
75 cx.struct_span_lint(ENUM_INTRINSICS_NON_ENUMS, span, |builder| {
78 "the return value of `mem::variant_count` is \
79 unspecified when called with a non-enum type",
82 "the type parameter of `variant_count` should \
83 be an enum, but it was instantiated with \
84 the type `{}`, which is not an enum.",
92 impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums {
93 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
94 let hir::ExprKind::Call(func, args) = &expr.kind else { return };
95 let hir::ExprKind::Path(qpath) = &func.kind else { return };
96 let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() else { return };
97 let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return };
99 sym::mem_discriminant => enforce_mem_discriminant(cx, func, expr.span, args[0].span),
100 sym::mem_variant_count => enforce_mem_variant_count(cx, func, expr.span),