core/attribute_docs.rs
1#[doc(attribute = "must_use")]
2//
3/// Warn when a value is ignored.
4///
5/// The `must_use` attribute applies to values where simply creating or returning them is
6/// often not enough. If a value marked with `#[must_use]` is produced and then ignored, the
7/// compiler warns through the [`unused_must_use`] lint.
8///
9/// This is most common on types that represent an important state or outcome. For example,
10/// [`Result`] is marked `#[must_use]` because ignoring an error value can hide a failed operation.
11/// In the following example, the returned `Result` is the only sign that writing the message
12/// might have failed:
13///
14/// ```rust
15/// # #![allow(unused_must_use)]
16/// fn write_message() -> std::io::Result<()> {
17/// // Write the message...
18/// Ok(())
19/// }
20///
21/// write_message();
22/// ```
23///
24/// Ignoring that `Result` triggers this warning:
25///
26/// ```text
27/// warning: unused `Result` that must be used
28/// = note: this `Result` may be an `Err` variant, which should be handled
29/// = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default
30/// help: use `let _ = ...` to ignore the resulting value
31/// ```
32///
33/// Future values are also `#[must_use]`: creating a future does not run it, so ignoring one often
34/// means the intended asynchronous work never happens.
35///
36/// You can also place `#[must_use]` on a function, method, or trait declaration. On a function or
37/// method, the warning is tied to ignoring that call's return value:
38///
39/// ```rust
40/// # #![allow(unused_must_use)]
41/// #[must_use]
42/// fn make_token() -> String {
43/// String::from("token")
44/// }
45///
46/// // Ignoring this call's return value triggers `unused_must_use`.
47/// make_token();
48/// ```
49///
50/// On a trait, the warning applies when a function returns an opaque type (`impl Trait`) or trait
51/// object (`dyn Trait`) whose bounds include that trait. This is how futures warn if you create one
52/// but never poll or await it, since an `async fn` returns an opaque type implementing [`Future`].
53///
54/// The attribute can include a message explaining what the caller should do with the value:
55///
56/// ```rust
57/// # #![allow(dead_code)]
58/// #[must_use = "call `.finish()` to complete the operation"]
59/// fn start_operation() -> Operation {
60/// Operation
61/// }
62///
63/// struct Operation;
64/// ```
65///
66/// If intentionally ignoring the value is correct, bind it to `_` or call [`drop`]:
67///
68/// ```rust
69/// # #[must_use]
70/// # fn make_token() -> String {
71/// # String::from("token")
72/// # }
73/// let _ = make_token();
74/// drop(make_token());
75/// ```
76///
77/// The attribute is a warning tool, not a type-system rule. Code can still explicitly discard a
78/// `#[must_use]` value, and the compiler does not require callers to inspect or otherwise act on
79/// the value.
80///
81/// For more information, see the Reference on [the `must_use` attribute].
82///
83/// [`Result`]: result::Result
84/// [`Future`]: future::Future
85/// [`unused_must_use`]: ../rustc/lints/listing/warn-by-default.html#unused-must-use
86/// [the `must_use` attribute]: ../reference/attributes/diagnostics.html#the-must_use-attribute
87mod must_use_attribute {}
88
89#[doc(attribute = "allow")]
90//
91/// The `allow` attribute suppresses lint diagnostics that would otherwise produce
92/// warnings or errors. It can be used on any lint or lint group (except those
93/// set to `forbid`).
94///
95/// ```rust
96/// #[allow(dead_code)]
97/// fn unused_function() {
98/// // ...
99/// }
100///
101/// fn main() {
102/// // `unused_function` does not generate a compiler warning.
103/// }
104/// ```
105///
106/// Without `#[allow(dead_code)]`, the example above would emit:
107///
108/// ```text
109/// warning: function `unused_function` is never used
110/// --> main.rs:1:4
111/// |
112/// 1 | fn unused_function() {
113/// | ^^^^^^^^^^^^^^^
114/// |
115/// = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
116///
117/// warning: 1 warning emitted
118/// ```
119///
120/// Multiple lints can be set to `allow` at once with commas:
121///
122/// ```rust
123/// #[allow(unused_variables, unused_mut)]
124/// fn main() {
125/// let mut x: u32 = 42;
126/// }
127/// ```
128///
129/// This is mostly used to prevent lint warnings or errors while still under development.
130///
131/// It cannot override a lint that has been set to `forbid`.
132///
133/// It's also important to consider that overusing `allow` could make code harder to maintain
134/// and possibly hide issues. To mitigate this issue, using the `expect` attribute is preferred.
135///
136/// `allow` can be overridden by `warn`, `deny`, and `forbid`.
137///
138/// The lint checks supported by rustc can be found via `rustc -W help`,
139/// along with their default settings and are documented in [the `rustc` book].
140///
141/// [the `rustc` book]: ../rustc/lints/listing/index.html
142///
143/// For more information, see the Reference on [the `allow` attribute].
144///
145/// [the `allow` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
146mod allow_attribute {}
147
148#[doc(attribute = "cfg")]
149//
150/// Used for conditional compilation.
151///
152/// The `cfg` attribute allows compiling an item under specific conditions, otherwise it
153/// will be ignored.
154///
155/// ```rust
156/// // Only compiles this function for Linux.
157/// #[cfg(target_os = "linux")]
158/// fn platform_specific() {
159/// println!("Running on Linux");
160/// }
161///
162/// // Only compiles this function if not for Linux.
163/// #[cfg(not(target_os = "linux"))]
164/// fn platform_specific() {
165/// println!("Running on something else");
166/// }
167/// ```
168///
169/// Depending on the platform you're targeting, only one of these two functions will be considered
170/// during the compilation.
171///
172/// Conditions can also be combined with `all(...)`, `any(...)`, and `not(...)`.
173///
174/// * `all`: True if all given predicates are true.
175/// * `any`: True if at least one of the given predicates is true.
176/// * `not`: True if the predicate is false and false if the predicate is true.
177///
178/// ```rust
179/// #[cfg(all(unix, target_pointer_width = "64"))]
180/// fn unix_64bit() {
181/// }
182/// ```
183///
184/// If you want to use this mechanism in an `if` condition in your code, you
185/// can use the [`cfg!`] macro. To conditionally apply an attribute,
186/// see [`cfg_attr`].
187///
188/// For more information, see the Reference on [the `cfg` attribute].
189///
190/// [`cfg_attr`]: ../reference/conditional-compilation.html#the-cfg_attr-attribute
191/// [the `cfg` attribute]: ../reference/conditional-compilation.html#the-cfg-attribute
192mod cfg_attribute {}
193
194#[doc(attribute = "deny")]
195//
196/// Emits an error, preventing the compilation from finishing, when a lint check has failed.
197/// This is useful for enforcing rules or preventing certain patterns:
198///
199/// ```rust,compile_fail
200/// #[deny(unused)]
201/// fn foo() {
202/// let x = 42; // Emits an error because x is unused.
203/// }
204/// ```
205///
206/// `deny` can be overridden by `allow`, `warn`, and `forbid`:
207///
208/// ```rust
209/// #![deny(unused)]
210///
211/// #[allow(unused)] // We override the `deny` for this function.
212/// fn foo() {
213/// let x = 42; // No lint emitted even though `x` is unused.
214/// }
215/// ```
216///
217/// Multiple lints can also be set to `deny` at once:
218///
219/// ```rust,compile_fail
220/// #![deny(unused_imports, unused_variables)]
221/// use std::collections::*;
222///
223/// fn main() {
224/// let mut x = 10;
225/// }
226/// ```
227///
228/// The lint checks supported by rustc can be found via `rustc -W help`,
229/// along with their default settings and are documented in [the `rustc` book].
230///
231/// [the `rustc` book]: ../rustc/lints/listing/index.html
232///
233/// For more information, see the Reference on [the `deny` attribute].
234///
235/// [the `deny` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
236mod deny_attribute {}
237
238#[doc(attribute = "forbid")]
239//
240/// Emits an error, preventing the compilation from finishing, when a lint check has failed.
241///
242/// A lint set to `forbid` cannot be overridden by `allow` or `warn`.
243/// Attempting either will result in a compilation error. Writing `#[deny(...)]` on the same lint inside a
244/// `forbid` scope is permitted, but has no effect; the lint remains at the `forbid` level.
245///
246/// This is useful for enforcing strict policies that should not be relaxed
247/// anywhere in the codebase. Example:
248///
249/// ```rust
250/// #![forbid(unsafe_code)]
251///
252/// // This would cause a compilation error if uncommented:
253/// // #[allow(unsafe_code)] // error: cannot override `forbid`
254/// ```
255///
256/// Multiple lints can be set to `forbid` at once:
257///
258/// ```rust
259/// #![forbid(unsafe_code, unused)]
260/// ```
261///
262/// The lint checks supported by rustc can be found via `rustc -W help`,
263/// along with their default settings and are documented in [the `rustc` book].
264///
265/// [the `rustc` book]: ../rustc/lints/listing/index.html
266///
267/// For more information, see the Reference on [the `forbid` attribute].
268///
269/// [the `forbid` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
270mod forbid_attribute {}
271
272#[doc(attribute = "deprecated")]
273//
274/// Emits a warning during compilation when an item with this attribute is used.
275/// `since` and `note` are optional fields giving more detail about why the item is deprecated.
276///
277/// * `since`: the version since when the item is deprecated.
278/// * `note`: the reason why an item is deprecated.
279///
280/// Example:
281///
282/// ```rust
283/// #[deprecated(since = "1.0.0", note = "Use bar instead")]
284/// struct Foo;
285/// struct Bar;
286/// ```
287///
288/// `deprecated` attribute helps developers transition away from old code by providing warnings when
289/// deprecated items are used. Note that during `Cargo` builds, warnings on dependencies get silenced
290/// by default, so you may not see a deprecation warning unless you build that dependency directly.
291///
292/// For more information, see the Reference on [the `deprecated` attribute].
293///
294/// [the `deprecated` attribute]: ../reference/attributes/diagnostics.html#the-deprecated-attribute
295mod deprecated_attribute {}
296
297#[doc(attribute = "warn")]
298//
299/// Emits a warning during compilation when a lint check failed.
300///
301/// Unlike `deny` or `forbid`, `warn` does not produce a hard error: the compilation continues, but
302/// the compiler emits a warning message. `warn` can be overridden by `allow`, `deny`, and `forbid`.
303///
304/// Example:
305///
306/// ```rust,compile_fail
307/// #![allow(unused)]
308///
309/// #[warn(unused)] // We override the allowed `unused` lint.
310/// fn foo() {
311/// // This lint warns by default even without #[warn(unused)] being explicitly set
312/// let x = 42; // warning: unused variable `x`
313/// }
314/// ```
315///
316///
317/// Many lints, including `unused`, are already set to `warn` by default so this attribute is
318/// mainly useful for lints that are normally `allow` by default.
319///
320/// Multiple lints can be set to `warn` at once:
321///
322/// ```rust,compile_fail
323/// #[warn(unused_mut, unused_variables)]
324/// fn main() {
325/// let mut x = 42;
326/// }
327/// ```
328///
329/// The lint checks supported by rustc can be found via `rustc -W help`,
330/// along with their default settings and are documented in [the `rustc` book].
331///
332/// [the `rustc` book]: ../rustc/lints/listing/index.html
333///
334/// For more information, see the Reference on [the `warn` attribute].
335///
336/// [the `warn` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
337mod warn_attribute {}