From 258bee6703286cc34f77c22f698d01ef850d8b46 Mon Sep 17 00:00:00 2001
From: Urgau <urgau@numericable.fr>
Date: Tue, 12 Dec 2023 19:31:17 +0100
Subject: [PATCH] Reorganize and rename some functions

---
 compiler/rustc_lint/src/reference_casting.rs | 60 +++++++++++---------
 1 file changed, 32 insertions(+), 28 deletions(-)

diff --git a/compiler/rustc_lint/src/reference_casting.rs b/compiler/rustc_lint/src/reference_casting.rs
index 4c40abc8ad890..aa315ccc5ee1f 100644
--- a/compiler/rustc_lint/src/reference_casting.rs
+++ b/compiler/rustc_lint/src/reference_casting.rs
@@ -37,35 +37,39 @@ declare_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]);
 
 impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
-        let Some((is_assignment, e)) = is_operation_we_care_about(cx, expr) else {
-            return;
-        };
-
-        let init = cx.expr_or_init(e);
-
-        let Some(ty_has_interior_mutability) = is_cast_from_const_to_mut(cx, init) else {
-            return;
-        };
-        let orig_cast = if init.span != e.span { Some(init.span) } else { None };
-        let ty_has_interior_mutability = ty_has_interior_mutability.then_some(());
-
-        cx.emit_spanned_lint(
-            INVALID_REFERENCE_CASTING,
-            expr.span,
-            if is_assignment {
-                InvalidReferenceCastingDiag::AssignToRef { orig_cast, ty_has_interior_mutability }
-            } else {
-                InvalidReferenceCastingDiag::BorrowAsMut { orig_cast, ty_has_interior_mutability }
-            },
-        );
+        if let Some((e, is_assignment)) = deref_or_borrow(cx, expr) {
+            let init = cx.expr_or_init(e);
+
+            let Some(ty_has_interior_mutability) = is_cast_from_ref_to_mut_ptr(cx, init) else {
+                return;
+            };
+            let orig_cast = if init.span != e.span { Some(init.span) } else { None };
+            let ty_has_interior_mutability = ty_has_interior_mutability.then_some(());
+
+            cx.emit_spanned_lint(
+                INVALID_REFERENCE_CASTING,
+                expr.span,
+                if is_assignment {
+                    InvalidReferenceCastingDiag::AssignToRef {
+                        orig_cast,
+                        ty_has_interior_mutability,
+                    }
+                } else {
+                    InvalidReferenceCastingDiag::BorrowAsMut {
+                        orig_cast,
+                        ty_has_interior_mutability,
+                    }
+                },
+            );
+        }
     }
 }
 
-fn is_operation_we_care_about<'tcx>(
+fn deref_or_borrow<'tcx>(
     cx: &LateContext<'tcx>,
     e: &'tcx Expr<'tcx>,
-) -> Option<(bool, &'tcx Expr<'tcx>)> {
-    fn deref_assign_or_addr_of<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<(bool, &'tcx Expr<'tcx>)> {
+) -> Option<(&'tcx Expr<'tcx>, bool)> {
+    fn deref_assign_or_addr_of<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<(&'tcx Expr<'tcx>, bool)> {
         // &mut <expr>
         let inner = if let ExprKind::AddrOf(_, Mutability::Mut, expr) = expr.kind {
             expr
@@ -80,7 +84,7 @@ fn is_operation_we_care_about<'tcx>(
         };
 
         if let ExprKind::Unary(UnOp::Deref, e) = &inner.kind {
-            Some((!matches!(expr.kind, ExprKind::AddrOf(..)), e))
+            Some((e, !matches!(expr.kind, ExprKind::AddrOf(..))))
         } else {
             None
         }
@@ -89,7 +93,7 @@ fn is_operation_we_care_about<'tcx>(
     fn ptr_write<'tcx>(
         cx: &LateContext<'tcx>,
         e: &'tcx Expr<'tcx>,
-    ) -> Option<(bool, &'tcx Expr<'tcx>)> {
+    ) -> Option<(&'tcx Expr<'tcx>, bool)> {
         if let ExprKind::Call(path, [arg_ptr, _arg_val]) = e.kind
             && let ExprKind::Path(ref qpath) = path.kind
             && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
@@ -98,7 +102,7 @@ fn is_operation_we_care_about<'tcx>(
                 Some(sym::ptr_write | sym::ptr_write_volatile | sym::ptr_write_unaligned)
             )
         {
-            Some((true, arg_ptr))
+            Some((arg_ptr, true))
         } else {
             None
         }
@@ -107,7 +111,7 @@ fn is_operation_we_care_about<'tcx>(
     deref_assign_or_addr_of(e).or_else(|| ptr_write(cx, e))
 }
 
-fn is_cast_from_const_to_mut<'tcx>(
+fn is_cast_from_ref_to_mut_ptr<'tcx>(
     cx: &LateContext<'tcx>,
     orig_expr: &'tcx Expr<'tcx>,
 ) -> Option<bool> {