Skip to content

Commit

Permalink
Revert "coverage_attribute has been stabilized"
Browse files Browse the repository at this point in the history
This reverts commit 67a45bf.

Stabilization has been reverted.

Closes #410
  • Loading branch information
taiki-e committed Dec 24, 2024
1 parent e95c27b commit 0263d8b
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 203 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,19 @@ cargo llvm-cov --open --ignore-filename-regex build

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

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.
Since `#[coverage(off)]` is unstable, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov.

```rust
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

// function
#[cfg_attr(coverage, coverage(off))]
#[cfg_attr(coverage_nightly, coverage(off))]
fn exclude_fn_from_coverage() {
// ...
}

// module
#[cfg_attr(coverage, coverage(off))]
#[cfg_attr(coverage_nightly, coverage(off))]
mod exclude_mod_from_coverage {
// ...
}
Expand All @@ -520,8 +522,10 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)
If you want to ignore all `#[test]`-related code, you can use module-level `#[coverage(off)]` attribute:

```rust
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

#[cfg(test)]
#[cfg_attr(coverage, coverage(off))]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
// ...
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}}}
{"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","38":"1/1","39":"1/2","40":"0/1","41":"1/2","42":"1/1"}}}
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
1| 1|fn func(x: i32) {
2| 1| match x {
3| 1| 0 => {}
4| 0| 1 => {}
5| 0| 2 => {}
6| 0| 3 => {}
7| 0| _ => {}
8| | }
9| 1|}
10| |
11| |#[coverage(off)]
12| |#[test]
13| |fn fn_level() {
14| | func(0);
15| |
16| | if false {
17| | func(1);
18| | }
19| |}
20| |
21| |// #[coverage(off)] has no effect on expressions.
22| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
23| |#[test]
24| 1|fn expr_level() {
25| 1| if false {
26| 0| // #[coverage(off)]
27| 0| func(2);
28| 1| }
29| 1|}
30| |
31| |#[coverage(off)]
32| |mod mod_level {
33| | use super::func;
34| |
35| | #[test]
36| | fn mod_level() {
37| | if false {
38| | func(3);
39| | }
40| | }
41| |}
1| |#![cfg_attr(coverage, feature(coverage_attribute))]
2| |
3| 1|fn func(x: i32) {
4| 1| match x {
5| 1| 0 => {}
6| 0| 1 => {}
7| 0| 2 => {}
8| 0| 3 => {}
9| 0| _ => {}
10| | }
11| 1|}
12| |
13| |#[cfg_attr(coverage, coverage(off))]
14| |#[test]
15| 1|fn fn_level() {
16| 1| func(0);
17| 1|
18| 1| if false {
19| 0| func(1);
20| 1| }
21| 1|}
22| |
23| |// #[coverage(off)] has no effect on expressions.
24| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
25| |#[test]
26| 1|fn expr_level() {
27| 1| if false {
28| 0| // #[cfg_attr(coverage, coverage(off))]
29| 0| func(2);
30| 1| }
31| 1|}
32| |
33| |#[cfg_attr(coverage, coverage(off))]
34| |mod mod_level {
35| | use super::func;
36| |
37| | #[test]
38| 1| fn mod_level() {
39| 1| if false {
40| 0| func(3);
41| 1| }
42| 1| }
43| |}
44 changes: 22 additions & 22 deletions tests/fixtures/coverage-reports/no_coverage/no_cfg_coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
"count": 4,
"covered": 4,
"percent": 100.0
},
"instantiations": {
"count": 2,
"covered": 2,
"count": 4,
"covered": 4,
"percent": 100.0
},
"lines": {
"count": 14,
"covered": 8,
"percent": 57.14285714285714
"count": 26,
"covered": 18,
"percent": 69.23076923076923
},
"regions": {
"count": 11,
"covered": 6,
"notcovered": 5,
"percent": 54.54545454545454
"count": 19,
"covered": 12,
"notcovered": 7,
"percent": 63.1578947368421
}
}
}
Expand All @@ -49,19 +49,19 @@
"percent": 0
},
"functions": {
"count": 2,
"covered": 2,
"count": 4,
"covered": 4,
"percent": 100
},
"instantiations": {
"count": 2,
"covered": 2,
"count": 4,
"covered": 4,
"percent": 100
},
"lines": {
"count": 14,
"covered": 8,
"percent": 57.14285714285714
"count": 26,
"covered": 18,
"percent": 69.23076923076923
},
"mcdc": {
"count": 0,
Expand All @@ -70,10 +70,10 @@
"percent": 0
},
"regions": {
"count": 11,
"covered": 6,
"notcovered": 5,
"percent": 54.54545454545454
"count": 19,
"covered": 12,
"notcovered": 7,
"percent": 63.1578947368421
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SF:src/lib.rs
FNF:2
FNH:2
FNF:4
FNH:4
BRF:0
BRH:0
LF:14
LH:8
LF:26
LH:18
end_of_record
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src/lib.rs 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -
src/lib.rs 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -
TOTAL 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
84 changes: 43 additions & 41 deletions tests/fixtures/coverage-reports/no_coverage/no_cfg_coverage.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
1| 1|fn func(x: i32) {
2| 1| match x {
3| 1| 0 => {}
4| 0| 1 => {}
5| 0| 2 => {}
6| 0| 3 => {}
7| 0| _ => {}
8| | }
9| 1|}
10| |
11| |#[coverage(off)]
12| |#[test]
13| |fn fn_level() {
14| | func(0);
15| |
16| | if false {
17| | func(1);
18| | }
19| |}
20| |
21| |// #[coverage(off)] has no effect on expressions.
22| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
23| |#[test]
24| 1|fn expr_level() {
25| 1| if false {
26| 0| // #[coverage(off)]
27| 0| func(2);
28| 1| }
29| 1|}
30| |
31| |#[coverage(off)]
32| |mod mod_level {
33| | use super::func;
34| |
35| | #[test]
36| | fn mod_level() {
37| | if false {
38| | func(3);
39| | }
40| | }
41| |}
1| |#![cfg_attr(coverage, feature(coverage_attribute))]
2| |
3| 1|fn func(x: i32) {
4| 1| match x {
5| 1| 0 => {}
6| 0| 1 => {}
7| 0| 2 => {}
8| 0| 3 => {}
9| 0| _ => {}
10| | }
11| 1|}
12| |
13| |#[cfg_attr(coverage, coverage(off))]
14| |#[test]
15| 1|fn fn_level() {
16| 1| func(0);
17| 1|
18| 1| if false {
19| 0| func(1);
20| 1| }
21| 1|}
22| |
23| |// #[coverage(off)] has no effect on expressions.
24| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
25| |#[test]
26| 1|fn expr_level() {
27| 1| if false {
28| 0| // #[cfg_attr(coverage, coverage(off))]
29| 0| func(2);
30| 1| }
31| 1|}
32| |
33| |#[cfg_attr(coverage, coverage(off))]
34| |mod mod_level {
35| | use super::func;
36| |
37| | #[test]
38| 1| fn mod_level() {
39| 1| if false {
40| 0| func(3);
41| 1| }
42| 1| }
43| |}
Original file line number Diff line number Diff line change
@@ -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"}}}
{"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","26":"1/1","27":"1/2","28":"0/1","29":"0/1","30":"1/2","31":"1/1"}}}
Loading

0 comments on commit 0263d8b

Please sign in to comment.