Skip to content

Commit

Permalink
Add false negative test for uninit_assumed_init
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Apr 15, 2021
1 parent 9b93be1 commit 76dcd0d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions tests/ui/manual_map_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ fn main() {
None => None,
};
}

// #7077
let s = &String::new();
let _: Option<&str> = Some(s).map(|s| s);
}
12 changes: 11 additions & 1 deletion tests/ui/manual_map_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,15 @@ LL | | None
LL | | };
| |_____^ help: try this: `{ Some(0).map(|x| x + 1) }`

error: aborting due to 21 previous errors
error: manual implementation of `Option::map`
--> $DIR/manual_map_option.rs:218:27
|
LL | let _: Option<&str> = match Some(s) {
| ___________________________^
LL | | Some(s) => Some(s),
LL | | None => None,
LL | | };
| |_____^ help: try this: `Some(s).map(|s| s)`

error: aborting due to 22 previous errors

4 changes: 2 additions & 2 deletions tests/ui/repl_uninit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(deprecated, invalid_value)]
#![warn(clippy::all)]
#![allow(deprecated, invalid_value, clippy::uninit_assumed_init)]
#![warn(clippy::mem_replace_with_uninit)]

use std::mem;

Expand Down
10 changes: 1 addition & 9 deletions tests/ui/repl_uninit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ error: replacing with `mem::MaybeUninit::uninit().assume_init()`
LL | let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::ptr::read(&mut v)`

error: this call for this type may be undefined behavior
--> $DIR/repl_uninit.rs:21:44
|
LL | let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::uninit-assumed-init` implied by `-D warnings`

error: replacing with `mem::zeroed()`
--> $DIR/repl_uninit.rs:27:23
|
Expand All @@ -34,5 +26,5 @@ error: replacing with `mem::uninitialized()`
LL | let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::ptr::read(uref)`

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

5 changes: 4 additions & 1 deletion tests/ui/uninit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(stmt_expr_attributes)]

use std::mem::MaybeUninit;
use std::mem::{self, MaybeUninit};

fn main() {
let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
Expand All @@ -19,4 +19,7 @@ fn main() {

// This is OK, because all constitutent types are uninit-compatible.
let _: (MaybeUninit<usize>, [MaybeUninit<bool>; 2]) = unsafe { MaybeUninit::uninit().assume_init() };

// Was a false negative.
let _: usize = unsafe { mem::MaybeUninit::uninit().assume_init() };
}
8 changes: 7 additions & 1 deletion tests/ui/uninit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ error: this call for this type may be undefined behavior
LL | let _: [u8; 0] = unsafe { MaybeUninit::uninit().assume_init() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: this call for this type may be undefined behavior
--> $DIR/uninit.rs:24:29
|
LL | let _: usize = unsafe { mem::MaybeUninit::uninit().assume_init() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

0 comments on commit 76dcd0d

Please sign in to comment.