Skip to content

Commit 3fe1a80

Browse files
committed
Recurse into let bindings if possible
1 parent 258bee6 commit 3fe1a80

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

compiler/rustc_lint/src/reference_casting.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ fn peel_casts<'tcx>(cx: &LateContext<'tcx>, mut e: &'tcx Expr<'tcx>) -> (&'tcx E
172172
}
173173
arg
174174
} else {
175-
break;
175+
let init = cx.expr_or_init(e);
176+
if init.hir_id != e.hir_id {
177+
init
178+
} else {
179+
break;
180+
}
176181
};
177182
}
178183

tests/ui/lint/reference_casting.rs

+7
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ unsafe fn assign_to_ref() {
113113
*((&std::cell::UnsafeCell::new(0)) as *const _ as *mut i32) = 5;
114114
//~^ ERROR assigning to `&T` is undefined behavior
115115

116+
let value = num as *const i32 as *mut i32;
117+
*value = 1;
118+
//~^ ERROR assigning to `&T` is undefined behavior
119+
let value = num as *const i32;
120+
let value = value as *mut i32;
121+
*value = 1;
122+
//~^ ERROR assigning to `&T` is undefined behavior
116123
let value = num as *const i32 as *mut i32;
117124
*value = 1;
118125
//~^ ERROR assigning to `&T` is undefined behavior

tests/ui/lint/reference_casting.stderr

+28-8
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,27 @@ LL | *value = 1;
286286
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
287287

288288
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
289-
--> $DIR/reference_casting.rs:120:5
289+
--> $DIR/reference_casting.rs:121:5
290+
|
291+
LL | let value = value as *mut i32;
292+
| ----------------- casting happend here
293+
LL | *value = 1;
294+
| ^^^^^^^^^^
295+
|
296+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
297+
298+
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
299+
--> $DIR/reference_casting.rs:124:5
300+
|
301+
LL | let value = num as *const i32 as *mut i32;
302+
| ----------------------------- casting happend here
303+
LL | *value = 1;
304+
| ^^^^^^^^^^
305+
|
306+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
307+
308+
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
309+
--> $DIR/reference_casting.rs:127:5
290310
|
291311
LL | let value = num as *const i32 as *mut i32;
292312
| ----------------------------- casting happend here
@@ -297,23 +317,23 @@ LL | *value_rebind = 1;
297317
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
298318

299319
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
300-
--> $DIR/reference_casting.rs:122:5
320+
--> $DIR/reference_casting.rs:129:5
301321
|
302322
LL | *(num as *const i32).cast::<i32>().cast_mut() = 2;
303323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304324
|
305325
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
306326

307327
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
308-
--> $DIR/reference_casting.rs:124:5
328+
--> $DIR/reference_casting.rs:131:5
309329
|
310330
LL | *(num as *const _ as usize as *mut i32) = 2;
311331
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
312332
|
313333
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
314334

315335
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
316-
--> $DIR/reference_casting.rs:126:5
336+
--> $DIR/reference_casting.rs:133:5
317337
|
318338
LL | let value = num as *const i32 as *mut i32;
319339
| ----------------------------- casting happend here
@@ -324,7 +344,7 @@ LL | std::ptr::write(value, 2);
324344
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
325345

326346
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
327-
--> $DIR/reference_casting.rs:128:5
347+
--> $DIR/reference_casting.rs:135:5
328348
|
329349
LL | let value = num as *const i32 as *mut i32;
330350
| ----------------------------- casting happend here
@@ -335,7 +355,7 @@ LL | std::ptr::write_unaligned(value, 2);
335355
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
336356

337357
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
338-
--> $DIR/reference_casting.rs:130:5
358+
--> $DIR/reference_casting.rs:137:5
339359
|
340360
LL | let value = num as *const i32 as *mut i32;
341361
| ----------------------------- casting happend here
@@ -346,12 +366,12 @@ LL | std::ptr::write_volatile(value, 2);
346366
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
347367

348368
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
349-
--> $DIR/reference_casting.rs:134:9
369+
--> $DIR/reference_casting.rs:141:9
350370
|
351371
LL | *(this as *const _ as *mut _) = a;
352372
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353373
|
354374
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
355375

356-
error: aborting due to 40 previous errors
376+
error: aborting due to 42 previous errors
357377

0 commit comments

Comments
 (0)