about summary refs log tree commit diff
path: root/compiler/rustc_lint/src/reference_casting.rs
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2023-07-14 22:15:28 +0200
committerUrgau <urgau@numericable.fr>2023-07-29 12:20:59 +0200
commit50a46710a9c6942751930fa963e0d70d84128859 (patch)
treeb382e23d9e9702a680c80940613da4ac6cb8ea66 /compiler/rustc_lint/src/reference_casting.rs
parentf3dafe91ff753e3a801aa336d41be9eca75925bc (diff)
downloadrust-50a46710a9c6942751930fa963e0d70d84128859.tar.gz
rust-50a46710a9c6942751930fa963e0d70d84128859.zip
Avoid linting on expression that are only UB with SB/TB
Diffstat (limited to 'compiler/rustc_lint/src/reference_casting.rs')
-rw-r--r--compiler/rustc_lint/src/reference_casting.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/reference_casting.rs b/compiler/rustc_lint/src/reference_casting.rs
index e6cab4cebe7..428bf750bbd 100644
--- a/compiler/rustc_lint/src/reference_casting.rs
+++ b/compiler/rustc_lint/src/reference_casting.rs
@@ -56,7 +56,20 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
     }
 
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
-        let ExprKind::Unary(UnOp::Deref, e) = &expr.kind else {
+        // &mut <expr>
+        let inner = if let ExprKind::AddrOf(_, Mutability::Mut, expr) = expr.kind {
+            expr
+        // <expr> = ...
+        } else if let ExprKind::Assign(expr, _, _) = expr.kind {
+            expr
+        // <expr> += ...
+        } else if let ExprKind::AssignOp(_, expr, _) = expr.kind {
+            expr
+        } else {
+            return;
+        };
+
+        let ExprKind::Unary(UnOp::Deref, e) = &inner.kind else {
             return;
         };
 
@@ -103,5 +116,5 @@ fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>)
     };
 
     let e = e.peel_blocks();
-    matches!(cx.typeck_results().node_type(e.hir_id).kind(), ty::Ref(..))
+    matches!(cx.typeck_results().node_type(e.hir_id).kind(), ty::Ref(_, _, Mutability::Not))
 }