Skip to content

Commit 42106e0

Browse files
committed
Add lint fn_null_check
1 parent 3cc67d0 commit 42106e0

File tree

6 files changed

+196
-1
lines changed

6 files changed

+196
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,6 +4203,7 @@ Released 2018-09-13
42034203
[`float_cmp_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp_const
42044204
[`float_equality_without_abs`]: https://rust-lang.github.io/rust-clippy/master/index.html#float_equality_without_abs
42054205
[`fn_address_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_address_comparisons
4206+
[`fn_null_check`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_null_check
42064207
[`fn_params_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_params_excessive_bools
42074208
[`fn_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast
42084209
[`fn_to_numeric_cast_any`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_any

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
161161
crate::float_literal::LOSSY_FLOAT_LITERAL_INFO,
162162
crate::floating_point_arithmetic::IMPRECISE_FLOPS_INFO,
163163
crate::floating_point_arithmetic::SUBOPTIMAL_FLOPS_INFO,
164+
crate::fn_null_check::FN_NULL_CHECK_INFO,
164165
crate::format::USELESS_FORMAT_INFO,
165166
crate::format_args::FORMAT_IN_FORMAT_ARGS_INFO,
166167
crate::format_args::TO_STRING_IN_FORMAT_ARGS_INFO,

clippy_lints/src/fn_null_check.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

clippy_lints/src/transmute/transmute_null_to_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_span::symbol::sym;
99
use super::TRANSMUTE_NULL_TO_FN;
1010

1111
const LINT_MSG: &str = "transmuting a known null pointer into a function pointer";
12-
const NOTE_MSG: &str = "this transmute results in a null function pointer";
12+
const NOTE_MSG: &str = "this transmute results in undefined behavior";
1313
const HELP_MSG: &str =
1414
"try wrapping your function pointer type in `Option<T>` instead, and using `None` as a null pointer value";
1515

tests/ui/fn_null_check.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![allow(unused)]
2+
#![warn(clippy::fn_null_check)]
3+
#![allow(clippy::cmp_null)]
4+
#![allow(clippy::ptr_eq)]
5+
#![allow(clippy::zero_ptr)]
6+
7+
pub const ZPTR: *const () = 0 as *const _;
8+
pub const NOT_ZPTR: *const () = 1 as *const _;
9+
10+
fn main() {
11+
let fn_ptr = main;
12+
13+
if (fn_ptr as *mut ()).is_null() {}
14+
if (fn_ptr as *const u8).is_null() {}
15+
if (fn_ptr as *const ()) == std::ptr::null() {}
16+
if (fn_ptr as *const ()) == (0 as *const ()) {}
17+
if (fn_ptr as *const ()) == ZPTR {}
18+
19+
// no lint
20+
if (fn_ptr as *const ()) == NOT_ZPTR {}
21+
}

tests/ui/fn_null_check.stderr

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: function pointer assumed to be nullable, even though it isn't
2+
--> $DIR/fn_null_check.rs:13:8
3+
|
4+
LL | if (fn_ptr as *mut ()).is_null() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
8+
= note: `-D clippy::fn-null-check` implied by `-D warnings`
9+
10+
error: function pointer assumed to be nullable, even though it isn't
11+
--> $DIR/fn_null_check.rs:14:8
12+
|
13+
LL | if (fn_ptr as *const u8).is_null() {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
17+
18+
error: function pointer assumed to be nullable, even though it isn't
19+
--> $DIR/fn_null_check.rs:15:8
20+
|
21+
LL | if (fn_ptr as *const ()) == std::ptr::null() {}
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
25+
26+
error: function pointer assumed to be nullable, even though it isn't
27+
--> $DIR/fn_null_check.rs:16:8
28+
|
29+
LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
33+
34+
error: function pointer assumed to be nullable, even though it isn't
35+
--> $DIR/fn_null_check.rs:17:8
36+
|
37+
LL | if (fn_ptr as *const ()) == ZPTR {}
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= help: try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value
41+
42+
error: aborting due to 5 previous errors
43+

0 commit comments

Comments
 (0)