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