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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#![allow(unused)]
#![warn(clippy::missing_fields_in_debug)]
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
struct NamedStruct1Ignored {
data: u8,
hidden: u32,
}
impl fmt::Debug for NamedStruct1Ignored {
//~^ ERROR: manual `Debug` impl does not include all fields
// unused field: hidden
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("NamedStruct1Ignored")
.field("data", &self.data)
.finish()
}
}
struct NamedStructMultipleIgnored {
data: u8,
hidden: u32,
hidden2: String,
hidden3: Vec<Vec<i32>>,
hidden4: ((((u8), u16), u32), u64),
}
impl fmt::Debug for NamedStructMultipleIgnored {
//~^ ERROR: manual `Debug` impl does not include all fields
// unused fields: hidden, hidden2, hidden4
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("NamedStructMultipleIgnored")
.field("data", &self.data)
.field("hidden3", &self.hidden3)
.finish()
}
}
struct Unit;
// ok
impl fmt::Debug for Unit {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("Unit").finish()
}
}
struct UnnamedStruct1Ignored(String);
impl fmt::Debug for UnnamedStruct1Ignored {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_tuple("UnnamedStruct1Ignored").finish()
}
}
struct UnnamedStructMultipleIgnored(String, Vec<u8>, i32);
// tuple structs are not linted
impl fmt::Debug for UnnamedStructMultipleIgnored {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_tuple("UnnamedStructMultipleIgnored")
.field(&self.1)
.finish()
}
}
struct NamedStructNonExhaustive {
a: u8,
b: String,
}
// ok
impl fmt::Debug for NamedStructNonExhaustive {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("NamedStructNonExhaustive")
.field("a", &self.a)
.finish_non_exhaustive() // should not warn here
}
}
struct MultiExprDebugImpl {
a: u8,
b: String,
}
// ok
impl fmt::Debug for MultiExprDebugImpl {
//~^ ERROR: manual `Debug` impl does not include all fields
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = formatter.debug_struct("MultiExprDebugImpl");
f.field("a", &self.a);
f.finish()
}
}
#[derive(Debug)]
struct DerivedStruct {
a: u8,
b: i32,
}
// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1166846953
struct Inner {
a: usize,
b: usize,
}
struct HasInner {
inner: Inner,
}
impl HasInner {
fn get(&self) -> &Inner {
&self.inner
}
}
impl fmt::Debug for HasInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let inner = self.get();
f.debug_struct("HasInner")
.field("a", &inner.a)
.field("b", &inner.b)
.finish()
}
}
// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1170306053
struct Foo {
a: u8,
b: u8,
}
impl fmt::Debug for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Foo").field("a", &self.a).field("b", &()).finish()
}
}
// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1175473620
mod comment1175473620 {
use super::*;
struct Inner {
a: usize,
b: usize,
}
struct Wrapper(Inner);
impl Deref for Wrapper {
type Target = Inner;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Debug for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Wrapper")
.field("a", &self.a)
.field("b", &self.b)
.finish()
}
}
}
// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1175488757
// PhantomData is an exception and does not need to be included
struct WithPD {
a: u8,
b: u8,
c: PhantomData<String>,
}
impl fmt::Debug for WithPD {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WithPD")
.field("a", &self.a)
.field("b", &self.b)
.finish()
}
}
fn main() {}
|