Skip to content

Commit 67a45bf

Browse files
committed
coverage_attribute has been stabilized
1 parent cec754f commit 67a45bf

16 files changed

+265
-278
lines changed

README.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This is a wrapper around rustc [`-C instrument-coverage`][instrument-coverage] a
2222
- [Get coverage of C/C++ code linked to Rust library/binary](#get-coverage-of-cc-code-linked-to-rust-librarybinary)
2323
- [Get coverage of external tests](#get-coverage-of-external-tests)
2424
- [Exclude file from coverage](#exclude-file-from-coverage)
25-
- [Exclude function from coverage](#exclude-function-from-coverage)
25+
- [Exclude code from coverage](#exclude-code-from-coverage)
2626
- [Continuous Integration](#continuous-integration)
2727
- [Display coverage in VS Code](#display-coverage-in-vs-code)
2828
- [Environment variables](#environment-variables)
@@ -485,17 +485,22 @@ To exclude specific file patterns from the report, use the `--ignore-filename-re
485485
cargo llvm-cov --open --ignore-filename-regex build
486486
```
487487

488-
### Exclude function from coverage
488+
### Exclude code from coverage
489489

490-
To exclude the specific function from coverage, use the [`#[coverage(off)]` attribute][rust-lang/rust#84605].
490+
To exclude the specific function or module from coverage, use the [`#[coverage(off)]` attribute][rust-lang/rust#84605].
491491

492-
Since `#[coverage(off)]` is unstable, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov.
492+
Since `#[coverage(off)]` attribute stabilized in Rust 1.85, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov for compatibility with old Rust.
493493

494494
```rust
495-
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
495+
// function
496+
#[cfg_attr(coverage, coverage(off))]
497+
fn exclude_fn_from_coverage() {
498+
// ...
499+
}
496500

497-
#[cfg_attr(coverage_nightly, coverage(off))]
498-
fn exclude_from_coverage() {
501+
// module
502+
#[cfg_attr(coverage, coverage(off))]
503+
mod exclude_mod_from_coverage {
499504
// ...
500505
}
501506
```
@@ -512,11 +517,17 @@ Rust 1.80+ warns the above cfgs as `unexpected_cfgs`. The recommended way to add
512517
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }
513518
```
514519

515-
If you want to ignore all `#[test]`-related code, consider using [coverage-helper] crate version 0.2+.
520+
If you want to ignore all `#[test]`-related code, you can use module-level `#[coverage(off)]` attribute:
516521

517-
cargo-llvm-cov excludes code contained in the directory named `tests` from the report by default, so you can also use it instead of coverage-helper crate.
522+
```rust
523+
#[cfg(test)]
524+
#[cfg_attr(coverage, coverage(off))]
525+
mod tests {
526+
// ...
527+
}
528+
```
518529

519-
**Note:** `#[coverage(off)]` was previously named `#[no_coverage]`. When using `#[no_coverage]` in the old nightly, replace `feature(coverage_attribute)` with `feature(no_coverage)`, `coverage(off)` with `no_coverage`, and `coverage-helper` 0.2+ with `coverage-helper` 0.1.
530+
cargo-llvm-cov excludes code contained in the directory named `tests` from the report by default, so you can also use it instead of `#[coverage(off)]` attribute.
520531

521532
### Continuous Integration
522533

@@ -716,21 +727,18 @@ See also [the code-coverage-related issues reported in rust-lang/rust](https://g
716727

717728
## Related Projects
718729

719-
- [coverage-helper]: Helper for [#123].
720730
- [cargo-config2]: Library to load and resolve Cargo configuration. cargo-llvm-cov uses this library.
721731
- [cargo-hack]: Cargo subcommand to provide various options useful for testing and continuous integration.
722732
- [cargo-minimal-versions]: Cargo subcommand for proper use of `-Z minimal-versions`.
723733

724734
[#2]: https://github.com/taiki-e/cargo-llvm-cov/issues/2
725735
[#8]: https://github.com/taiki-e/cargo-llvm-cov/issues/8
726736
[#20]: https://github.com/taiki-e/cargo-llvm-cov/issues/20
727-
[#123]: https://github.com/taiki-e/cargo-llvm-cov/issues/123
728737
[#219]: https://github.com/taiki-e/cargo-llvm-cov/issues/219
729738
[cargo-config2]: https://github.com/taiki-e/cargo-config2
730739
[cargo-hack]: https://github.com/taiki-e/cargo-hack
731740
[cargo-minimal-versions]: https://github.com/taiki-e/cargo-minimal-versions
732741
[codecov]: https://codecov.io
733-
[coverage-helper]: https://github.com/taiki-e/coverage-helper
734742
[instrument-coverage]: https://doc.rust-lang.org/rustc/instrument-coverage.html
735743
[nextest]: https://nexte.st/book/test-coverage.html
736744
[rust-lang/rust#79417]: https://github.com/rust-lang/rust/issues/79417
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage":{"src/lib.rs":{"5":"1/1","6":"1/1","7":"1/1","8":"0/1","9":"1/1","10":"0/1","11":"0/1","13":"1/1"}}}
1+
{"coverage":{"src/lib.rs":{"3":"1/1","4":"1/1","5":"1/1","6":"0/1","7":"1/1","8":"0/1","9":"0/1","11":"1/1"}}}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
1| |#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
1+
1| |use coverage_helper::test;
22
2| |
3-
3| |use coverage_helper::test;
4-
4| |
5-
5| 2|fn func(x: i32) {
6-
6| 2| match x {
7-
7| 1| 0 => {}
8-
8| 0| 1 => {}
9-
9| 1| 2 => {}
10-
10| 0| 3 => {}
11-
11| 0| _ => {}
12-
12| | }
13-
13| 2|}
14-
14| |
15-
15| |#[test]
16-
16| |fn test() {
17-
17| | func(0);
18-
18| |
19-
19| | if false {
20-
20| | func(1);
21-
21| | } else {
22-
22| | func(2);
23-
23| | }
24-
24| |}
3+
3| 2|fn func(x: i32) {
4+
4| 2| match x {
5+
5| 1| 0 => {}
6+
6| 0| 1 => {}
7+
7| 1| 2 => {}
8+
8| 0| 3 => {}
9+
9| 0| _ => {}
10+
10| | }
11+
11| 2|}
12+
12| |
13+
13| |#[test]
14+
14| |fn test() {
15+
15| | func(0);
16+
16| |
17+
17| | if false {
18+
18| | func(1);
19+
19| | } else {
20+
20| | func(2);
21+
21| | }
22+
22| |}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
1| |#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
1+
1| |use coverage_helper::test;
22
2| |
3-
3| |use coverage_helper::test;
4-
4| |
5-
5| 2|fn func(x: i32) {
6-
6| 2| match x {
7-
7| 1| 0 => {}
8-
8| 0| 1 => {}
9-
9| 1| 2 => {}
10-
10| 0| 3 => {}
11-
11| 0| _ => {}
12-
12| | }
13-
13| 2|}
14-
14| |
15-
15| |#[test]
16-
16| |fn test() {
17-
17| | func(0);
18-
18| |
19-
19| | if false {
20-
20| | func(1);
21-
21| | } else {
22-
22| | func(2);
23-
23| | }
24-
24| |}
3+
3| 2|fn func(x: i32) {
4+
4| 2| match x {
5+
5| 1| 0 => {}
6+
6| 0| 1 => {}
7+
7| 1| 2 => {}
8+
8| 0| 3 => {}
9+
9| 0| _ => {}
10+
10| | }
11+
11| 2|}
12+
12| |
13+
13| |#[test]
14+
14| |fn test() {
15+
15| | func(0);
16+
16| |
17+
17| | if false {
18+
18| | func(1);
19+
19| | } else {
20+
20| | func(2);
21+
21| | }
22+
22| |}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"coverage":{"src/lib.rs":{"3":"1/1","4":"1/1","5":"1/1","6":"0/1","7":"0/1","8":"0/1","9":"0/1","11":"1/1","15":"1/1","16":"1/1","17":"1/1","18":"1/2","19":"0/1","20":"1/2","21":"1/1","26":"1/1","27":"1/2","28":"0/1","29":"0/1","30":"1/2","31":"1/1","39":"1/1","40":"1/2","41":"0/1","42":"1/2","43":"1/1"}}}
1+
{"coverage":{"src/lib.rs":{"1":"1/1","2":"1/1","3":"1/1","4":"0/1","5":"0/1","6":"0/1","7":"0/1","9":"1/1","24":"1/1","25":"1/2","26":"0/1","27":"0/1","28":"1/2","29":"1/1"}}}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1-
1| |#![cfg_attr(coverage, feature(coverage_attribute))]
2-
2| |
3-
3| 1|fn func(x: i32) {
4-
4| 1| match x {
5-
5| 1| 0 => {}
6-
6| 0| 1 => {}
7-
7| 0| 2 => {}
8-
8| 0| 3 => {}
9-
9| 0| _ => {}
10-
10| | }
11-
11| 1|}
12-
12| |
13-
13| |#[cfg_attr(coverage, coverage(off))]
14-
14| |#[test]
15-
15| 1|fn fn_level() {
16-
16| 1| func(0);
17-
17| 1|
18-
18| 1| if false {
19-
19| 0| func(1);
20-
20| 1| }
21-
21| 1|}
22-
22| |
23-
23| |// #[coverage(off)] has no effect on expressions.
24-
24| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
25-
25| |#[test]
26-
26| 1|fn expr_level() {
27-
27| 1| if false {
28-
28| 0| // #[cfg_attr(coverage, coverage(off))]
29-
29| 0| func(2);
30-
30| 1| }
31-
31| 1|}
32-
32| |
33-
33| |// #[coverage(off)] has no effect on modules.
34-
34| |#[cfg_attr(coverage, coverage(off))]
35-
35| |mod mod_level {
36-
36| | use super::func;
37-
37| |
38-
38| | #[test]
39-
39| 1| fn mod_level() {
40-
40| 1| if false {
41-
41| 0| func(3);
42-
42| 1| }
43-
43| 1| }
44-
44| |}
1+
1| 1|fn func(x: i32) {
2+
2| 1| match x {
3+
3| 1| 0 => {}
4+
4| 0| 1 => {}
5+
5| 0| 2 => {}
6+
6| 0| 3 => {}
7+
7| 0| _ => {}
8+
8| | }
9+
9| 1|}
10+
10| |
11+
11| |#[coverage(off)]
12+
12| |#[test]
13+
13| |fn fn_level() {
14+
14| | func(0);
15+
15| |
16+
16| | if false {
17+
17| | func(1);
18+
18| | }
19+
19| |}
20+
20| |
21+
21| |// #[coverage(off)] has no effect on expressions.
22+
22| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
23+
23| |#[test]
24+
24| 1|fn expr_level() {
25+
25| 1| if false {
26+
26| 0| // #[coverage(off)]
27+
27| 0| func(2);
28+
28| 1| }
29+
29| 1|}
30+
30| |
31+
31| |#[coverage(off)]
32+
32| |mod mod_level {
33+
33| | use super::func;
34+
34| |
35+
35| | #[test]
36+
36| | fn mod_level() {
37+
37| | if false {
38+
38| | func(3);
39+
39| | }
40+
40| | }
41+
41| |}

tests/fixtures/coverage-reports/no_coverage/no_cfg_coverage.json

+22-22
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
"percent": 0.0
1919
},
2020
"functions": {
21-
"count": 4,
22-
"covered": 4,
21+
"count": 2,
22+
"covered": 2,
2323
"percent": 100.0
2424
},
2525
"instantiations": {
26-
"count": 4,
27-
"covered": 4,
26+
"count": 2,
27+
"covered": 2,
2828
"percent": 100.0
2929
},
3030
"lines": {
31-
"count": 26,
32-
"covered": 18,
33-
"percent": 69.23076923076923
31+
"count": 14,
32+
"covered": 8,
33+
"percent": 57.14285714285714
3434
},
3535
"regions": {
36-
"count": 19,
37-
"covered": 12,
38-
"notcovered": 7,
39-
"percent": 63.1578947368421
36+
"count": 11,
37+
"covered": 6,
38+
"notcovered": 5,
39+
"percent": 54.54545454545454
4040
}
4141
}
4242
}
@@ -49,19 +49,19 @@
4949
"percent": 0
5050
},
5151
"functions": {
52-
"count": 4,
53-
"covered": 4,
52+
"count": 2,
53+
"covered": 2,
5454
"percent": 100
5555
},
5656
"instantiations": {
57-
"count": 4,
58-
"covered": 4,
57+
"count": 2,
58+
"covered": 2,
5959
"percent": 100
6060
},
6161
"lines": {
62-
"count": 26,
63-
"covered": 18,
64-
"percent": 69.23076923076923
62+
"count": 14,
63+
"covered": 8,
64+
"percent": 57.14285714285714
6565
},
6666
"mcdc": {
6767
"count": 0,
@@ -70,10 +70,10 @@
7070
"percent": 0
7171
},
7272
"regions": {
73-
"count": 19,
74-
"covered": 12,
75-
"notcovered": 7,
76-
"percent": 63.1578947368421
73+
"count": 11,
74+
"covered": 6,
75+
"notcovered": 5,
76+
"percent": 54.54545454545454
7777
}
7878
}
7979
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
SF:src/lib.rs
2-
FNF:4
3-
FNH:4
2+
FNF:2
3+
FNH:2
44
BRF:0
55
BRH:0
6-
LF:26
7-
LH:18
6+
LF:14
7+
LH:8
88
end_of_record
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
22
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3-
src/lib.rs 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
3+
src/lib.rs 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -
44
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5-
TOTAL 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
5+
TOTAL 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -

0 commit comments

Comments
 (0)