summaryrefslogtreecommitdiffstats
path: root/src/test/ui/lint/lint-const-item-mutation.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/lint/lint-const-item-mutation.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/lint/lint-const-item-mutation.rs')
-rw-r--r--src/test/ui/lint/lint-const-item-mutation.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/test/ui/lint/lint-const-item-mutation.rs b/src/test/ui/lint/lint-const-item-mutation.rs
new file mode 100644
index 000000000..4bf5e0a9e
--- /dev/null
+++ b/src/test/ui/lint/lint-const-item-mutation.rs
@@ -0,0 +1,66 @@
+// check-pass
+
+struct MyStruct {
+ field: bool,
+ inner_array: [char; 1],
+ raw_ptr: *mut u8
+}
+impl MyStruct {
+ fn use_mut(&mut self) {}
+}
+
+struct Mutable {
+ msg: &'static str,
+}
+impl Drop for Mutable {
+ fn drop(&mut self) {
+ println!("{}", self.msg);
+ }
+}
+
+struct Mutable2 { // this one has drop glue but not a Drop impl
+ msg: &'static str,
+ other: String,
+}
+
+const ARRAY: [u8; 1] = [25];
+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;
+const MUTABLE: Mutable = Mutable { msg: "" };
+const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
+const VEC: Vec<i32> = Vec::new();
+const PTR: *mut () = 1 as *mut _;
+const PTR_TO_ARRAY: *mut [u32; 4] = 0x12345678 as _;
+const ARRAY_OF_PTR: [*mut u32; 1] = [1 as *mut _];
+
+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
+
+ // 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;
+ }
+
+ MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
+ MUTABLE2.msg = "wow"; //~ WARN attempting to modify
+ VEC.push(0); //~ WARN taking a mutable reference to a `const` item
+
+ // Test that we don't warn when converting a raw pointer
+ // into a mutable reference
+ unsafe { &mut *PTR };
+
+ // 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; }
+}