1
1
# Diagnostic attributes
2
2
3
+ r[ attributes.diagnostics]
4
+
3
5
The following [ attributes] are used for controlling or generating diagnostic
4
6
messages during compilation.
5
7
6
8
## Lint check attributes
7
9
10
+ r[ attributes.diagnostics.lints]
11
+
8
12
A lint check names a potentially undesirable coding pattern, such as
9
- unreachable code or omitted documentation. The lint attributes ` allow ` ,
13
+ unreachable code or omitted documentation.
14
+
15
+ r[ attributes.diagnostics.lints.level]
16
+ The lint attributes ` allow ` ,
10
17
` expect ` , ` warn ` , ` deny ` , and ` forbid ` use the [ _ MetaListPaths_ ] syntax
11
18
to specify a list of lint names to change the lint level for the entity
12
19
to which the attribute applies.
13
20
14
21
For any lint check ` C ` :
15
22
23
+ r[ attributes.diagnostics.lints.allow]
16
24
* ` #[allow(C)] ` overrides the check for ` C ` so that violations will go
17
25
unreported.
26
+
27
+ r[ attributes.diagnostics.lints.expect]
18
28
* ` #[expect(C)] ` indicates that lint ` C ` is expected to be emitted. The
19
29
attribute will suppress the emission of ` C ` or issue a warning, if the
20
30
expectation is unfulfilled.
31
+
32
+ r[ attributes.diagnostics.lints.warn]
21
33
* ` #[warn(C)] ` warns about violations of ` C ` but continues compilation.
34
+
35
+ r[ attributes.diagnostics.lints.deny]
22
36
* ` #[deny(C)] ` signals an error after encountering a violation of ` C ` ,
37
+
38
+ r[ attributes.diagnostics.lints.forbid]
23
39
* ` #[forbid(C)] ` is the same as ` deny(C) ` , but also forbids changing the lint
24
40
level afterwards,
25
41
@@ -42,6 +58,7 @@ pub mod m1 {
42
58
}
43
59
```
44
60
61
+ r[ attributes.diagnostics.lints.override]
45
62
Lint attributes can override the level specified from a previous attribute, as
46
63
long as the level does not attempt to change a forbidden lint
47
64
(except for ` deny ` , which is allowed inside a ` forbid ` context, but ignored).
@@ -89,6 +106,7 @@ pub mod m3 {
89
106
90
107
### Lint Reasons
91
108
109
+ r[ attributes.diagnostics.lints.reason]
92
110
All lint attributes support an additional ` reason ` parameter, to give context why
93
111
a certain attribute was added. This reason will be displayed as part of the lint
94
112
message if the lint is emitted at the defined level.
@@ -125,6 +143,9 @@ pub fn get_path() -> PathBuf {
125
143
126
144
### The ` #[expect] ` attribute
127
145
146
+ r[ attributes.diagnostics.expect]
147
+
148
+ r[ attributes.diagnostics.expect.general]
128
149
The ` #[expect(C)] ` attribute creates a lint expectation for lint ` C ` . The
129
150
expectation will be fulfilled, if a ` #[warn(C)] ` attribute at the same location
130
151
would result in a lint emission. If the expectation is unfulfilled, because
@@ -150,6 +171,7 @@ fn main() {
150
171
}
151
172
```
152
173
174
+ r[ attributes.lints.expect.fulfilment]
153
175
The lint expectation is only fulfilled by lint emissions which have been suppressed by
154
176
the ` expect ` attribute. If the lint level is modified in the scope with other level
155
177
attributes like ` allow ` or ` warn ` , the lint emission will be handled accordingly and the
@@ -179,6 +201,7 @@ fn select_song() {
179
201
}
180
202
```
181
203
204
+ r[ attributes.diagnostics.expect.independent]
182
205
If the ` expect ` attribute contains several lints, each one is expected separately. For a
183
206
lint group it's enough if one lint inside the group has been emitted:
184
207
@@ -207,6 +230,7 @@ pub fn another_example() {
207
230
208
231
### Lint groups
209
232
233
+ r[ attributes.lints.groups]
210
234
Lints may be organized into named groups so that the level of related lints
211
235
can be adjusted together. Using a named group is equivalent to listing out the
212
236
lints within that group.
@@ -227,6 +251,7 @@ fn example() {
227
251
}
228
252
```
229
253
254
+ r[ attributes.lints.warnings-group]
230
255
There is a special group named "warnings" which includes all lints at the
231
256
"warn" level. The "warnings" group ignores attribute order and applies to all
232
257
lints that would otherwise warn within the entity.
@@ -246,9 +271,13 @@ fn example_err() {
246
271
247
272
### Tool lint attributes
248
273
274
+ r[ attributes.lints.tools]
275
+
276
+ r[ attributes.lints.tools.general]
249
277
Tool lints allows using scoped lints, to ` allow ` , ` warn ` , ` deny ` or ` forbid `
250
278
lints of certain tools.
251
279
280
+ r[ attributes.lints.tools.activation]
252
281
Tool lints only get checked when the associated tool is active. If a lint
253
282
attribute, such as ` allow ` , references a nonexistent tool lint, the compiler
254
283
will not warn about the nonexistent lint until you use the tool.
@@ -276,10 +305,14 @@ fn foo() {
276
305
277
306
## The ` deprecated ` attribute
278
307
308
+ r[ attributes.diagnostics.deprecated]
309
+
310
+ r[ attributes.diagnostics.deprecated.general]
279
311
The * ` deprecated ` attribute* marks an item as deprecated. ` rustc ` will issue
280
312
warnings on usage of ` #[deprecated] ` items. ` rustdoc ` will show item
281
313
deprecation, including the ` since ` version and ` note ` , if available.
282
314
315
+ r[ attributes.diagnostics.deprectead.syntax]
283
316
The ` deprecated ` attribute has several forms:
284
317
285
318
- ` deprecated ` --- Issues a generic message.
@@ -293,6 +326,7 @@ The `deprecated` attribute has several forms:
293
326
message. This is typically used to provide an explanation about the
294
327
deprecation and preferred alternatives.
295
328
329
+ r[ attributes.diagnostics.deprecated.application]
296
330
The ` deprecated ` attribute may be applied to any [ item] , [ trait item] , [ enum
297
331
variant] , [ struct field] , [ external block item] , or [ macro definition] . It
298
332
cannot be applied to [ trait implementation items] . When applied to an item
@@ -318,15 +352,20 @@ The [RFC][1270-deprecation.md] contains motivations and more details.
318
352
319
353
## The ` must_use ` attribute
320
354
355
+ r[ attributes.diagnostics.must_use]
356
+
357
+ r[ attributes.diagnostics.must_use.application]
321
358
The * ` must_use ` attribute* is used to issue a diagnostic warning when a value
322
359
is not "used". It can be applied to user-defined composite types
323
360
([ ` struct ` s] [ struct ] , [ ` enum ` s] [ enum ] , and [ ` union ` s] [ union ] ), [ functions] ,
324
361
and [ traits] .
325
362
363
+ r[ attributes.diagnostics.must_use.syntax]
326
364
The ` must_use ` attribute may include a message by using the
327
365
[ _ MetaNameValueStr_ ] syntax such as ` #[must_use = "example message"] ` . The
328
366
message will be given alongside the warning.
329
367
368
+ r[ attributes.diagnostics.must_use.types]
330
369
When used on user-defined composite types, if the [ expression] of an
331
370
[ expression statement] has that type, then the ` unused_must_use ` lint is
332
371
violated.
@@ -345,6 +384,7 @@ struct MustUse {
345
384
MustUse :: new ();
346
385
```
347
386
387
+ r[ attributes.diagnostics.must_use.function]
348
388
When used on a function, if the [ expression] of an [ expression statement] is a
349
389
[ call expression] to that function, then the ` unused_must_use ` lint is
350
390
violated.
@@ -357,6 +397,7 @@ fn five() -> i32 { 5i32 }
357
397
five ();
358
398
```
359
399
400
+ r[ attributes.diagnostics.must_use.traits]
360
401
When used on a [ trait declaration] , a [ call expression] of an [ expression
361
402
statement] to a function that returns an [ impl trait] or a [ dyn trait] of that trait violates
362
403
the ` unused_must_use ` lint.
@@ -374,6 +415,7 @@ fn get_critical() -> impl Critical {
374
415
get_critical ();
375
416
```
376
417
418
+ r[ attributes.diagnostics.must_use.trait-function]
377
419
When used on a function in a trait declaration, then the behavior also applies
378
420
when the call expression is a function from an implementation of the trait.
379
421
@@ -391,6 +433,7 @@ impl Trait for i32 {
391
433
5i32 . use_me ();
392
434
```
393
435
436
+ r[ attributes.diagnostics.must_use.impl-function]
394
437
When used on a function in a trait implementation, the attribute does nothing.
395
438
396
439
> Note: Trivial no-op expressions containing the value will not violate the
@@ -425,36 +468,59 @@ When used on a function in a trait implementation, the attribute does nothing.
425
468
426
469
## The `diagnostic ` tool attribute namespace
427
470
471
+ r [attributes . diagnostics. namespace]
472
+
473
+ r [attributes . diagnostics. namespace. general]
428
474
The `#[diagnostic]` attribute namespace is a home for attributes to influence compile - time error messages .
429
475
The hints provided by these attributes are not guaranteed to be used .
476
+
477
+ r [attributes . diagnostics. namespace. unknown- invalid - syntax ]
430
478
Unknown attributes in this namespace are accepted , though they may emit warnings for unused attributes .
431
479
Additionally , invalid inputs to known attributes will typically be a warning (see the attribute definitions for details ).
432
480
This is meant to allow adding or discarding attributes and changing inputs in the future to allow changes without the need to keep the non - meaningful attributes or options working .
433
481
434
482
### The `diagnostic :: on_unimplemented ` attribute
435
483
484
+ r [attributes . diagnostics. on_unimplemented]
485
+
486
+ r [attributes . diagnostics. on_unimplemented. general]
436
487
The `#[diagnostic:: on_unimplemented]` attribute is a hint to the compiler to supplement the error message that would normally be generated in scenarios where a trait is required but not implemented on a type .
488
+
489
+ r [attributes . diagnostics. on_unimplemented. restriction]
437
490
The attribute should be placed on a [trait declaration ], though it is not an error to be located in other positions .
491
+
492
+ r [attributes . diagnostics. on_unimplemented. syntax]
438
493
The attribute uses the [_MetaListNameValueStr_ ] syntax to specify its inputs , though any malformed input to the attribute is not considered as an error to provide both forwards and backwards compatibility .
439
- The following keys have the given meaning :
440
494
495
+
496
+ r [attributes . diagnostics. on_unimplemented. keys]
497
+ The following keys have the given meaning :
441
498
* `message ` --- The text for the top level error message .
442
499
* `label ` --- The text for the label shown inline in the broken code in the error message .
443
500
* `note ` --- Provides additional notes .
444
501
502
+ r [attributes . diagnostics. on_unimplemented- note - repetition ]
445
503
The `note ` option can appear several times , which results in several note messages being emitted .
504
+
505
+ r [attributes . diagnostics. on_unimplemented. repetition]
446
506
If any of the other options appears several times the first occurrence of the relevant option specifies the actually used value .
507
+
508
+ r [attributes . diagnostics. on_unimplemented. warnings]
447
509
Any other occurrence generates an lint warning .
448
510
For any other non - existing option a lint - warning is generated .
449
511
512
+ r [attributes . diagnostics. format- string ]
450
513
All three options accept a string as an argument , interpreted using the same formatting as a [`std :: fmt `] string .
451
- Format parameters with the given named parameter will be replaced with the following text :
452
514
515
+ r [attributes . diagnostics. format- parameters ]
516
+ Format parameters with the given named parameter will be replaced with the following text :
453
517
* `{Self }` --- The name of the type implementing the trait .
454
518
* `{` * GenericParameterName * `}` --- The name of the generic argument 's type for the given generic parameter .
455
519
520
+ r [attributes . diagnostics. invalid- formats ]
456
521
Any other format parameter will generate a warning , but will otherwise be included in the string as - is .
457
522
523
+ r [attributes . diagnostics. invalid- string ]
458
524
Invalid format strings may generate a warning , but are otherwise allowed , but may not display as intended .
459
525
Format specifiers may generate a warning , but are otherwise ignored .
460
526
0 commit comments