Skip to content

Commit 63ef74b

Browse files
committed
Auto merge of rust-lang#111717 - Urgau:uplift_fn_null_check, r=oli-obk
Uplift `clippy::fn_null_check` lint This PR aims at uplifting the `clippy::fn_null_check` lint into rustc. ## `incorrect_fn_null_checks` (warn-by-default) The `incorrect_fn_null_checks` lint checks for expression that checks if a function pointer is null. ### Example ```rust let fn_ptr: fn() = /* somehow obtained nullable function pointer */ if (fn_ptr as *const ()).is_null() { /* ... */ } ``` ### Explanation Function pointers are assumed to be non-null, checking for their nullity is incorrect. ----- Mostly followed the instructions for uplifting a clippy lint described here: rust-lang#99696 (review) `@rustbot` label: +I-lang-nominated r? compiler
2 parents 5b733e2 + c0fbeea commit 63ef74b

18 files changed

+289
-223
lines changed

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ lint_expectation = this lint expectation is unfulfilled
215215
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
216216
.rationale = {$rationale}
217217
218+
lint_fn_null_check = function pointers are not nullable, so checking them for null will always return false
219+
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
220+
218221
lint_for_loops_over_fallibles =
219222
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
220223
.suggestion = consider using `if let` to clear intent
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ mod early;
5858
mod enum_intrinsics_non_enums;
5959
mod errors;
6060
mod expect;
61+
mod fn_null_check;
6162
mod for_loops_over_fallibles;
6263
pub mod hidden_unicode_codepoints;
6364
mod internal;
@@ -102,6 +103,7 @@ use cast_ref_to_mut::*;
102103
use deref_into_dyn_supertrait::*;
103104
use drop_forget_useless::*;
104105
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
106+
use fn_null_check::*;
105107
use for_loops_over_fallibles::*;
106108
use hidden_unicode_codepoints::*;
107109
use internal::*;
@@ -225,6 +227,7 @@ late_lint_methods!(
225227
// Depends on types used in type definitions
226228
MissingCopyImplementations: MissingCopyImplementations,
227229
// Depends on referenced function signatures in expressions
230+
IncorrectFnNullChecks: IncorrectFnNullChecks,
228231
MutableTransmutes: MutableTransmutes,
229232
TypeAliasBounds: TypeAliasBounds,
230233
TrivialConstraints: TrivialConstraints,

compiler/rustc_lint/src/lints.rs

+6
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ pub struct ExpectationNote {
613613
pub rationale: Symbol,
614614
}
615615

616+
// fn_null_check.rs
617+
#[derive(LintDiagnostic)]
618+
#[diag(lint_fn_null_check)]
619+
#[help]
620+
pub struct FnNullCheckDiag;
621+
616622
// for_loops_over_fallibles.rs
617623
#[derive(LintDiagnostic)]
618624
#[diag(lint_for_loops_over_fallibles)]

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,10 @@ symbols! {
11521152
profiler_runtime,
11531153
ptr,
11541154
ptr_cast_mut,
1155+
ptr_const_is_null,
11551156
ptr_from_ref,
11561157
ptr_guaranteed_cmp,
1158+
ptr_is_null,
11571159
ptr_mask,
11581160
ptr_null,
11591161
ptr_null_mut,

library/core/src/ptr/const_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl<T: ?Sized> *const T {
3030
/// ```
3131
#[stable(feature = "rust1", since = "1.0.0")]
3232
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
33+
#[rustc_diagnostic_item = "ptr_const_is_null"]
3334
#[inline]
3435
pub const fn is_null(self) -> bool {
3536
#[inline]

library/core/src/ptr/mut_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl<T: ?Sized> *mut T {
2929
/// ```
3030
#[stable(feature = "rust1", since = "1.0.0")]
3131
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
32+
#[rustc_diagnostic_item = "ptr_is_null"]
3233
#[inline]
3334
pub const fn is_null(self) -> bool {
3435
#[inline]

src/tools/clippy/clippy_lints/src/declared_lints.rs

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
171171
crate::float_literal::LOSSY_FLOAT_LITERAL_INFO,
172172
crate::floating_point_arithmetic::IMPRECISE_FLOPS_INFO,
173173
crate::floating_point_arithmetic::SUBOPTIMAL_FLOPS_INFO,
174-
crate::fn_null_check::FN_NULL_CHECK_INFO,
175174
crate::format::USELESS_FORMAT_INFO,
176175
crate::format_args::FORMAT_IN_FORMAT_ARGS_INFO,
177176
crate::format_args::TO_STRING_IN_FORMAT_ARGS_INFO,

src/tools/clippy/clippy_lints/src/fn_null_check.rs

-102
This file was deleted.

src/tools/clippy/clippy_lints/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ mod extra_unused_type_parameters;
131131
mod fallible_impl_from;
132132
mod float_literal;
133133
mod floating_point_arithmetic;
134-
mod fn_null_check;
135134
mod format;
136135
mod format_args;
137136
mod format_impl;
@@ -1003,7 +1002,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10031002
semicolon_outside_block_ignore_multiline,
10041003
))
10051004
});
1006-
store.register_late_pass(|_| Box::new(fn_null_check::FnNullCheck));
10071005
store.register_late_pass(|_| Box::new(permissions_set_readonly_false::PermissionsSetReadonlyFalse));
10081006
store.register_late_pass(|_| Box::new(size_of_ref::SizeOfRef));
10091007
store.register_late_pass(|_| Box::new(multiple_unsafe_ops_per_block::MultipleUnsafeOpsPerBlock));

src/tools/clippy/clippy_lints/src/renamed_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
4242
("clippy::for_loops_over_fallibles", "for_loops_over_fallibles"),
4343
("clippy::forget_copy", "forgetting_copy_types"),
4444
("clippy::forget_ref", "forgetting_references"),
45+
("clippy::fn_null_check", "incorrect_fn_null_checks"),
4546
("clippy::into_iter_on_array", "array_into_iter"),
4647
("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"),
4748
("clippy::invalid_ref", "invalid_value"),

src/tools/clippy/tests/ui/fn_null_check.rs

-22
This file was deleted.

src/tools/clippy/tests/ui/fn_null_check.stderr

-43
This file was deleted.

0 commit comments

Comments
 (0)