forked from rust-lang/rust-clippy
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
171 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![allow(deprecated, invalid_value)] | ||
#![warn(clippy::all)] | ||
|
||
use std::mem; | ||
|
||
fn might_panic<X>(x: X) -> X { | ||
// in practice this would be a possibly-panicky operation | ||
x | ||
} | ||
|
||
fn main() { | ||
let mut v = vec![0i32; 4]; | ||
// the following is UB if `might_panic` panics | ||
unsafe { | ||
let taken_v = mem::replace(&mut v, mem::uninitialized()); | ||
let new_v = might_panic(taken_v); | ||
std::mem::forget(mem::replace(&mut v, new_v)); | ||
} | ||
|
||
unsafe { | ||
let taken_v = mem::replace(&mut v, mem::zeroed()); | ||
let new_v = might_panic(taken_v); | ||
std::mem::forget(mem::replace(&mut v, new_v)); | ||
} | ||
|
||
// this is silly but OK, because usize is a primitive type | ||
let mut u: usize = 42; | ||
let uref = &mut u; | ||
let taken_u = unsafe { mem::replace(uref, mem::zeroed()) }; | ||
*uref = taken_u + 1; | ||
|
||
// this is still not OK, because uninit | ||
let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) }; | ||
*uref = taken_u + 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error: replacing with `mem::uninitialized()` | ||
--> $DIR/repl_uninit.rs:15:23 | ||
| | ||
LL | let taken_v = mem::replace(&mut v, mem::uninitialized()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::mem-replace-with-uninit` implied by `-D warnings` | ||
= help: consider using the `take_mut` crate instead | ||
|
||
error: replacing with `mem::zeroed()` | ||
--> $DIR/repl_uninit.rs:21:23 | ||
| | ||
LL | let taken_v = mem::replace(&mut v, mem::zeroed()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a default value or the `take_mut` crate instead | ||
|
||
error: replacing with `mem::uninitialized()` | ||
--> $DIR/repl_uninit.rs:33:28 | ||
| | ||
LL | let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using the `take_mut` crate instead | ||
|
||
error: aborting due to 3 previous errors | ||
|