|  | 
|  | 1 | +use crate::{lints::FnNullCheckDiag, LateContext, LateLintPass, LintContext}; | 
|  | 2 | +use rustc_ast::LitKind; | 
|  | 3 | +use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind}; | 
|  | 4 | +use rustc_session::{declare_lint, declare_lint_pass}; | 
|  | 5 | +use rustc_span::sym; | 
|  | 6 | + | 
|  | 7 | +declare_lint! { | 
|  | 8 | +    /// The `incorrect_fn_null_checks` lint checks for expression that checks if a | 
|  | 9 | +    /// function pointer is null. | 
|  | 10 | +    /// | 
|  | 11 | +    /// ### Example | 
|  | 12 | +    /// | 
|  | 13 | +    /// ```rust | 
|  | 14 | +    /// # fn test() {} | 
|  | 15 | +    /// let fn_ptr: fn() = /* somehow obtained nullable function pointer */ | 
|  | 16 | +    /// #   test; | 
|  | 17 | +    /// | 
|  | 18 | +    /// if (fn_ptr as *const ()).is_null() { /* ... */ } | 
|  | 19 | +    /// ``` | 
|  | 20 | +    /// | 
|  | 21 | +    /// {{produces}} | 
|  | 22 | +    /// | 
|  | 23 | +    /// ### Explanation | 
|  | 24 | +    /// | 
|  | 25 | +    /// Function pointers are assumed to be non-null, checking them for null will always | 
|  | 26 | +    /// return false. | 
|  | 27 | +    INCORRECT_FN_NULL_CHECKS, | 
|  | 28 | +    Warn, | 
|  | 29 | +    "incorrect checking of null function pointer" | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +declare_lint_pass!(IncorrectFnNullChecks => [INCORRECT_FN_NULL_CHECKS]); | 
|  | 33 | + | 
|  | 34 | +fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | 
|  | 35 | +    let mut expr = expr.peel_blocks(); | 
|  | 36 | +    let mut had_at_least_one_cast = false; | 
|  | 37 | +    while let ExprKind::Cast(cast_expr, cast_ty) = expr.kind | 
|  | 38 | +            && let TyKind::Ptr(_) = cast_ty.kind { | 
|  | 39 | +        expr = cast_expr.peel_blocks(); | 
|  | 40 | +        had_at_least_one_cast = true; | 
|  | 41 | +    } | 
|  | 42 | +    had_at_least_one_cast && cx.typeck_results().expr_ty_adjusted(expr).is_fn() | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks { | 
|  | 46 | +    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | 
|  | 47 | +        match expr.kind { | 
|  | 48 | +            // Catching: | 
|  | 49 | +            // <*<const/mut> <ty>>::is_null(fn_ptr as *<const/mut> <ty>) | 
|  | 50 | +            ExprKind::Call(path, [arg]) | 
|  | 51 | +                if let ExprKind::Path(ref qpath) = path.kind | 
|  | 52 | +                    && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | 
|  | 53 | +                    && matches!( | 
|  | 54 | +                        cx.tcx.get_diagnostic_name(def_id), | 
|  | 55 | +                        Some(sym::ptr_const_is_null | sym::ptr_is_null) | 
|  | 56 | +                    ) | 
|  | 57 | +                    && is_fn_ptr_cast(cx, arg) => | 
|  | 58 | +            { | 
|  | 59 | +                cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag) | 
|  | 60 | +            } | 
|  | 61 | + | 
|  | 62 | +            // Catching: | 
|  | 63 | +            // (fn_ptr as *<const/mut> <ty>).is_null() | 
|  | 64 | +            ExprKind::MethodCall(_, receiver, _, _) | 
|  | 65 | +                if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) | 
|  | 66 | +                    && matches!( | 
|  | 67 | +                        cx.tcx.get_diagnostic_name(def_id), | 
|  | 68 | +                        Some(sym::ptr_const_is_null | sym::ptr_is_null) | 
|  | 69 | +                    ) | 
|  | 70 | +                    && is_fn_ptr_cast(cx, receiver) => | 
|  | 71 | +            { | 
|  | 72 | +                cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag) | 
|  | 73 | +            } | 
|  | 74 | + | 
|  | 75 | +            ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => { | 
|  | 76 | +                let to_check: &Expr<'_>; | 
|  | 77 | +                if is_fn_ptr_cast(cx, left) { | 
|  | 78 | +                    to_check = right; | 
|  | 79 | +                } else if is_fn_ptr_cast(cx, right) { | 
|  | 80 | +                    to_check = left; | 
|  | 81 | +                } else { | 
|  | 82 | +                    return; | 
|  | 83 | +                } | 
|  | 84 | + | 
|  | 85 | +                match to_check.kind { | 
|  | 86 | +                    // Catching: | 
|  | 87 | +                    // (fn_ptr as *<const/mut> <ty>) == (0 as <ty>) | 
|  | 88 | +                    ExprKind::Cast(cast_expr, _) | 
|  | 89 | +                        if let ExprKind::Lit(spanned) = cast_expr.kind | 
|  | 90 | +                            && let LitKind::Int(v, _) = spanned.node && v == 0 => | 
|  | 91 | +                    { | 
|  | 92 | +                        cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag) | 
|  | 93 | +                    }, | 
|  | 94 | + | 
|  | 95 | +                    // Catching: | 
|  | 96 | +                    // (fn_ptr as *<const/mut> <ty>) == std::ptr::null() | 
|  | 97 | +                    ExprKind::Call(path, []) | 
|  | 98 | +                        if let ExprKind::Path(ref qpath) = path.kind | 
|  | 99 | +                            && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | 
|  | 100 | +                            && let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id) | 
|  | 101 | +                            && (diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut) => | 
|  | 102 | +                    { | 
|  | 103 | +                        cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag) | 
|  | 104 | +                    }, | 
|  | 105 | + | 
|  | 106 | +                    _ => {}, | 
|  | 107 | +                } | 
|  | 108 | +            } | 
|  | 109 | +            _ => {} | 
|  | 110 | +        } | 
|  | 111 | +    } | 
|  | 112 | +} | 
0 commit comments