about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiki4tap <rombiklol2@gmail.com>2022-12-18 18:58:15 +0300
committerNiki4tap <rombiklol2@gmail.com>2022-12-18 18:58:15 +0300
commitb1ca307168b622d66a7f0d66421933236bb8cbcf (patch)
tree0cc847f5d381a74e7889357e6da913a1e949c367
parentdae54fad3ee4b51ed286d4686f6d064b48b56cca (diff)
downloadrust-b1ca307168b622d66a7f0d66421933236bb8cbcf.tar.gz
rust-b1ca307168b622d66a7f0d66421933236bb8cbcf.zip
Address some of the code style issues
-rw-r--r--clippy_lints/src/fn_null_check.rs75
-rw-r--r--clippy_lints/src/transmute/transmute_null_to_fn.rs3
2 files changed, 27 insertions, 51 deletions
diff --git a/clippy_lints/src/fn_null_check.rs b/clippy_lints/src/fn_null_check.rs
index 89841936954..f4b7a55fa74 100644
--- a/clippy_lints/src/fn_null_check.rs
+++ b/clippy_lints/src/fn_null_check.rs
@@ -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);
             }
         }
     }
diff --git a/clippy_lints/src/transmute/transmute_null_to_fn.rs b/clippy_lints/src/transmute/transmute_null_to_fn.rs
index 2286e17fd16..074a5d31763 100644
--- a/clippy_lints/src/transmute/transmute_null_to_fn.rs
+++ b/clippy_lints/src/transmute/transmute_null_to_fn.rs
@@ -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)