1 # Diagnostic attributes
3 The following [attributes] are used for controlling or generating diagnostic
4 messages during compilation.
6 ## Lint check attributes
8 A lint check names a potentially undesirable coding pattern, such as
9 unreachable code or omitted documentation. The lint attributes `allow`,
10 `warn`, `deny`, and `forbid` use the [_MetaListPaths_] syntax to specify a
11 list of lint names to change the lint level for the entity to which the
14 For any lint check `C`:
16 * `allow(C)` overrides the check for `C` so that violations will go
18 * `warn(C)` warns about violations of `C` but continues compilation.
19 * `deny(C)` signals an error after encountering a violation of `C`,
20 * `forbid(C)` is the same as `deny(C)`, but also forbids changing the lint
23 > Note: The lint checks supported by `rustc` can be found via `rustc -W help`,
24 > along with their default settings and are documented in the [rustc book].
28 // Missing documentation is ignored here
29 #[allow(missing_docs)]
30 pub fn undocumented_one() -> i32 { 1 }
32 // Missing documentation signals a warning here
34 pub fn undocumented_too() -> i32 { 2 }
36 // Missing documentation signals an error here
38 pub fn undocumented_end() -> i32 { 3 }
42 Lint attributes can override the level specified from a previous attribute, as
43 long as the level does not attempt to change a forbidden lint. Previous
44 attributes are those from a higher level in the syntax tree, or from a
45 previous attribute on the same entity as listed in left-to-right source order.
47 This example shows how one can use `allow` and `warn` to toggle a particular
53 #[allow(missing_docs)]
55 // Missing documentation is ignored here
56 pub fn undocumented_one() -> i32 { 1 }
58 // Missing documentation signals a warning here,
59 // despite the allow above.
61 pub fn undocumented_two() -> i32 { 2 }
64 // Missing documentation signals a warning here
65 pub fn undocumented_too() -> i32 { 3 }
69 This example shows how one can use `forbid` to disallow uses of `allow` for
73 #[forbid(missing_docs)]
75 // Attempting to toggle warning signals an error here
76 #[allow(missing_docs)]
78 pub fn undocumented_too() -> i32 { 2 }
82 > Note: `rustc` allows setting lint levels on the
83 > [command-line][rustc-lint-cli], and also supports [setting
84 > caps][rustc-lint-caps] on the lints that are reported.
88 Lints may be organized into named groups so that the level of related lints
89 can be adjusted together. Using a named group is equivalent to listing out the
90 lints within that group.
93 // This allows all lints in the "unused" group.
95 // This overrides the "unused_must_use" lint from the "unused"
97 #[deny(unused_must_use)]
99 // This does not generate a warning because the "unused_variables"
100 // lint is in the "unused" group.
102 // This generates an error because the result is unused and
103 // "unused_must_use" is marked as "deny".
104 std::fs::remove_file("some_file"); // ERROR: unused `Result` that must be used
108 There is a special group named "warnings" which includes all lints at the
109 "warn" level. The "warnings" group ignores attribute order and applies to all
110 lints that would otherwise warn within the entity.
113 # unsafe fn an_unsafe_fn() {}
114 // The order of these two attributes does not matter.
116 // The unsafe_code lint is normally "allow" by default.
119 // This is an error because the `unsafe_code` warning has
120 // been lifted to "deny".
121 unsafe { an_unsafe_fn() } // ERROR: usage of `unsafe` block
125 ### Tool lint attributes
127 Tool lints allows using scoped lints, to `allow`, `warn`, `deny` or `forbid`
128 lints of certain tools.
130 Tool lints only get checked when the associated tool is active. If a lint
131 attribute, such as `allow`, references a nonexistent tool lint, the compiler
132 will not warn about the nonexistent lint until you use the tool.
134 Otherwise, they work just like regular lint attributes:
137 // set the entire `pedantic` clippy lint group to warn
138 #![warn(clippy::pedantic)]
139 // silence warnings from the `filter_map` clippy lint
140 #![allow(clippy::filter_map)]
146 // silence the `cmp_nan` clippy lint just for this function
147 #[allow(clippy::cmp_nan)]
153 > Note: `rustc` currently recognizes the tool lints for "[clippy]" and "[rustdoc]".
155 ## The `deprecated` attribute
157 The *`deprecated` attribute* marks an item as deprecated. `rustc` will issue
158 warnings on usage of `#[deprecated]` items. `rustdoc` will show item
159 deprecation, including the `since` version and `note`, if available.
161 The `deprecated` attribute has several forms:
163 - `deprecated` — Issues a generic message.
164 - `deprecated = "message"` — Includes the given string in the deprecation
166 - [_MetaListNameValueStr_] syntax with two optional fields:
167 - `since` — Specifies a version number when the item was deprecated. `rustc`
168 does not currently interpret the string, but external tools like [Clippy]
169 may check the validity of the value.
170 - `note` — Specifies a string that should be included in the deprecation
171 message. This is typically used to provide an explanation about the
172 deprecation and preferred alternatives.
174 The `deprecated` attribute may be applied to any [item], [trait item], [enum
175 variant], [struct field], [external block item], or [macro definition]. It
176 cannot be applied to [trait implementation items]. When applied to an item
177 containing other items, such as a [module] or [implementation], all child
178 items inherit the deprecation attribute.
179 <!-- NOTE: It is only rejected for trait impl items
180 (AnnotationKind::Prohibited). In all other locations, it is silently ignored.
181 Tuple struct fields are ignored.
187 #[deprecated(since = "5.2.0", note = "foo was rarely used. Users should instead use bar")]
193 The [RFC][1270-deprecation.md] contains motivations and more details.
195 [1270-deprecation.md]: https://github.com/rust-lang/rfcs/blob/master/text/1270-deprecation.md
197 ## The `must_use` attribute
199 The *`must_use` attribute* is used to issue a diagnostic warning when a value
200 is not "used". It can be applied to user-defined composite types
201 ([`struct`s][struct], [`enum`s][enum], and [`union`s][union]), [functions],
204 The `must_use` attribute may include a message by using the
205 [_MetaNameValueStr_] syntax such as `#[must_use = "example message"]`. The
206 message will be given alongside the warning.
208 When used on user-defined composite types, if the [expression] of an
209 [expression statement] has that type, then the `unused_must_use` lint is
219 # fn new() -> MustUse { MustUse {} }
222 // Violates the `unused_must_use` lint.
226 When used on a function, if the [expression] of an [expression statement] is a
227 [call expression] to that function, then the `unused_must_use` lint is
232 fn five() -> i32 { 5i32 }
234 // Violates the unused_must_use lint.
238 When used on a [trait declaration], a [call expression] of an [expression
239 statement] to a function that returns an [impl trait] or a [dyn trait] of that trait violates
240 the `unused_must_use` lint.
245 impl Critical for i32 {}
247 fn get_critical() -> impl Critical {
251 // Violates the `unused_must_use` lint.
255 When used on a function in a trait declaration, then the behavior also applies
256 when the call expression is a function from an implementation of the trait.
261 fn use_me(&self) -> i32;
265 fn use_me(&self) -> i32 { 0i32 }
268 // Violates the `unused_must_use` lint.
272 When used on a function in a trait implementation, the attribute does nothing.
274 > Note: Trivial no-op expressions containing the value will not violate the
275 > lint. Examples include wrapping the value in a type that does not implement
276 > [`Drop`] and then not using that type and being the final expression of a
277 > [block expression] that is not used.
281 > fn five() -> i32 { 5i32 }
283 > // None of these violate the unused_must_use lint.
287 > if true { five() } else { 0i32 };
293 > Note: It is idiomatic to use a [let statement] with a pattern of `_`
294 > when a must-used value is purposely discarded.
298 > fn five() -> i32 { 5i32 }
300 > // Does not violate the unused_must_use lint.
304 [Clippy]: https://github.com/rust-lang/rust-clippy
305 [_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
306 [_MetaListPaths_]: ../attributes.md#meta-item-attribute-syntax
307 [_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
308 [`Drop`]: ../special-types-and-traits.md#drop
309 [attributes]: ../attributes.md
310 [block expression]: ../expressions/block-expr.md
311 [call expression]: ../expressions/call-expr.md
312 [dyn trait]: ../types/trait-object.md
313 [enum variant]: ../items/enumerations.md
314 [enum]: ../items/enumerations.md
315 [expression statement]: ../statements.md#expression-statements
316 [expression]: ../expressions.md
317 [external block item]: ../items/external-blocks.md
318 [functions]: ../items/functions.md
319 [impl trait]: ../types/impl-trait.md
320 [implementation]: ../items/implementations.md
322 [let statement]: ../statements.md#let-statements
323 [macro definition]: ../macros-by-example.md
324 [module]: ../items/modules.md
325 [rustc book]: ../../rustc/lints/index.html
326 [rustc-lint-caps]: ../../rustc/lints/levels.html#capping-lints
327 [rustc-lint-cli]: ../../rustc/lints/levels.html#via-compiler-flag
328 [rustdoc]: ../../rustdoc/lints.html
329 [struct field]: ../items/structs.md
330 [struct]: ../items/structs.md
331 [trait declaration]: ../items/traits.md
332 [trait implementation items]: ../items/implementations.md#trait-implementations
333 [trait item]: ../items/traits.md
334 [traits]: ../items/traits.md
335 [union]: ../items/unions.md