]> git.lizzy.rs Git - rust.git/blob - tests/ui/temporary_assignment.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / temporary_assignment.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::temporary_assignment)]
11
12 use std::ops::{Deref, DerefMut};
13
14 struct TupleStruct(i32);
15
16 struct Struct {
17     field: i32,
18 }
19
20 struct MultiStruct {
21     structure: Struct,
22 }
23
24 struct Wrapper<'a> {
25     inner: &'a mut Struct,
26 }
27
28 impl<'a> Deref for Wrapper<'a> {
29     type Target = Struct;
30     fn deref(&self) -> &Struct {
31         self.inner
32     }
33 }
34
35 impl<'a> DerefMut for Wrapper<'a> {
36     fn deref_mut(&mut self) -> &mut Struct {
37         self.inner
38     }
39 }
40
41 struct ArrayStruct {
42     array: [i32; 1],
43 }
44
45 const A: TupleStruct = TupleStruct(1);
46 const B: Struct = Struct { field: 1 };
47 const C: MultiStruct = MultiStruct {
48     structure: Struct { field: 1 },
49 };
50 const D: ArrayStruct = ArrayStruct { array: [1] };
51
52 fn main() {
53     let mut s = Struct { field: 0 };
54     let mut t = (0, 0);
55
56     Struct { field: 0 }.field = 1;
57     MultiStruct {
58         structure: Struct { field: 0 },
59     }
60     .structure
61     .field = 1;
62     ArrayStruct { array: [0] }.array[0] = 1;
63     (0, 0).0 = 1;
64
65     A.0 = 2;
66     B.field = 2;
67     C.structure.field = 2;
68     D.array[0] = 2;
69
70     // no error
71     s.field = 1;
72     t.0 = 1;
73     Wrapper { inner: &mut s }.field = 1;
74     let mut a_mut = TupleStruct(1);
75     a_mut.0 = 2;
76     let mut b_mut = Struct { field: 1 };
77     b_mut.field = 2;
78     let mut c_mut = MultiStruct {
79         structure: Struct { field: 1 },
80     };
81     c_mut.structure.field = 2;
82     let mut d_mut = ArrayStruct { array: [1] };
83     d_mut.array[0] = 2;
84 }