Skip to content

Commit 0f6284c

Browse files
committed
Add test of const item mutation with Drop impl
1 parent bb760b5 commit 0f6284c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/test/ui/lint/lint-const-item-mutation.rs

+12
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ impl MyStruct {
99
fn use_mut(&mut self) {}
1010
}
1111

12+
struct Mutable {
13+
msg: &'static str,
14+
}
15+
impl Drop for Mutable {
16+
fn drop(&mut self) {
17+
println!("{}", self.msg);
18+
}
19+
}
20+
1221
const ARRAY: [u8; 1] = [25];
1322
const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
1423
const RAW_PTR: *mut u8 = 1 as *mut u8;
24+
const MUTABLE: Mutable = Mutable { msg: "" };
1525

1626
fn main() {
1727
ARRAY[0] = 5; //~ WARN attempting to modify
@@ -29,4 +39,6 @@ fn main() {
2939
*RAW_PTR = 0;
3040
*MY_STRUCT.raw_ptr = 0;
3141
}
42+
43+
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
3244
}

src/test/ui/lint/lint-const-item-mutation.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
warning: attempting to modify a `const` item
2-
--> $DIR/lint-const-item-mutation.rs:17:5
2+
--> $DIR/lint-const-item-mutation.rs:27:5
33
|
44
LL | ARRAY[0] = 5;
55
| ^^^^^^^^^^^^
66
|
77
= note: `#[warn(const_item_mutation)]` on by default
88
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
99
note: `const` item defined here
10-
--> $DIR/lint-const-item-mutation.rs:12:1
10+
--> $DIR/lint-const-item-mutation.rs:21:1
1111
|
1212
LL | const ARRAY: [u8; 1] = [25];
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: attempting to modify a `const` item
16-
--> $DIR/lint-const-item-mutation.rs:18:5
16+
--> $DIR/lint-const-item-mutation.rs:28:5
1717
|
1818
LL | MY_STRUCT.field = false;
1919
| ^^^^^^^^^^^^^^^^^^^^^^^
2020
|
2121
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
2222
note: `const` item defined here
23-
--> $DIR/lint-const-item-mutation.rs:13:1
23+
--> $DIR/lint-const-item-mutation.rs:22:1
2424
|
2525
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

2828
warning: attempting to modify a `const` item
29-
--> $DIR/lint-const-item-mutation.rs:19:5
29+
--> $DIR/lint-const-item-mutation.rs:29:5
3030
|
3131
LL | MY_STRUCT.inner_array[0] = 'b';
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
|
3434
= note: each usage of a `const` item creates a new temporary - the original `const` item will not be modified
3535
note: `const` item defined here
36-
--> $DIR/lint-const-item-mutation.rs:13:1
36+
--> $DIR/lint-const-item-mutation.rs:22:1
3737
|
3838
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

4141
warning: taking a mutable reference to a `const` item
42-
--> $DIR/lint-const-item-mutation.rs:20:5
42+
--> $DIR/lint-const-item-mutation.rs:30:5
4343
|
4444
LL | MY_STRUCT.use_mut();
4545
| ^^^^^^^^^^^^^^^^^^^
@@ -52,35 +52,35 @@ note: mutable reference created due to call to this method
5252
LL | fn use_mut(&mut self) {}
5353
| ^^^^^^^^^^^^^^^^^^^^^
5454
note: `const` item defined here
55-
--> $DIR/lint-const-item-mutation.rs:13:1
55+
--> $DIR/lint-const-item-mutation.rs:22:1
5656
|
5757
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959

6060
warning: taking a mutable reference to a `const` item
61-
--> $DIR/lint-const-item-mutation.rs:21:5
61+
--> $DIR/lint-const-item-mutation.rs:31:5
6262
|
6363
LL | &mut MY_STRUCT;
6464
| ^^^^^^^^^^^^^^
6565
|
6666
= note: each usage of a `const` item creates a new temporary
6767
= note: the mutable reference will refer to this temporary, not the original `const` item
6868
note: `const` item defined here
69-
--> $DIR/lint-const-item-mutation.rs:13:1
69+
--> $DIR/lint-const-item-mutation.rs:22:1
7070
|
7171
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7373

7474
warning: taking a mutable reference to a `const` item
75-
--> $DIR/lint-const-item-mutation.rs:22:5
75+
--> $DIR/lint-const-item-mutation.rs:32:5
7676
|
7777
LL | (&mut MY_STRUCT).use_mut();
7878
| ^^^^^^^^^^^^^^^^
7979
|
8080
= note: each usage of a `const` item creates a new temporary
8181
= note: the mutable reference will refer to this temporary, not the original `const` item
8282
note: `const` item defined here
83-
--> $DIR/lint-const-item-mutation.rs:13:1
83+
--> $DIR/lint-const-item-mutation.rs:22:1
8484
|
8585
LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw_ptr: 2 as *mut u8 };
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)