@@ -147,10 +147,15 @@ The [RFC][1270-deprecation.md] contains motivations and more details.
147
147
148
148
## The ` must_use ` attribute
149
149
150
- The * ` must_use ` attribute* can be used on user-defined composite types
150
+ The * ` must_use ` attribute* is used to issue a diagnostic warning when a value
151
+ is not "used". It can be applied to user-defined composite types
151
152
([ ` struct ` s] [ struct ] , [ ` enum ` s] [ enum ] , and [ ` union ` s] [ union ] ), [ functions] ,
152
153
and [ traits] .
153
154
155
+ The ` must_use ` attribute may include a message by using the
156
+ [ _ MetaNameValueStr_ ] syntax such as ` #[must_use = "example message"] ` . The
157
+ message will be given alongside the warning.
158
+
154
159
When used on user-defined composite types, if the [ expression] of an
155
160
[ expression statement] has that type, then the ` unused_must_use ` lint is
156
161
violated.
@@ -165,10 +170,8 @@ struct MustUse {
165
170
# fn new () -> MustUse { MustUse {} }
166
171
# }
167
172
#
168
- fn main () {
169
- // Violates the `unused_must_use` lint.
170
- MustUse :: new ();
171
- }
173
+ // Violates the `unused_must_use` lint.
174
+ MustUse :: new ();
172
175
```
173
176
174
177
When used on a function, if the [ expression] of an [ expression statement] is a
@@ -179,10 +182,25 @@ violated.
179
182
#[must_use]
180
183
fn five () -> i32 { 5i32 }
181
184
182
- fn main () {
183
- // Violates the unused_must_use lint.
184
- five ();
185
+ // Violates the unused_must_use lint.
186
+ five ();
187
+ ```
188
+
189
+ When used on a [ trait declaration] , a [ call expression] of an [ expression
190
+ statement] to a function that returns an [ impl trait] of that trait violates
191
+ the ` unsued_must_use ` lint.
192
+
193
+ ``` rust
194
+ #[must_use]
195
+ trait Critical {}
196
+ impl Critical for i32 {}
197
+
198
+ fn get_critical () -> impl Critical {
199
+ 4i32
185
200
}
201
+
202
+ // Violates the `unused_must_use` lint.
203
+ get_critical ();
186
204
```
187
205
188
206
When used on a function in a trait declaration, then the behavior also applies
@@ -198,10 +216,8 @@ impl Trait for i32 {
198
216
fn use_me (& self ) -> i32 { 0i32 }
199
217
}
200
218
201
- fn main () {
202
- // Violates the `unused_must_use` lint.
203
- 5i32 . use_me ();
204
- }
219
+ // Violates the `unused_must_use` lint.
220
+ 5i32 . use_me ();
205
221
```
206
222
207
223
When used on a function in a trait implementation, the attribute does nothing.
@@ -215,16 +231,14 @@ When used on a function in a trait implementation, the attribute does nothing.
215
231
> #[must_use]
216
232
> fn five () -> i32 { 5i32 }
217
233
>
218
- > fn main () {
219
- > // None of these violate the unused_must_use lint.
220
- > (five (),);
221
- > Some (five ());
222
- > { five () };
223
- > if true { five () } else { 0i32 };
224
- > match true {
225
- > _ => five ()
226
- > };
227
- > }
234
+ > // None of these violate the unused_must_use lint.
235
+ > (five (),);
236
+ > Some (five ());
237
+ > { five () };
238
+ > if true { five () } else { 0i32 };
239
+ > match true {
240
+ > _ => five ()
241
+ > };
228
242
> ```
229
243
230
244
> Note : It is idiomatic to use a [let statement] with a pattern of `_`
@@ -234,16 +248,10 @@ When used on a function in a trait implementation, the attribute does nothing.
234
248
> #[must_use]
235
249
> fn five() -> i32 { 5i32 }
236
250
>
237
- > fn main() {
238
- > // Does not violate the unused_must_use lint.
239
- > let _ = five();
240
- > }
251
+ > // Does not violate the unused_must_use lint.
252
+ > let _ = five();
241
253
> ```
242
254
243
- The `must_use ` attribute may include a message by using the
244
- [_MetaNameValueStr_ ] syntax such as `#[must_use = " example message" ]`. The
245
- message will be given alongside the warning .
246
-
247
255
[Clippy ]: https : // github.com/rust-lang/rust-clippy
248
256
[_MetaListNameValueStr_ ]: attributes . html#meta - item - attribute - syntax
249
257
[_MetaListPaths_ ]: attributes . html#meta - item - attribute - syntax
@@ -258,13 +266,15 @@ message will be given alongside the warning.
258
266
[expression ]: expressions . html
259
267
[external block item ]: items / external - blocks . html
260
268
[functions ]: items / functions . html
269
+ [impl trait ]: types / impl - trait . html
261
270
[implementation ]: items / implementations . html
262
271
[item ]: items . html
263
272
[let statement ]: statements . html#let - statements
264
273
[module ]: items / modules . html
265
274
[rustc book ]: .. / rustc / lints / index . html
266
275
[struct field ]: items / structs . html
267
276
[struct ]: items / structs . html
277
+ [trait declaration ]: items / traits . html
268
278
[trait implementation items ]: items / implementations . html#trait - implementations
269
279
[trait item ]: items / traits . html
270
280
[traits ]: items / traits . html
0 commit comments