|
| 1 | +use clippy_utils::consts::{constant, Constant}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 3 | +use clippy_utils::{is_integer_literal, is_path_diagnostic_item}; |
| 4 | +use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for comparing a function pointer to null. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// Function pointers are assumed to not be null. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// ```rust,ignore |
| 18 | + /// let fn_ptr: fn() = /* somehow obtained nullable function pointer */ |
| 19 | + /// |
| 20 | + /// if (fn_ptr as *const ()).is_null() { ... } |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust,ignore |
| 24 | + /// let fn_ptr: Option<fn()> = /* somehow obtained nullable function pointer */ |
| 25 | + /// |
| 26 | + /// if fn_ptr.is_none() { ... } |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.67.0"] |
| 29 | + pub FN_NULL_CHECK, |
| 30 | + correctness, |
| 31 | + "`fn()` type assumed to be nullable" |
| 32 | +} |
| 33 | +declare_lint_pass!(FnNullCheck => [FN_NULL_CHECK]); |
| 34 | + |
| 35 | +fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 36 | + span_lint_and_help( |
| 37 | + cx, |
| 38 | + FN_NULL_CHECK, |
| 39 | + expr.span, |
| 40 | + "function pointer assumed to be nullable, even though it isn't", |
| 41 | + None, |
| 42 | + "try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value", |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 47 | + if let ExprKind::Cast(cast_expr, cast_ty) = expr.kind |
| 48 | + && let TyKind::Ptr(_) = cast_ty.kind |
| 49 | + { |
| 50 | + cx.typeck_results().expr_ty_adjusted(cast_expr).is_fn() |
| 51 | + } else { |
| 52 | + false |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<'tcx> LateLintPass<'tcx> for FnNullCheck { |
| 57 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 58 | + match expr.kind { |
| 59 | + // Catching: |
| 60 | + // (fn_ptr as *<const/mut> <ty>).is_null() |
| 61 | + ExprKind::MethodCall(method_name, receiver, _, _) |
| 62 | + if method_name.ident.as_str() == "is_null" && is_fn_ptr_cast(cx, receiver) => |
| 63 | + { |
| 64 | + lint_expr(cx, expr); |
| 65 | + }, |
| 66 | + |
| 67 | + ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => { |
| 68 | + let to_check: &Expr<'_>; |
| 69 | + if is_fn_ptr_cast(cx, left) { |
| 70 | + to_check = right; |
| 71 | + } else if is_fn_ptr_cast(cx, right) { |
| 72 | + to_check = left; |
| 73 | + } else { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + match to_check.kind { |
| 78 | + // Catching: |
| 79 | + // (fn_ptr as *<const/mut> <ty>) == (0 as <ty>) |
| 80 | + ExprKind::Cast(cast_expr, _) if is_integer_literal(cast_expr, 0) => { |
| 81 | + lint_expr(cx, expr); |
| 82 | + }, |
| 83 | + |
| 84 | + // Catching: |
| 85 | + // (fn_ptr as *<const/mut> <ty>) == std::ptr::null() |
| 86 | + ExprKind::Call(func, []) if is_path_diagnostic_item(cx, func, sym::ptr_null) => { |
| 87 | + lint_expr(cx, expr); |
| 88 | + }, |
| 89 | + |
| 90 | + // Catching: |
| 91 | + // (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr> |
| 92 | + _ if matches!( |
| 93 | + constant(cx, cx.typeck_results(), to_check), |
| 94 | + Some((Constant::RawPtr(0), _)) |
| 95 | + ) => |
| 96 | + { |
| 97 | + lint_expr(cx, expr); |
| 98 | + }, |
| 99 | + |
| 100 | + _ => {}, |
| 101 | + } |
| 102 | + }, |
| 103 | + _ => {}, |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments