1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#![allow(clippy::non_canonical_clone_impl, unused)]
#![warn(clippy::unnecessary_struct_initialization)]
struct S {
f: String,
}
#[derive(Clone, Copy)]
struct T {
f: u32,
}
struct U {
f: u32,
}
impl Clone for U {
fn clone(&self) -> Self {
// Do not lint: `Self` does not implement `Copy`
Self { ..*self }
}
}
#[derive(Copy)]
struct V {
f: u32,
}
struct W {
f1: u32,
f2: u32,
}
impl Clone for V {
fn clone(&self) -> Self {
// Lint: `Self` implements `Copy`
*self
//~^ unnecessary_struct_initialization
}
}
fn main() {
// Should lint: `a` would be consumed anyway
let a = S { f: String::from("foo") };
let mut b = a;
//~^ unnecessary_struct_initialization
// Should lint: `b` would be consumed, and is mutable
let c = &mut b;
//~^ unnecessary_struct_initialization
// Should not lint as `d` is not mutable
let d = S { f: String::from("foo") };
let e = &mut S { ..d };
// Should lint as `f` would be consumed anyway
let f = S { f: String::from("foo") };
let g = &f;
//~^ unnecessary_struct_initialization
// Should lint: the result of an expression is mutable
let h = &mut *Box::new(S { f: String::from("foo") });
// Should not lint: `m` would be both alive and borrowed
let m = T { f: 17 };
let n = &T { ..m };
// Should not lint: `m` should not be modified
let o = &mut T { ..m };
o.f = 32;
assert_eq!(m.f, 17);
// Should not lint: `m` should not be modified
let o = &mut T { ..m } as *mut T;
unsafe { &mut *o }.f = 32;
assert_eq!(m.f, 17);
// Should lint: the result of an expression is mutable and temporary
let p = &mut *Box::new(T { f: 5 });
// Should lint: all fields of `q` would be consumed anyway
let q = W { f1: 42, f2: 1337 };
let r = q;
//~^ unnecessary_struct_initialization
// Should not lint: not all fields of `t` from same source
let s = W { f1: 1337, f2: 42 };
let t = W { f1: s.f1, f2: r.f2 };
// Should not lint: different fields of `s` assigned
let u = W { f1: s.f2, f2: s.f1 };
// Should lint: all fields of `v` would be consumed anyway
let v = W { f1: 42, f2: 1337 };
let w = v;
//~^ unnecessary_struct_initialization
// Should not lint: source differs between fields and base
let x = W { f1: 42, f2: 1337 };
let y = W { f1: w.f1, ..x };
// Should lint: range desugars to struct
let r1 = 0..5;
let r2 = r1;
//~^ unnecessary_struct_initialization
references();
shorthand();
}
fn references() {
// Should not lint as `a` is not mutable
let a = W { f1: 42, f2: 1337 };
let b = &mut W { f1: a.f1, f2: a.f2 };
// Should lint as `d` is a shared reference
let c = W { f1: 42, f2: 1337 };
let d = &c;
//~^ unnecessary_struct_initialization
// Should not lint as `e` is not mutable
let e = W { f1: 42, f2: 1337 };
let f = &mut W { f1: e.f1, ..e };
// Should lint as `h` is a shared reference
let g = W { f1: 42, f2: 1337 };
let h = &g;
//~^ unnecessary_struct_initialization
// Should not lint as `j` is copy
let i = V { f: 0x1701d };
let j = &V { ..i };
// Should not lint as `k` is copy
let k = V { f: 0x1701d };
let l = &V { f: k.f };
}
fn shorthand() {
struct S1 {
a: i32,
b: i32,
}
let a = 42;
let s = S1 { a: 3, b: 4 };
// Should not lint: `a` is not from `s`
let s = S1 { a, b: s.b };
}
|