|
| 1 | +# Coverage instrumentation attributes |
| 2 | + |
| 3 | +The following [attributes] are used for controlling coverage instrumentation. |
| 4 | + |
| 5 | +> **Note**: Coverage instrumentation is controlled in `rustc` with the [`-C instrument-coverage`] compiler flag. |
| 6 | +
|
| 7 | +[`-C instrument-coverage`]: ../../rustc/instrument-coverage.html |
| 8 | + |
| 9 | +### The `coverage` attribute |
| 10 | + |
| 11 | +r[attributes.coverage] |
| 12 | + |
| 13 | +r[attributes.coverage.intro] |
| 14 | +The *`coverage` [attribute]* indicates whether a function should include instrumentation for code coverage and show up in code coverage reports. |
| 15 | + |
| 16 | +r[attributes.coverage.syntax] |
| 17 | +The attribute uses the [_MetaListIdents_] syntax to specify its behavior: |
| 18 | + |
| 19 | +* `#[coverage(off)]` indicates that all functions within an item, recursively, should not be instrumented, unless specified by another attribute. |
| 20 | +* `#[coverage(on)]` (the default) indicates that all functions within an item, recursively, *should* be instrumented, unless specified by another attribute. |
| 21 | + |
| 22 | +```rust |
| 23 | +#[coverage(off)] |
| 24 | +fn example() {} |
| 25 | + |
| 26 | +struct S; |
| 27 | + |
| 28 | +#[coverage(off)] |
| 29 | +impl S { |
| 30 | + #[coverage(on)] |
| 31 | + fn function_with_coverage() {} |
| 32 | + |
| 33 | + fn function_without_coverage() {} |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +r[attributes.coverage.allowed-positions] |
| 38 | +The `coverage` attribute can only be controlled at the granularity of individual functions. It can be applied to [functions], [closures], [associated functions], [implementations], [modules], or [the crate root]. |
| 39 | + |
| 40 | +It is an error to specify the attribute on a trait function without a body. |
| 41 | + |
| 42 | +r[attributes.coverage.trait-impl-inherit] |
| 43 | +When specified on a trait function, the attribute only applies to the default function body. Trait implementations do not inherit the setting from the trait definition. |
| 44 | + |
| 45 | +r[attributes.coverage.duplicates] |
| 46 | +It is an error to specify the `#[coverage]` attribute multiple times on the same item. |
| 47 | + |
| 48 | +r[attributes.coverage.nesting] |
| 49 | +Coverage attributes on more deeply nested items take priority over attributes at a higher nesting level. For example, if a crate is marked `#![coverage(off)]`, then functions inside that crate marked `#[coverage(on)]` will still have coverage. |
| 50 | + |
| 51 | +[_MetaListIdents_]: ../attributes.md#meta-item-attribute-syntax |
| 52 | +[associated functions]: ../items/associated-items.md#associated-functions-and-methods |
| 53 | +[attribute]: ../attributes.md |
| 54 | +[attributes]: ../attributes.md |
| 55 | +[closures]: ../expressions/closure-expr.md |
| 56 | +[functions]: ../items/functions.md |
| 57 | +[implementations]: ../items/implementations.md |
| 58 | +[modules]: ../items/modules.md |
| 59 | +[the crate root]: ../crates-and-source-files.md |
0 commit comments