2020-08-10 11:16:30 +00:00
|
|
|
// check-pass
|
|
|
|
|
|
|
|
struct MyStruct {
|
|
|
|
field: bool,
|
|
|
|
inner_array: [char; 1],
|
2020-09-29 03:51:57 +00:00
|
|
|
raw_ptr: *mut u8
|
2020-08-10 11:16:30 +00:00
|
|
|
}
|
|
|
|
impl MyStruct {
|
|
|
|
fn use_mut(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2020-09-27 01:42:19 +00:00
|
|
|
struct Mutable {
|
|
|
|
msg: &'static str,
|
|
|
|
}
|
|
|
|
impl Drop for Mutable {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("{}", self.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 04:19:46 +00:00
|
|
|
struct Mutable2 { // this one has drop glue but not a Drop impl
|
|
|
|
msg: &'static str,
|
|
|
|
other: String,
|
|
|
|
}
|
|
|
|
|
2020-08-10 11:16:30 +00:00
|
|
|
const ARRAY: [u8; 1] = [25];
|
2020-09-29 03:51:57 +00:00
|
|
|
const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
|
|
|
|
const RAW_PTR: *mut u8 = 1 as *mut u8;
|
2020-09-27 01:42:19 +00:00
|
|
|
const MUTABLE: Mutable = Mutable { msg: "" };
|
2020-09-27 04:19:46 +00:00
|
|
|
const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
|
2020-10-01 05:13:33 +00:00
|
|
|
const VEC: Vec<i32> = Vec::new();
|
2020-11-08 01:11:53 +00:00
|
|
|
const PTR: *mut () = 1 as *mut _;
|
2020-12-17 20:25:55 +00:00
|
|
|
const PTR_TO_ARRAY: *mut [u32; 4] = 0x12345678 as _;
|
|
|
|
const ARRAY_OF_PTR: [*mut u32; 1] = [1 as *mut _];
|
2020-08-10 11:16:30 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
ARRAY[0] = 5; //~ WARN attempting to modify
|
|
|
|
MY_STRUCT.field = false; //~ WARN attempting to modify
|
|
|
|
MY_STRUCT.inner_array[0] = 'b'; //~ WARN attempting to modify
|
|
|
|
MY_STRUCT.use_mut(); //~ WARN taking
|
|
|
|
&mut MY_STRUCT; //~ WARN taking
|
|
|
|
(&mut MY_STRUCT).use_mut(); //~ WARN taking
|
2020-09-29 03:51:57 +00:00
|
|
|
|
|
|
|
// Test that we don't warn when writing through
|
|
|
|
// a raw pointer
|
|
|
|
// This is U.B., but this test is check-pass,
|
|
|
|
// so this never actually executes
|
|
|
|
unsafe {
|
|
|
|
*RAW_PTR = 0;
|
|
|
|
*MY_STRUCT.raw_ptr = 0;
|
|
|
|
}
|
2020-09-27 01:42:19 +00:00
|
|
|
|
|
|
|
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
|
2020-09-27 04:19:46 +00:00
|
|
|
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
|
2020-10-01 05:16:32 +00:00
|
|
|
VEC.push(0); //~ WARN taking a mutable reference to a `const` item
|
2020-11-08 01:11:53 +00:00
|
|
|
|
|
|
|
// Test that we don't warn when converting a raw pointer
|
|
|
|
// into a mutable reference
|
|
|
|
unsafe { &mut *PTR };
|
2020-12-17 20:25:55 +00:00
|
|
|
|
|
|
|
// Test that we don't warn when there's a dereference involved.
|
|
|
|
// If we ever 'leave' the const via a deference, we're going
|
|
|
|
// to end up modifying something other than the temporary
|
|
|
|
unsafe { (*PTR_TO_ARRAY)[0] = 1 };
|
|
|
|
unsafe { *ARRAY_OF_PTR[0] = 25; }
|
2020-08-10 11:16:30 +00:00
|
|
|
}
|