|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_macros::{LintDiagnostic, Subdiagnostic}; |
| 3 | +use rustc_middle::ty::{self, Ty}; |
| 4 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 5 | +use rustc_span::{BytePos, Span}; |
| 6 | + |
| 7 | +use crate::{LateContext, LateLintPass}; |
| 8 | + |
| 9 | +declare_lint! { |
| 10 | + /// The lint detects cases where users cast a function into an integer. |
| 11 | + /// |
| 12 | + /// ### Example |
| 13 | + /// |
| 14 | + /// ```rust |
| 15 | + /// fn foo() {} |
| 16 | + /// let x = foo as usize; |
| 17 | + /// ``` |
| 18 | + /// |
| 19 | + /// {{produces}} |
| 20 | + /// |
| 21 | + /// ### Explanation |
| 22 | + /// |
| 23 | + /// You should never cast a function directly into an integer but go through |
| 24 | + /// a cast as `fn` first to make it obvious what's going on. It also allows |
| 25 | + /// to prevent confusion with (associated) constants. |
| 26 | + pub FUNCTION_CASTS_AS_INTEGER, |
| 27 | + Warn, |
| 28 | + "Casting a function into an integer", |
| 29 | +} |
| 30 | + |
| 31 | +declare_lint_pass!( |
| 32 | + /// Lint for casts of functions into integers. |
| 33 | + FunctionCastsAsInteger => [FUNCTION_CASTS_AS_INTEGER] |
| 34 | +); |
| 35 | + |
| 36 | +impl<'tcx> LateLintPass<'tcx> for FunctionCastsAsInteger { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { |
| 38 | + let hir::ExprKind::Cast(cast_from_expr, cast_to_expr) = expr.kind else { return }; |
| 39 | + let cast_to_ty = cx.typeck_results().expr_ty(expr); |
| 40 | + // Casting to a function (pointer?), so all good. |
| 41 | + if matches!(cast_to_ty.kind(), ty::FnDef(..) | ty::FnPtr(..)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + let cast_from_ty = cx.typeck_results().expr_ty(cast_from_expr); |
| 45 | + if matches!(cast_from_ty.kind(), ty::FnDef(..)) { |
| 46 | + cx.tcx.emit_node_span_lint( |
| 47 | + FUNCTION_CASTS_AS_INTEGER, |
| 48 | + expr.hir_id, |
| 49 | + cast_to_expr.span.with_lo(cast_from_expr.span.hi() + BytePos(1)), |
| 50 | + FunctionCastsAsIntegerMsg { |
| 51 | + sugg: FunctionCastsAsIntegerSugg { |
| 52 | + suggestion: cast_from_expr.span.shrink_to_hi(), |
| 53 | + // We get the function pointer to have a nice display. |
| 54 | + cast_from_ty: cx.typeck_results().expr_ty_adjusted(cast_from_expr), |
| 55 | + cast_to_ty, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(LintDiagnostic)] |
| 64 | +#[diag(lint_function_casts_as_integer)] |
| 65 | +struct FunctionCastsAsIntegerMsg<'tcx> { |
| 66 | + #[subdiagnostic] |
| 67 | + sugg: FunctionCastsAsIntegerSugg<'tcx>, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Subdiagnostic)] |
| 71 | +#[suggestion( |
| 72 | + lint_cast_as_fn, |
| 73 | + code = " as {cast_from_ty}", |
| 74 | + applicability = "machine-applicable", |
| 75 | + style = "verbose" |
| 76 | +)] |
| 77 | +struct FunctionCastsAsIntegerSugg<'tcx> { |
| 78 | + #[primary_span] |
| 79 | + pub suggestion: Span, |
| 80 | + pub cast_from_ty: Ty<'tcx>, |
| 81 | + pub cast_to_ty: Ty<'tcx>, |
| 82 | +} |
0 commit comments