Expand incorrect_fn_null_check lint with reference null checking
This commit is contained in:
parent
c435af0d5c
commit
743ae5a2eb
@ -213,9 +213,12 @@ lint_expectation = this lint expectation is unfulfilled
|
||||
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
|
||||
.rationale = {$rationale}
|
||||
|
||||
lint_fn_null_check = function pointers are not nullable, so checking them for null will always return false
|
||||
lint_fn_null_check_fn_ptr = function pointers are not nullable, so checking them for null will always return false
|
||||
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
lint_fn_null_check_ref = references are not nullable, so checking them for null will always return false
|
||||
.label = expression has type `{$orig_ty}`
|
||||
|
||||
lint_for_loops_over_fallibles =
|
||||
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
|
||||
.suggestion = consider using `if let` to clear intent
|
||||
|
@ -31,7 +31,7 @@ declare_lint! {
|
||||
|
||||
declare_lint_pass!(IncorrectFnNullChecks => [INCORRECT_FN_NULL_CHECKS]);
|
||||
|
||||
fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<FnNullCheckDiag<'a>> {
|
||||
let mut expr = expr.peel_blocks();
|
||||
let mut had_at_least_one_cast = false;
|
||||
while let ExprKind::Cast(cast_expr, cast_ty) = expr.kind
|
||||
@ -39,7 +39,18 @@ fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
expr = cast_expr.peel_blocks();
|
||||
had_at_least_one_cast = true;
|
||||
}
|
||||
had_at_least_one_cast && cx.typeck_results().expr_ty_adjusted(expr).is_fn()
|
||||
if !had_at_least_one_cast {
|
||||
None
|
||||
} else {
|
||||
let orig_ty = cx.typeck_results().expr_ty(expr);
|
||||
if orig_ty.is_fn() {
|
||||
Some(FnNullCheckDiag::FnPtr)
|
||||
} else if orig_ty.is_ref() {
|
||||
Some(FnNullCheckDiag::Ref { orig_ty, label: expr.span })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks {
|
||||
@ -54,9 +65,9 @@ impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks {
|
||||
cx.tcx.get_diagnostic_name(def_id),
|
||||
Some(sym::ptr_const_is_null | sym::ptr_is_null)
|
||||
)
|
||||
&& is_fn_ptr_cast(cx, arg) =>
|
||||
&& let Some(diag) = incorrect_check(cx, arg) =>
|
||||
{
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag)
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, diag)
|
||||
}
|
||||
|
||||
// Catching:
|
||||
@ -67,17 +78,20 @@ impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks {
|
||||
cx.tcx.get_diagnostic_name(def_id),
|
||||
Some(sym::ptr_const_is_null | sym::ptr_is_null)
|
||||
)
|
||||
&& is_fn_ptr_cast(cx, receiver) =>
|
||||
&& let Some(diag) = incorrect_check(cx, receiver) =>
|
||||
{
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag)
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, diag)
|
||||
}
|
||||
|
||||
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => {
|
||||
let to_check: &Expr<'_>;
|
||||
if is_fn_ptr_cast(cx, left) {
|
||||
let diag: FnNullCheckDiag<'_>;
|
||||
if let Some(ddiag) = incorrect_check(cx, left) {
|
||||
to_check = right;
|
||||
} else if is_fn_ptr_cast(cx, right) {
|
||||
diag = ddiag;
|
||||
} else if let Some(ddiag) = incorrect_check(cx, right) {
|
||||
to_check = left;
|
||||
diag = ddiag;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -89,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks {
|
||||
if let ExprKind::Lit(spanned) = cast_expr.kind
|
||||
&& let LitKind::Int(v, _) = spanned.node && v == 0 =>
|
||||
{
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag)
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, diag)
|
||||
},
|
||||
|
||||
// Catching:
|
||||
@ -100,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for IncorrectFnNullChecks {
|
||||
&& let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id)
|
||||
&& (diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut) =>
|
||||
{
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, FnNullCheckDiag)
|
||||
cx.emit_spanned_lint(INCORRECT_FN_NULL_CHECKS, expr.span, diag)
|
||||
},
|
||||
|
||||
_ => {},
|
||||
|
@ -615,9 +615,17 @@ pub struct ExpectationNote {
|
||||
|
||||
// fn_null_check.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_fn_null_check)]
|
||||
#[help]
|
||||
pub struct FnNullCheckDiag;
|
||||
pub enum FnNullCheckDiag<'a> {
|
||||
#[diag(lint_fn_null_check_fn_ptr)]
|
||||
#[help(lint_help)]
|
||||
FnPtr,
|
||||
#[diag(lint_fn_null_check_ref)]
|
||||
Ref {
|
||||
orig_ty: Ty<'a>,
|
||||
#[label]
|
||||
label: Span,
|
||||
},
|
||||
}
|
||||
|
||||
// for_loops_over_fallibles.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
|
@ -3,6 +3,7 @@
|
||||
fn main() {
|
||||
let fn_ptr = main;
|
||||
|
||||
// ------------- Function pointers ---------------
|
||||
if (fn_ptr as *mut ()).is_null() {}
|
||||
//~^ WARN function pointers are not nullable
|
||||
if (fn_ptr as *const u8).is_null() {}
|
||||
@ -20,6 +21,26 @@ fn main() {
|
||||
if (fn_ptr as fn() as *const ()).is_null() {}
|
||||
//~^ WARN function pointers are not nullable
|
||||
|
||||
// ---------------- References ------------------
|
||||
if (&mut 8 as *mut i32).is_null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if (&8 as *const i32).is_null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if (&8 as *const i32) == std::ptr::null() {}
|
||||
//~^ WARN references are not nullable
|
||||
let ref_num = &8;
|
||||
if (ref_num as *const i32) == std::ptr::null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if (b"\0" as *const u8).is_null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if ("aa" as *const str).is_null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if (&[1, 2] as *const i32).is_null() {}
|
||||
//~^ WARN references are not nullable
|
||||
if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
|
||||
//~^ WARN references are not nullable
|
||||
|
||||
// ----------------------------------------------
|
||||
const ZPTR: *const () = 0 as *const _;
|
||||
const NOT_ZPTR: *const () = 1 as *const _;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:6:8
|
||||
--> $DIR/fn_null_check.rs:7:8
|
||||
|
|
||||
LL | if (fn_ptr as *mut ()).is_null() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -8,7 +8,7 @@ LL | if (fn_ptr as *mut ()).is_null() {}
|
||||
= note: `#[warn(incorrect_fn_null_checks)]` on by default
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:8:8
|
||||
--> $DIR/fn_null_check.rs:9:8
|
||||
|
|
||||
LL | if (fn_ptr as *const u8).is_null() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -16,7 +16,7 @@ LL | if (fn_ptr as *const u8).is_null() {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:10:8
|
||||
--> $DIR/fn_null_check.rs:11:8
|
||||
|
|
||||
LL | if (fn_ptr as *const ()) == std::ptr::null() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -24,7 +24,7 @@ LL | if (fn_ptr as *const ()) == std::ptr::null() {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:12:8
|
||||
--> $DIR/fn_null_check.rs:13:8
|
||||
|
|
||||
LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -32,7 +32,7 @@ LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:14:8
|
||||
--> $DIR/fn_null_check.rs:15:8
|
||||
|
|
||||
LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -40,7 +40,7 @@ LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:16:8
|
||||
--> $DIR/fn_null_check.rs:17:8
|
||||
|
|
||||
LL | if <*const _>::is_null(fn_ptr as *const ()) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -48,7 +48,7 @@ LL | if <*const _>::is_null(fn_ptr as *const ()) {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:18:8
|
||||
--> $DIR/fn_null_check.rs:19:8
|
||||
|
|
||||
LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -56,12 +56,76 @@ LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: function pointers are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:20:8
|
||||
--> $DIR/fn_null_check.rs:21:8
|
||||
|
|
||||
LL | if (fn_ptr as fn() as *const ()).is_null() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
|
||||
|
||||
warning: 8 warnings emitted
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:25:8
|
||||
|
|
||||
LL | if (&mut 8 as *mut i32).is_null() {}
|
||||
| ^------^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&mut i32`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:27:8
|
||||
|
|
||||
LL | if (&8 as *const i32).is_null() {}
|
||||
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&i32`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:29:8
|
||||
|
|
||||
LL | if (&8 as *const i32) == std::ptr::null() {}
|
||||
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&i32`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:32:8
|
||||
|
|
||||
LL | if (ref_num as *const i32) == std::ptr::null() {}
|
||||
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&i32`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:34:8
|
||||
|
|
||||
LL | if (b"\0" as *const u8).is_null() {}
|
||||
| ^-----^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&[u8; 1]`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:36:8
|
||||
|
|
||||
LL | if ("aa" as *const str).is_null() {}
|
||||
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&str`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:38:8
|
||||
|
|
||||
LL | if (&[1, 2] as *const i32).is_null() {}
|
||||
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&[i32; 2]`
|
||||
|
||||
warning: references are not nullable, so checking them for null will always return false
|
||||
--> $DIR/fn_null_check.rs:40:8
|
||||
|
|
||||
LL | if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
|
||||
| ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| expression has type `&mut [i32; 2]`
|
||||
|
||||
warning: 16 warnings emitted
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user