Skip to content

Commit 73a30f8

Browse files
committed
More tests for invalid_value lint
1 parent 57ddb2d commit 73a30f8

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/test/ui/lint/uninitialized-zeroed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ fn main() {
103103
let _val: i32 = mem::zeroed();
104104
let _val: i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
105105

106+
let _val: f32 = mem::zeroed();
107+
let _val: f32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
108+
109+
let _val: *const () = mem::zeroed();
110+
let _val: *const () = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
111+
112+
let _val: *const [()] = mem::zeroed();
113+
let _val: *const [()] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
114+
106115
// Transmute-from-0
107116
let _val: &'static i32 = mem::transmute(0usize); //~ ERROR: does not permit zero-initialization
108117
let _val: &'static [i32] = mem::transmute((0usize, 0usize)); //~ ERROR: does not permit zero-initialization

src/test/ui/lint/uninitialized-zeroed.stderr

+40-7
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,41 @@ LL | let _val: i32 = mem::uninitialized();
425425
|
426426
= note: integers must not be uninitialized
427427

428+
error: the type `f32` does not permit being left uninitialized
429+
--> $DIR/uninitialized-zeroed.rs:107:25
430+
|
431+
LL | let _val: f32 = mem::uninitialized();
432+
| ^^^^^^^^^^^^^^^^^^^^
433+
| |
434+
| this code causes undefined behavior when executed
435+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
436+
|
437+
= note: floats must not be uninitialized
438+
439+
error: the type `*const ()` does not permit being left uninitialized
440+
--> $DIR/uninitialized-zeroed.rs:110:31
441+
|
442+
LL | let _val: *const () = mem::uninitialized();
443+
| ^^^^^^^^^^^^^^^^^^^^
444+
| |
445+
| this code causes undefined behavior when executed
446+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
447+
|
448+
= note: raw pointers must not be uninitialized
449+
450+
error: the type `*const [()]` does not permit being left uninitialized
451+
--> $DIR/uninitialized-zeroed.rs:113:33
452+
|
453+
LL | let _val: *const [()] = mem::uninitialized();
454+
| ^^^^^^^^^^^^^^^^^^^^
455+
| |
456+
| this code causes undefined behavior when executed
457+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
458+
|
459+
= note: raw pointers must not be uninitialized
460+
428461
error: the type `&i32` does not permit zero-initialization
429-
--> $DIR/uninitialized-zeroed.rs:107:34
462+
--> $DIR/uninitialized-zeroed.rs:116:34
430463
|
431464
LL | let _val: &'static i32 = mem::transmute(0usize);
432465
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -437,7 +470,7 @@ LL | let _val: &'static i32 = mem::transmute(0usize);
437470
= note: references must be non-null
438471

439472
error: the type `&[i32]` does not permit zero-initialization
440-
--> $DIR/uninitialized-zeroed.rs:108:36
473+
--> $DIR/uninitialized-zeroed.rs:117:36
441474
|
442475
LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
443476
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -448,7 +481,7 @@ LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
448481
= note: references must be non-null
449482

450483
error: the type `NonZeroU32` does not permit zero-initialization
451-
--> $DIR/uninitialized-zeroed.rs:109:32
484+
--> $DIR/uninitialized-zeroed.rs:118:32
452485
|
453486
LL | let _val: NonZeroU32 = mem::transmute(0);
454487
| ^^^^^^^^^^^^^^^^^
@@ -459,7 +492,7 @@ LL | let _val: NonZeroU32 = mem::transmute(0);
459492
= note: `std::num::NonZeroU32` must be non-null
460493

461494
error: the type `NonNull<i32>` does not permit zero-initialization
462-
--> $DIR/uninitialized-zeroed.rs:112:34
495+
--> $DIR/uninitialized-zeroed.rs:121:34
463496
|
464497
LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
465498
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -470,7 +503,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
470503
= note: `std::ptr::NonNull<i32>` must be non-null
471504

472505
error: the type `NonNull<i32>` does not permit being left uninitialized
473-
--> $DIR/uninitialized-zeroed.rs:113:34
506+
--> $DIR/uninitialized-zeroed.rs:122:34
474507
|
475508
LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
476509
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -481,7 +514,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
481514
= note: `std::ptr::NonNull<i32>` must be non-null
482515

483516
error: the type `bool` does not permit being left uninitialized
484-
--> $DIR/uninitialized-zeroed.rs:114:26
517+
--> $DIR/uninitialized-zeroed.rs:123:26
485518
|
486519
LL | let _val: bool = MaybeUninit::uninit().assume_init();
487520
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -491,5 +524,5 @@ LL | let _val: bool = MaybeUninit::uninit().assume_init();
491524
|
492525
= note: booleans must be either `true` or `false`
493526

494-
error: aborting due to 40 previous errors
527+
error: aborting due to 43 previous errors
495528

0 commit comments

Comments
 (0)