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/// ```
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/// ```
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/// ```
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/// ```
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/// [`unused_must_use`]: ../rustc/lints/listing/warn-by-default.html#unused-must-use
84/// [the `must_use` attribute]: ../reference/attributes/diagnostics.html#the-must_use-attribute
85const _: () = ();
86
87#[doc(attribute = "allow")]
88//
89/// The `allow` attribute suppresses lint diagnostics that would otherwise produce
90/// warnings or errors. It can be used on any lint or lint group (except those
91/// set to [`forbid`]).
92///
93/// ```
94/// #[allow(dead_code)]
95/// fn unused_function() {
96/// // ...
97/// }
98///
99/// fn main() {
100/// // `unused_function` does not generate a compiler warning.
101/// }
102/// ```
103///
104/// Without `#[allow(dead_code)]`, the example above would emit:
105///
106/// ```text
107/// warning: function `unused_function` is never used
108/// --> main.rs:1:4
109/// |
110/// 1 | fn unused_function() {
111/// | ^^^^^^^^^^^^^^^
112/// |
113/// = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
114///
115/// warning: 1 warning emitted
116/// ```
117///
118/// Multiple lints can be set to `allow` at once with commas:
119///
120/// ```
121/// #[allow(unused_variables, unused_mut)]
122/// fn main() {
123/// let mut x: u32 = 42;
124/// }
125/// ```
126///
127/// This is mostly used to prevent lint warnings or errors while still under development.
128///
129/// It cannot override a lint that has been set to [`forbid`].
130///
131/// It's also important to consider that overusing `allow` could make code harder to maintain
132/// and possibly hide issues. To mitigate this issue, using the `expect` attribute is preferred.
133///
134/// `allow` can be overridden by [`warn`], [`deny`], and [`forbid`].
135///
136/// The lint checks supported by rustc can be found via `rustc -W help`,
137/// along with their default settings and are documented in [the `rustc` book].
138///
139/// [the `rustc` book]: ../rustc/lints/listing/index.html
140///
141/// For more information, see the Reference on [the `allow` attribute].
142///
143/// [the `allow` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
144/// [`forbid`]: ./attribute.forbid.html
145/// [`warn`]: ./attribute.warn.html
146/// [`deny`]: ./attribute.deny.html
147const _: () = ();
148
149#[doc(attribute = "cfg")]
150//
151/// Used for conditional compilation.
152///
153/// The `cfg` attribute allows compiling an item under specific conditions, otherwise it
154/// will be ignored.
155///
156/// ```
157/// // Only compiles this function for Linux.
158/// #[cfg(target_os = "linux")]
159/// fn platform_specific() {
160/// println!("Running on Linux");
161/// }
162///
163/// // Only compiles this function if not for Linux.
164/// #[cfg(not(target_os = "linux"))]
165/// fn platform_specific() {
166/// println!("Running on something else");
167/// }
168/// ```
169///
170/// Depending on the platform you're targeting, only one of these two functions will be considered
171/// during the compilation.
172///
173/// Conditions can also be combined with `all(...)`, `any(...)`, and `not(...)`:
174///
175/// * `all`: True if all given predicates are true.
176/// * `any`: True if at least one of the given predicates is true.
177/// * `not`: True if the predicate is false and false if the predicate is true.
178///
179/// ```
180/// #[cfg(all(unix, target_pointer_width = "64"))]
181/// fn unix_64bit() {
182/// // ...
183/// }
184/// ```
185///
186/// If you want to use this mechanism in an [`if`] condition in your code, you
187/// can use the [`cfg!`] macro. To conditionally apply an attribute,
188/// see [`cfg_attr`].
189///
190/// For more information, see the Reference on [the `cfg` attribute].
191///
192/// [`cfg_attr`]: ./attribute.cfg_attr.html
193/// [the `cfg` attribute]: ../reference/conditional-compilation.html#the-cfg-attribute
194/// [`if`]: ./keyword.if.html
195const _: () = ();
196
197#[doc(attribute = "crate_name")]
198//
199/// Specifies the name of the crate.
200///
201/// Written as an inner attribute at the top of the crate root, `crate_name` is used
202/// to specify the name of a crate.
203///
204/// ```
205/// #![crate_name = "my_crate"]
206/// ```
207///
208/// The crate name must not be empty, and must only contain [Unicode alphanumeric] or
209/// `_` (U+005F) characters.
210///
211/// Note that the specified name must match the name given by the `--crate-name` flag.
212///
213/// ```text
214/// error: `--crate-name` and `#[crate_name]` are required to match, but `cli_name` != `attr_name`
215/// --> main.rs:1:17
216/// |
217/// 1 | #![crate_name = "attr_name"]
218/// | ^^^^^^^^^^^
219/// ```
220///
221/// In a Cargo project the crate name is always passed as `--crate-name`, derived from the target
222/// name (`[lib] name` or `[[bin]] name` if set, otherwise `[package] name` for libraries and the
223/// primary binary, or the source file name for other binaries). The `crate_name` attribute is
224/// therefore redundant here: it adds nothing, and if it is present it must match Cargo's name,
225/// otherwise `rustc` reports an error.
226///
227/// For more information, see the Reference on [the `crate_name` attribute].
228///
229/// [Unicode alphanumeric]: ../std/primitive.char.html#method.is_alphanumeric
230/// [the `crate_name` attribute]: ../reference/crates-and-source-files.html#the-crate_name-attribute
231const _: () = ();
232
233#[doc(attribute = "deny")]
234//
235/// Emits an error, preventing the compilation from finishing, when a lint check has failed.
236/// This is useful for enforcing rules or preventing certain patterns:
237///
238/// ```compile_fail
239/// #[deny(unused)]
240/// fn foo() {
241/// let x = 42; // Emits an error because x is unused.
242/// }
243/// ```
244///
245/// `deny` can be overridden by [`allow`], [`warn`], and [`forbid`]:
246///
247/// ```
248/// #![deny(unused)]
249///
250/// #[allow(unused)] // We override the `deny` for this function.
251/// fn foo() {
252/// let x = 42; // No lint emitted even though `x` is unused.
253/// }
254/// ```
255///
256/// Multiple lints can also be set to `deny` at once:
257///
258/// ```compile_fail
259/// #![deny(unused_imports, unused_variables)]
260/// use std::collections::*;
261///
262/// fn main() {
263/// let mut x = 10;
264/// }
265/// ```
266///
267/// The lint checks supported by rustc can be found via `rustc -W help`,
268/// along with their default settings and are documented in [the `rustc` book].
269///
270/// [the `rustc` book]: ../rustc/lints/listing/index.html
271///
272/// For more information, see the Reference on [the `deny` attribute].
273///
274/// [the `deny` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
275/// [`forbid`]: ./attribute.forbid.html
276/// [`allow`]: ./attribute.allow.html
277/// [`warn`]: ./attribute.warn.html
278/// [`deny`]: ./attribute.deny.html
279const _: () = ();
280
281#[doc(attribute = "forbid")]
282//
283/// Emits an error, preventing the compilation from finishing, when a lint check has failed.
284///
285/// A lint set to `forbid` cannot be overridden by [`allow`] or [`warn`].
286/// Attempting either will result in a compilation error. Writing `#[deny(...)]` on the same lint inside a
287/// `forbid` scope is permitted, but has no effect; the lint remains at the `forbid` level.
288///
289/// This is useful for enforcing strict policies that should not be relaxed
290/// anywhere in the codebase. Example:
291///
292/// ```
293/// #![forbid(unsafe_code)]
294///
295/// // This would cause a compilation error if uncommented:
296/// // #[allow(unsafe_code)] // error: cannot override `forbid`
297/// ```
298///
299/// Multiple lints can be set to `forbid` at once:
300///
301/// ```
302/// #![forbid(unsafe_code, unused)]
303/// ```
304///
305/// The lint checks supported by rustc can be found via `rustc -W help`,
306/// along with their default settings and are documented in [the `rustc` book].
307///
308/// [the `rustc` book]: ../rustc/lints/listing/index.html
309///
310/// For more information, see the Reference on [the `forbid` attribute].
311///
312/// [the `forbid` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
313/// [`allow`]: ./attribute.allow.html
314/// [`warn`]: ./attribute.warn.html
315const _: () = ();
316
317#[doc(attribute = "deprecated")]
318//
319/// Emits a warning during compilation when an item with this attribute is used.
320/// `since` and `note` are optional fields giving more detail about why the item is deprecated.
321///
322/// * `since`: the version since when the item is deprecated.
323/// * `note`: the reason why an item is deprecated.
324///
325/// Example:
326///
327/// ```
328/// #[deprecated(since = "1.0.0", note = "Use bar instead")]
329/// struct Foo;
330/// struct Bar;
331/// ```
332///
333/// The `deprecated` attribute helps developers transition away from old code by providing warnings
334/// when deprecated items are used. Note that during `Cargo` builds, warnings on dependencies get
335/// silenced by default, so you may not see a deprecation warning unless you build that dependency
336/// directly.
337///
338/// For more information, see the Reference on [the `deprecated` attribute].
339///
340/// [the `deprecated` attribute]: ../reference/attributes/diagnostics.html#the-deprecated-attribute
341const _: () = ();
342
343#[doc(attribute = "warn")]
344//
345/// Emits a warning during compilation when a lint check failed.
346///
347/// Unlike [`deny`] or [`forbid`], `warn` does not produce a hard error: the compilation
348/// continues, but the compiler emits a warning message. `warn` can be overridden by [`allow`],
349/// [`deny`], and [`forbid`].
350///
351/// Example:
352///
353/// ```compile_fail
354/// #![allow(unused)]
355///
356/// #[warn(unused)] // We override the allowed `unused` lint.
357/// fn foo() {
358/// // This lint warns by default even without #[warn(unused)] being explicitly set
359/// let x = 42; // warning: unused variable `x`
360/// }
361/// ```
362///
363///
364/// Many lints, including `unused`, are already set to `warn` by default so this attribute is
365/// mainly useful for lints that are normally [`allow`] by default.
366///
367/// Multiple lints can be set to `warn` at once:
368///
369/// ```compile_fail
370/// #[warn(unused_mut, unused_variables)]
371/// fn main() {
372/// let mut x = 42;
373/// }
374/// ```
375///
376/// The lint checks supported by rustc can be found via `rustc -W help`,
377/// along with their default settings and are documented in [the `rustc` book].
378///
379/// [the `rustc` book]: ../rustc/lints/listing/index.html
380///
381/// For more information, see the Reference on [the `warn` attribute].
382///
383/// [the `warn` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes
384/// [`allow`]: ./attribute.allow.html
385/// [`deny`]: ./attribute.deny.html
386/// [`forbid`]: ./attribute.forbid.html
387const _: () = ();
388
389#[doc(attribute = "no_std")]
390//
391/// Prevents automatically linking the standard library.
392///
393/// Written as an inner attribute at the top of the crate root, the `no_std` attribute stops
394/// the compiler from linking [`std`] into the crate, and only links [`core`].
395///
396/// The attribute also swaps which prelude gets inserted, with the [`core`] prelude replacing
397/// the [`std`] prelude. Items like [`Option`], [`Result`], and the primitive types remain
398/// available from [`core`] without any imports needed:
399///
400/// ```rust,ignore (no_std)
401/// #![no_std]
402///
403/// fn halve(x: u32) -> Option<u32> {
404/// if x % 2 == 0 { Some(x / 2) } else { None }
405/// }
406/// ```
407///
408/// Anything that requires heap memory allocations is not part of [`core`]. Linking the [`alloc`]
409/// crate explicitly can be used to include these types:
410///
411/// ```rust,ignore (no_std + needs a global allocator to link)
412/// #![no_std]
413///
414/// extern crate alloc;
415///
416/// use alloc::vec::Vec;
417/// ```
418///
419/// A `no_std` binary also removes the startup routine and default panic handler `std`
420/// normally provides. It must define its own `#[panic_handler]`, and typically its own
421/// entry point by additionally using `#![no_main]`:
422///
423/// ```rust,ignore (no_std + needs more than no_main alone to link)
424/// #![no_std]
425/// #![no_main]
426///
427/// use core::panic::PanicInfo;
428///
429/// #[panic_handler]
430/// fn on_panic(_info: &PanicInfo) -> ! {
431/// loop {}
432/// }
433/// ```
434///
435/// For more information, see the Reference on [the `no_std` attribute].
436///
437/// [`std`]: ../std/index.html
438/// [`core`]: ../core/index.html
439/// [`alloc`]: ../alloc/index.html
440/// [`Option`]: option::Option
441/// [`Result`]: result::Result
442/// [the `no_std` attribute]: ../reference/names/preludes.html#the-no_std-attribute
443const _: () = ();
444
445#[doc(attribute = "no_main")]
446/// Prevents emitting the `main` entrypoint symbol.
447///
448/// Written as an inner attribute at the top of the crate root, the `no_main` attribute prevents
449/// the compiler from generating the program's standard Rust entrypoint, leaving that up to you.
450/// This is useful when some other object being linked into the program defines the entrypoint,
451/// or when you want to define the platform's `main` symbol yourself.
452///
453/// If you define an entry point symbol that is suitable for your target, it must also be exported
454/// under an unmangled name using the [`no_mangle` attribute]:
455///
456/// ```no_run
457/// #![no_main]
458///
459/// use std::ffi::{c_char, c_int};
460///
461/// #[unsafe(no_mangle)]
462/// pub extern "C" fn main(_argc: c_int, _argv: *const *const c_char) -> c_int {
463/// println!("Hello world!");
464/// 0
465/// }
466/// ```
467///
468/// Unlike `no_std`, the `no_main` attribute does not prevent the compiler from generating
469/// the startup routine or linking the standard library provided by the operating system's executable [ABI].
470///
471/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
472/// [`no_mangle` attribute]: ../reference/abi.html#the-no_mangle-attribute
473const _: () = ();
474
475#[doc(attribute = "repr")]
476/// Specifies the [representation] of a type.
477///
478/// Written as an outer attribute on a `struct`, `enum` or `union`, `repr` controls how the
479/// type is laid out in memory. This is needed when passing types across an [FFI] boundary
480/// or whenever you rely on a specific layout.
481///
482/// The available representations are:
483///
484/// * [`Rust`] (the default): the layout is unspecified and may change between compilations.
485/// * [`C`]: the layout follows the rules of the C language for the target platform.
486/// * The [primitive representations] (`u8`, `i32`, `usize`, ...): set the discriminant
487/// type of an enum.
488/// * [`transparent`]: the type has the same layout and ABI as its only non-zero-sized field.
489///
490/// The `align(N)` and `packed(N)` modifiers raise or lower the alignment of a type, respectively.
491/// `N` must be a power of two no greater than 2<sup>29</sup>. The two modifiers cannot be
492/// applied to the same type, and references to fields of a `packed` type cannot be created,
493/// since they may be unaligned.
494///
495/// # Examples
496///
497/// ```
498/// #[repr(C, align(16))]
499/// struct Foo {
500/// first: u32,
501/// second: u32,
502/// third: u32,
503/// }
504///
505/// assert_eq!(align_of::<Foo>(), 16);
506/// // 12 bytes of fields, rounded up to a multiple of the alignment.
507/// assert_eq!(size_of::<Foo>(), 16);
508/// ```
509///
510/// ```
511/// #[repr(C, packed(2))]
512/// struct Foo {
513/// first: u32,
514/// second: u32,
515/// third: u32,
516/// }
517///
518/// assert_eq!(align_of::<Foo>(), 2);
519/// assert_eq!(size_of::<Foo>(), 12);
520/// ```
521///
522/// ```
523/// #[repr(C)]
524/// struct Foo<T> {
525/// item: T,
526/// }
527/// ```
528///
529/// `Foo` is laid out according to the C rules and is guaranteed to be 16-byte aligned.
530///
531/// The representation is a property of the type definition, so it does
532/// parameters: below, `Foo<u8>` and `Foo<u64>` both use the `C` representation. Their actual
533/// layouts still differ, because they depend on `T`.
534///
535/// [representation]: ../reference/type-layout.html#representations
536/// [`Rust`]: ../reference/type-layout.html#the-rust-representation
537/// [`C`]: ../reference/type-layout.html#the-c-representation
538/// [primitive representations]: ../reference/type-layout.html#primitive-representations
539/// [`transparent`]: ../reference/type-layout.html#the-transparent-representation
540/// [FFI]: https://en.wikipedia.org/wiki/Foreign_function_interface
541const _: () = ();
542
543#[doc(attribute = "inline")]
544//
545/// Suggest that the compiler inline a function at its call sites.
546///
547/// Inlining replaces a call with a copy of the called function's body, which can remove the
548/// overhead of the call. The `inline` attribute is only a hint: the compiler may ignore it, and
549/// it already inlines functions on its own when that looks worthwhile. Poor choices about what to
550/// inline can make a program larger or slower.
551///
552/// Where it does matter is inlining across crate boundaries. A non-generic function is not
553/// normally inlined into another crate, since the calling crate compiles against only its
554/// signature. Marking it `#[inline]` makes the body available to other crates so they can inline
555/// it too:
556///
557/// ```rust
558/// # #![allow(dead_code)]
559/// #[inline]
560/// pub fn square(x: i32) -> i32 {
561/// x * x
562/// }
563/// ```
564///
565/// Generic functions do not need this. They are instantiated in each crate that uses them, so
566/// their bodies are already available to inline.
567///
568/// The attribute applies to functions and has three forms:
569///
570/// - `#[inline]` suggests inlining the function.
571/// - `#[inline(always)]` suggests inlining it at every call site.
572/// - `#[inline(never)]` suggests never inlining it.
573///
574/// You should almost never need `#[inline(always)]`: prefer to let the compiler decide unless
575/// profiling shows a small, hot function that benefits from it. `#[inline(never)]` is useful to
576/// keep a rarely used path, such as a function that only reports an error, out of its caller.
577///
578/// For more information, see the Reference on [the `inline` attribute].
579///
580/// [the `inline` attribute]: ../reference/attributes/codegen.html#the-inline-attribute
581const _: () = ();
582
583#[doc(attribute = "cold")]
584//
585/// Hint to the compiler that a function is unlikely to be called.
586///
587/// Marking a function `#[cold]` tells the compiler that calls to it are rare, so it can
588/// optimize for the common case where the function is not called. It is only a hint: the
589/// compiler may ignore it, and it does not change the function's behavior.
590///
591/// It is typically used on functions that handle uncommon cases, such as error or panic paths:
592///
593/// ```rust
594/// # #![allow(dead_code)]
595/// fn check(value: i32) {
596/// if value < 0 {
597/// report_error("value must be non-negative");
598/// }
599/// // ... the common case continues here ...
600/// }
601///
602/// #[cold]
603/// fn report_error(message: &str) {
604/// eprintln!("error: {message}");
605/// }
606/// ```
607///
608/// For more information, see the Reference on [the `cold` attribute].
609///
610/// [the `cold` attribute]: ../reference/attributes/codegen.html#the-cold-attribute
611const _: () = ();
612
613#[doc(attribute = "track_caller")]
614//
615/// Make a function report the location of its caller instead of its own.
616///
617/// When a function panics, the panic message normally points at the line inside that function
618/// where the panic happened. `#[track_caller]` changes that: it lets the function see the
619/// [`Location`] it was called from, so the panic (and any direct use of [`Location::caller`])
620/// points at the call site instead. The standard library uses this on methods like
621/// [`Option::unwrap`], so a failed `unwrap` blames the line that called it rather than a line
622/// inside the standard library.
623///
624/// ```rust,should_panic
625/// #[track_caller]
626/// fn assert_even(n: i32) {
627/// assert!(n % 2 == 0, "{n} is not even");
628/// }
629///
630/// // The panic blames this line, not the `assert!` inside `assert_even`.
631/// assert_even(3);
632/// ```
633///
634/// The attribute applies to functions with the default `"Rust"` ABI, other than `fn main`.
635///
636/// For more information, see the Reference on [the `track_caller` attribute].
637///
638/// [`Location`]: panic::Location
639/// [`Location::caller`]: panic::Location::caller
640/// [`Option::unwrap`]: Option::unwrap
641/// [the `track_caller` attribute]: ../reference/attributes/codegen.html#the-track_caller-attribute
642const _: () = ();
643
644#[doc(attribute = "proc_macro")]
645//
646/// Defines a function-like procedural macro.
647///
648/// Applied to a `pub` function at the root of a proc-macro crate, `proc_macro` makes that function usable as a macro invoked as
649/// `foo!(...)` in other crates. The function receives the tokens written inside the invocation as a [`TokenStream`] and returns
650/// the [`TokenStream`] that replaces the invocation:
651///
652/// ```rust, ignore (requires depending on the proc-macro crate)
653/// # extern crate proc_macro;
654/// use proc_macro::TokenStream;
655///
656/// #[proc_macro]
657/// pub fn foo(input: TokenStream) -> TokenStream {
658/// "fn answer() -> u32 { 67 }".parse().unwrap()
659/// }
660/// ```
661///
662/// The macro can only be invoked from other crates, not from the crate where it is defined:
663///
664/// ```rust,ignore (requires depending on the proc-macro crate)
665/// use my_macro_crate::foo;
666///
667/// // Expands to `fn answer() -> u32 { 67 }`.
668/// foo!();
669///
670/// fn main() {
671/// println!("{}", answer()); // Prints 67
672/// }
673/// ```
674///
675/// The attribute is only usable with crates of the `proc-macro` crate type, which is set in the crate's `Cargo.toml`
676/// with `proc-macro = true` in the `[lib]` section. Using it anywhere else is a compilation error:
677///
678/// ```text
679///error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
680/// --> src/lib.rs:4:1
681/// |
682/// 4| #[proc_macro]
683/// | ^^^^^^^^^^^^
684/// ```
685///
686/// For more information, see the Reference on [function-like procedural macros] and the [`proc_macro`] crate documentation.
687///
688/// [`TokenStream`]: ../proc_macro/struct.TokenStream.html
689/// [function-like procedural macros]: ../reference/procedural-macros.html#the-proc_macro-attribute
690/// [`proc_macro`]: ../proc_macro/index.html
691const _: () = ();
692
693#[doc(attribute = "link_section")]
694//
695/// Places a function or static in a specific object-file section.
696///
697/// The `link_section` attribute specifies the section of the generated object file where a
698/// function or static is placed. Section names and their meaning are target-specific.
699///
700/// ```rust,no_run
701/// # #[cfg(target_os = "linux")] {
702/// #[unsafe(link_section = ".example_section")]
703/// pub static VALUE: u32 = 42;
704/// # }
705/// ```
706///
707/// Incorrectly placing code or data in a section can violate requirements imposed by the target,
708/// linker, or runtime. For example, placing mutable data in a read-only section may result in
709/// undefined behavior. For this reason, `link_section` is an unsafe attribute.
710///
711/// Starting with the 2024 edition, the attribute must be written using the `unsafe(...)` syntax.
712/// Earlier editions also permit `#[link_section = "..."]`.
713///
714/// For more information, see the Reference on [the `link_section` attribute].
715///
716/// [the `link_section` attribute]: ../reference/abi.html#the-link_section-attribute
717const _: () = ();
718
719#[doc(attribute = "non_exhaustive")]
720//
721/// Indicates that a type might have more fields or variants added in the future.
722///
723/// Placing `#[non_exhaustive]` on a struct or enum tells code in other crates not to assume
724/// the definition is complete. This lets a library add new fields or variants without breaking
725/// existing code.
726///
727/// On an enum, code outside the defining crate must include a wildcard arm when matching:
728///
729/// ```rust,ignore (cross-crate effect only)
730/// // in crate `errors`:
731/// #[non_exhaustive]
732/// pub enum ConnectionError {
733/// Refused,
734/// Timeout,
735/// }
736///
737/// // in another crate:
738/// use errors::ConnectionError;
739///
740/// match error {
741/// ConnectionError::Refused => println!("connection refused"),
742/// ConnectionError::Timeout => println!("timed out"),
743/// _ => println!("other error"), // required because of #[non_exhaustive]
744/// }
745/// ```
746///
747/// On a struct, code outside the defining crate cannot construct instances using struct literal
748/// syntax:
749///
750/// ```rust,ignore (cross-crate effect only)
751/// // in crate `config`:
752/// #[non_exhaustive]
753/// pub struct Config {
754/// pub width: u32,
755/// pub height: u32,
756/// }
757///
758/// // in another crate:
759/// use config::Config;
760///
761/// let c = Config { width: 800, height: 600 }; // ERROR: cannot construct
762/// ```
763///
764/// Inside the defining crate, exhaustive matching and direct construction are still allowed.
765///
766/// For more information, see the Reference on [the `non_exhaustive` attribute].
767///
768/// [the `non_exhaustive` attribute]: ../reference/attributes/type_system.html#the-non_exhaustive-attribute
769const _: () = ();
770
771#[doc(attribute = "automatically_derived")]
772//
773/// The `automatically_derived` attribute is used to indicate that an `impl` block is automatically
774/// generated by a [derive macro]. It has no direct effect, but it may be used by tools and
775/// diagnostic lints, such as [Clippy], to detect these automatically generated implementations.
776///
777/// Example:
778///
779/// ```rust
780/// // Given #[derive(Clone)] on struct Example, the derive macro may produce:
781/// #[automatically_derived]
782/// impl ::core::clone::Clone for Example {
783/// fn clone(&self) -> Self { Example }
784/// }
785/// # struct Example;
786/// ```
787///
788/// The attribute may only be applied to an `impl` block.
789///
790/// Because code generated by a macro becomes part of the source code representation of the crate
791/// that calls the macro, diagnostic lints for the expanded macro will be emitted in the caller's
792/// workspace (whereas warnings generated while building dependencies not in the local workspace
793/// are suppressed by Cargo). This may be undesirable, so the author of the macro can use the
794/// `automatically_derived` attribute to hint to tools that the generated code is not written by the
795/// caller (and that tools should not render warnings for it).
796///
797/// For example, this `impl` block lacks `#[automatically_derived]`:
798///
799/// ```rust
800/// // Suppose this impl block is the output of a derive macro
801/// impl ::core::cmp::PartialEq for Example {
802/// fn eq(&self, other: &Self) -> bool { true }
803/// fn ne(&self, other: &Self) -> bool { !(self == other) }
804/// }
805/// # struct Example;
806/// ```
807///
808/// In the example above, Clippy will emit the following warning:
809///
810/// ```text
811/// warning: re-implementing `PartialEq::ne` is unnecessary
812/// --> src/lib.rs:7:9
813/// |
814/// 4 | fn ne(&self, other: &Self) -> bool { !(self == other) }
815/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
816/// |
817/// = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.98.0/index.html#partialeq_ne_impl
818/// = note: `#[warn(clippy::partialeq_ne_impl)]` on by default
819/// ```
820///
821/// However, if the derive macro were written so that it outputs `#[automatically_derived]` on the
822/// `impl` block, as below, then Clippy will not emit the above `partialeq_ne_impl` lint:
823///
824/// ```rust
825/// // Suppose this impl block is the output of a derive macro
826/// #[automatically_derived]
827/// impl ::core::cmp::PartialEq for Example {
828/// fn eq(&self, other: &Self) -> bool { true }
829/// fn ne(&self, other: &Self) -> bool { !(self == other) }
830/// }
831/// # struct Example;
832/// ```
833///
834/// The `automatically_derived` attribute should be preferred over the [`allow`] attribute for
835/// suppressing unintended user-facing diagnostics in automatically generated code.
836///
837/// For more information, see the Reference on [the `automatically_derived` attribute].
838///
839/// [`allow`]: ./attribute.allow.html
840/// [derive macro]: ./attr.derive.html
841/// [Clippy]: https://github.com/rust-lang/rust-clippy
842/// [the `automatically_derived` attribute]: ../reference/attributes/derive.html#the-automatically_derived-attribute
843const _: () = ();
844
845#[doc(attribute = "cfg_attr")]
846//
847/// The `cfg_attr` attribute is used to conditionally apply one or more attributes to an item.
848///
849/// Example:
850///
851/// ```rust
852/// // The struct derives `Debug` when the `debug_impls` feature is enabled.
853/// #[cfg_attr(feature = "debug_impls", derive(Debug))]
854/// struct X;
855/// ```
856///
857/// You can apply multiple attributes by separating them with commas:
858///
859/// ```rust
860/// #[cfg_attr(feature = "nightly", allow(dead_code), deny(unused_variables))]
861/// // This function only gets the `allow(dead_code)` and `deny(unused_variables)` attributes when
862/// // `feature = "nightly"` is active.
863/// fn nightly_only_function() {
864/// let x = 42;
865/// }
866/// ```
867///
868/// For complex conditions, you can combine `all(...)`, `any(...)`, and `not(...)`.
869///
870/// * `all`: True if all given predicates are true.
871/// * `any`: True if at least one of the given predicates is true.
872/// * `not`: True if the predicate is false.
873///
874/// ```rust
875/// #[cfg_attr(
876/// all(feature = "system", feature = "disk"),
877/// doc = "These docs only show up if both `system`, and `disk` are enabled.",
878/// )]
879/// mod my_module {
880/// // ...
881/// }
882/// ```
883///
884/// For more information, see the Reference on [the `cfg_attr` attribute].
885///
886/// [`cfg`]: ./attribute.cfg.html
887/// [the `cfg_attr` attribute]: ../reference/conditional-compilation.html#the-cfg_attr-attribute
888const _: () = ();