11# Diagnostic attributes
22
3+ r[ attributes.diagnostics]
4+
35The following [ attributes] are used for controlling or generating diagnostic
46messages during compilation.
57
68## Lint check attributes
79
10+ r[ attributes.diagnostics.lints]
11+
812A 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 ` ,
1017` expect ` , ` warn ` , ` deny ` , and ` forbid ` use the [ _ MetaListPaths_ ] syntax
1118to specify a list of lint names to change the lint level for the entity
1219to which the attribute applies.
1320
1421For any lint check ` C ` :
1522
23+ r[ attributes.diagnostics.lints.allow]
1624* ` #[allow(C)] ` overrides the check for ` C ` so that violations will go
1725 unreported.
26+
27+ r[ attributes.diagnostics.lints.expect]
1828* ` #[expect(C)] ` indicates that lint ` C ` is expected to be emitted. The
1929 attribute will suppress the emission of ` C ` or issue a warning, if the
2030 expectation is unfulfilled.
31+
32+ r[ attributes.diagnostics.lints.warn]
2133* ` #[warn(C)] ` warns about violations of ` C ` but continues compilation.
34+
35+ r[ attributes.diagnostics.lints.deny]
2236* ` #[deny(C)] ` signals an error after encountering a violation of ` C ` ,
37+
38+ r[ attributes.diagnostics.lints.forbid]
2339* ` #[forbid(C)] ` is the same as ` deny(C) ` , but also forbids changing the lint
2440 level afterwards,
2541
@@ -42,6 +58,7 @@ pub mod m1 {
4258}
4359```
4460
61+ r[ attributes.diagnostics.lints.override]
4562Lint attributes can override the level specified from a previous attribute, as
4663long as the level does not attempt to change a forbidden lint
4764(except for ` deny ` , which is allowed inside a ` forbid ` context, but ignored).
@@ -89,6 +106,7 @@ pub mod m3 {
89106
90107### Lint Reasons
91108
109+ r[ attributes.diagnostics.lints.reason]
92110All lint attributes support an additional ` reason ` parameter, to give context why
93111a certain attribute was added. This reason will be displayed as part of the lint
94112message if the lint is emitted at the defined level.
@@ -125,6 +143,9 @@ pub fn get_path() -> PathBuf {
125143
126144### The ` #[expect] ` attribute
127145
146+ r[ attributes.diagnostics.expect]
147+
148+ r[ attributes.diagnostics.expect.general]
128149The ` #[expect(C)] ` attribute creates a lint expectation for lint ` C ` . The
129150expectation will be fulfilled, if a ` #[warn(C)] ` attribute at the same location
130151would result in a lint emission. If the expectation is unfulfilled, because
@@ -150,6 +171,7 @@ fn main() {
150171}
151172```
152173
174+ r[ attributes.lints.expect.fulfilment]
153175The lint expectation is only fulfilled by lint emissions which have been suppressed by
154176the ` expect ` attribute. If the lint level is modified in the scope with other level
155177attributes like ` allow ` or ` warn ` , the lint emission will be handled accordingly and the
@@ -179,6 +201,7 @@ fn select_song() {
179201}
180202```
181203
204+ r[ attributes.diagnostics.expect.independent]
182205If the ` expect ` attribute contains several lints, each one is expected separately. For a
183206lint group it's enough if one lint inside the group has been emitted:
184207
@@ -207,6 +230,7 @@ pub fn another_example() {
207230
208231### Lint groups
209232
233+ r[ attributes.lints.groups]
210234Lints may be organized into named groups so that the level of related lints
211235can be adjusted together. Using a named group is equivalent to listing out the
212236lints within that group.
@@ -227,6 +251,7 @@ fn example() {
227251}
228252```
229253
254+ r[ attributes.lints.warnings-group]
230255There is a special group named "warnings" which includes all lints at the
231256"warn" level. The "warnings" group ignores attribute order and applies to all
232257lints that would otherwise warn within the entity.
@@ -246,9 +271,13 @@ fn example_err() {
246271
247272### Tool lint attributes
248273
274+ r[ attributes.lints.tools]
275+
276+ r[ attributes.lints.tools.general]
249277Tool lints allows using scoped lints, to ` allow ` , ` warn ` , ` deny ` or ` forbid `
250278lints of certain tools.
251279
280+ r[ attributes.lints.tools.activation]
252281Tool lints only get checked when the associated tool is active. If a lint
253282attribute, such as ` allow ` , references a nonexistent tool lint, the compiler
254283will not warn about the nonexistent lint until you use the tool.
@@ -276,10 +305,14 @@ fn foo() {
276305
277306## The ` deprecated ` attribute
278307
308+ r[ attributes.diagnostics.deprecated]
309+
310+ r[ attributes.diagnostics.deprecated.general]
279311The * ` deprecated ` attribute* marks an item as deprecated. ` rustc ` will issue
280312warnings on usage of ` #[deprecated] ` items. ` rustdoc ` will show item
281313deprecation, including the ` since ` version and ` note ` , if available.
282314
315+ r[ attributes.diagnostics.deprectead.syntax]
283316The ` deprecated ` attribute has several forms:
284317
285318- ` deprecated ` --- Issues a generic message.
@@ -293,6 +326,7 @@ The `deprecated` attribute has several forms:
293326 message. This is typically used to provide an explanation about the
294327 deprecation and preferred alternatives.
295328
329+ r[ attributes.diagnostics.deprecated.application]
296330The ` deprecated ` attribute may be applied to any [ item] , [ trait item] , [ enum
297331variant] , [ struct field] , [ external block item] , or [ macro definition] . It
298332cannot 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.
318352
319353## The ` must_use ` attribute
320354
355+ r[ attributes.diagnostics.must_use]
356+
357+ r[ attributes.diagnostics.must_use.application]
321358The * ` must_use ` attribute* is used to issue a diagnostic warning when a value
322359is not "used". It can be applied to user-defined composite types
323360([ ` struct ` s] [ struct ] , [ ` enum ` s] [ enum ] , and [ ` union ` s] [ union ] ), [ functions] ,
324361and [ traits] .
325362
363+ r[ attributes.diagnostics.must_use.syntax]
326364The ` must_use ` attribute may include a message by using the
327365[ _ MetaNameValueStr_ ] syntax such as ` #[must_use = "example message"] ` . The
328366message will be given alongside the warning.
329367
368+ r[ attributes.diagnostics.must_use.types]
330369When used on user-defined composite types, if the [ expression] of an
331370[ expression statement] has that type, then the ` unused_must_use ` lint is
332371violated.
@@ -345,6 +384,7 @@ struct MustUse {
345384MustUse :: new ();
346385```
347386
387+ r[ attributes.diagnostics.must_use.function]
348388When used on a function, if the [ expression] of an [ expression statement] is a
349389[ call expression] to that function, then the ` unused_must_use ` lint is
350390violated.
@@ -357,6 +397,7 @@ fn five() -> i32 { 5i32 }
357397five ();
358398```
359399
400+ r[ attributes.diagnostics.must_use.traits]
360401When used on a [ trait declaration] , a [ call expression] of an [ expression
361402statement] to a function that returns an [ impl trait] or a [ dyn trait] of that trait violates
362403the ` unused_must_use ` lint.
@@ -374,6 +415,7 @@ fn get_critical() -> impl Critical {
374415get_critical ();
375416```
376417
418+ r[ attributes.diagnostics.must_use.trait-function]
377419When used on a function in a trait declaration, then the behavior also applies
378420when the call expression is a function from an implementation of the trait.
379421
@@ -391,6 +433,7 @@ impl Trait for i32 {
3914335i32 . use_me ();
392434```
393435
436+ r[ attributes.diagnostics.must_use.impl-function]
394437When used on a function in a trait implementation, the attribute does nothing.
395438
396439> 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.
425468
426469## The `diagnostic ` tool attribute namespace
427470
471+ r [attributes . diagnostics. namespace]
472+
473+ r [attributes . diagnostics. namespace. general]
428474The `#[diagnostic]` attribute namespace is a home for attributes to influence compile - time error messages .
429475The hints provided by these attributes are not guaranteed to be used .
476+
477+ r [attributes . diagnostics. namespace. unknown- invalid - syntax ]
430478Unknown attributes in this namespace are accepted , though they may emit warnings for unused attributes .
431479Additionally , invalid inputs to known attributes will typically be a warning (see the attribute definitions for details ).
432480This 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 .
433481
434482### The `diagnostic :: on_unimplemented ` attribute
435483
484+ r [attributes . diagnostics. on_unimplemented]
485+
486+ r [attributes . diagnostics. on_unimplemented. general]
436487The `#[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]
437490The 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]
438493The 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 :
440494
495+
496+ r [attributes . diagnostics. on_unimplemented. keys]
497+ The following keys have the given meaning :
441498* `message ` --- The text for the top level error message .
442499* `label ` --- The text for the label shown inline in the broken code in the error message .
443500* `note ` --- Provides additional notes .
444501
502+ r [attributes . diagnostics. on_unimplemented- note - repetition ]
445503The `note ` option can appear several times , which results in several note messages being emitted .
504+
505+ r [attributes . diagnostics. on_unimplemented. repetition]
446506If 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]
447509Any other occurrence generates an lint warning .
448510For any other non - existing option a lint - warning is generated .
449511
512+ r [attributes . diagnostics. format- string ]
450513All 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 :
452514
515+ r [attributes . diagnostics. format- parameters ]
516+ Format parameters with the given named parameter will be replaced with the following text :
453517* `{Self }` --- The name of the type implementing the trait .
454518* `{` * GenericParameterName * `}` --- The name of the generic argument 's type for the given generic parameter .
455519
520+ r [attributes . diagnostics. invalid- formats ]
456521Any other format parameter will generate a warning , but will otherwise be included in the string as - is .
457522
523+ r [attributes . diagnostics. invalid- string ]
458524Invalid format strings may generate a warning , but are otherwise allowed , but may not display as intended .
459525Format specifiers may generate a warning , but are otherwise ignored .
460526
0 commit comments