Address some of the code style issues

This commit is contained in:
Niki4tap 2022-12-18 18:58:15 +03:00
parent dae54fad3e
commit b1ca307168
2 changed files with 27 additions and 51 deletions

View File

@ -1,7 +1,6 @@
use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{is_integer_literal, is_path_diagnostic_item};
use if_chain::if_chain;
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -33,19 +32,24 @@ declare_clippy_lint! {
}
declare_lint_pass!(FnNullCheck => [FN_NULL_CHECK]);
const LINT_MSG: &str = "function pointer assumed to be nullable, even though it isn't";
const HELP_MSG: &str = "try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value";
fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
span_lint_and_help(
cx,
FN_NULL_CHECK,
expr.span,
"function pointer assumed to be nullable, even though it isn't",
None,
"try wrapping your function pointer type in `Option<T>` instead, and using `is_none` to check for null pointer value",
)
}
fn is_fn_ptr_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if_chain! {
if let ExprKind::Cast(cast_expr, cast_ty) = expr.kind;
if let TyKind::Ptr(_) = cast_ty.kind;
if cx.typeck_results().expr_ty_adjusted(cast_expr).is_fn();
then {
true
} else {
false
}
if let ExprKind::Cast(cast_expr, cast_ty) = expr.kind
&& let TyKind::Ptr(_) = cast_ty.kind
{
cx.typeck_results().expr_ty_adjusted(cast_expr).is_fn()
} else {
false
}
}
@ -53,20 +57,12 @@ impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// Catching:
// (fn_ptr as *<const/mut> <ty>).is_null()
if_chain! {
if let ExprKind::MethodCall(method_name, receiver, _, _) = expr.kind;
if method_name.ident.as_str() == "is_null";
if is_fn_ptr_cast(cx, receiver);
then {
span_lint_and_help(
cx,
FN_NULL_CHECK,
expr.span,
LINT_MSG,
None,
HELP_MSG
);
}
if let ExprKind::MethodCall(method_name, receiver, _, _) = expr.kind
&& method_name.ident.as_str() == "is_null"
&& is_fn_ptr_cast(cx, receiver)
{
lint_expr(cx, expr);
return;
}
if let ExprKind::Binary(op, left, right) = expr.kind
@ -85,28 +81,14 @@ impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
let c = constant(cx, cx.typeck_results(), to_check);
if let Some((Constant::RawPtr(0), _)) = c {
span_lint_and_help(
cx,
FN_NULL_CHECK,
expr.span,
LINT_MSG,
None,
HELP_MSG
);
lint_expr(cx, expr);
return;
}
// Catching:
// (fn_ptr as *<const/mut> <ty>) == (0 as <ty>)
if let ExprKind::Cast(cast_expr, _) = to_check.kind && is_integer_literal(cast_expr, 0) {
span_lint_and_help(
cx,
FN_NULL_CHECK,
expr.span,
LINT_MSG,
None,
HELP_MSG
);
lint_expr(cx, expr);
return;
}
@ -115,14 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
if let ExprKind::Call(func, []) = to_check.kind &&
is_path_diagnostic_item(cx, func, sym::ptr_null)
{
span_lint_and_help(
cx,
FN_NULL_CHECK,
expr.span,
LINT_MSG,
None,
HELP_MSG
);
lint_expr(cx, expr);
}
}
}

View File

@ -18,7 +18,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
return false;
}
// Catching transmute over constants that resolve to `null`.
// Catching:
// transmute over constants that resolve to `null`.
let mut const_eval_context = constant_context(cx, cx.typeck_results());
if let ExprKind::Path(ref _qpath) = arg.kind &&
let Some(Constant::RawPtr(0)) = const_eval_context.expr(arg)