Skip to content

Commit 9f41bc1

Browse files
authored
Merge pull request #1628 from clarfonthey/coverage
`coverage` attribute
2 parents 3436fc7 + b2c8b52 commit 9f41bc1

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [Limits](attributes/limits.md)
4646
- [Type System](attributes/type_system.md)
4747
- [Debugger](attributes/debugger.md)
48+
- [Coverage instrumentation](attributes/coverage-instrumentation.md)
4849

4950
- [Statements and expressions](statements-and-expressions.md)
5051
- [Statements](statements.md)

src/attributes.md

+4
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ The following is an index of all built-in attributes.
329329
- [`debugger_visualizer`] --- Embeds a file that specifies debugger output for a type.
330330
- [`collapse_debuginfo`] --- Controls how macro invocations are encoded in debuginfo.
331331

332+
- Coverage Instrumentation
333+
- [`coverage`] --- Controls how code coverage is instrumented.
334+
332335
[Doc comments]: comments.md#doc-comments
333336
[ECMA-334]: https://www.ecma-international.org/publications-and-standards/standards/ecma-334/
334337
[ECMA-335]: https://www.ecma-international.org/publications-and-standards/standards/ecma-335/
@@ -347,6 +350,7 @@ The following is an index of all built-in attributes.
347350
[`cfg`]: conditional-compilation.md#the-cfg-attribute
348351
[`cold`]: attributes/codegen.md#the-cold-attribute
349352
[`collapse_debuginfo`]: attributes/debugger.md#the-collapse_debuginfo-attribute
353+
[`coverage`]: attributes/coverage-instrumentation.md#the-coverage-attribute
350354
[`crate_name`]: crates-and-source-files.md#the-crate_name-attribute
351355
[`crate_type`]: linkage.md
352356
[`debugger_visualizer`]: attributes/debugger.md#the-debugger_visualizer-attribute
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)