Skip to content

Commit 6024426

Browse files
authored
Rollup merge of #92695 - Swatinem:cover-nested, r=wesleywiser
Add `#[no_coverage]` tests for nested functions I was playing around a bit trying to figure out how `#[no_coverage]` behaves for nested functions and thought I might as well add this as a testcase. The "nesting covered fn inside not covered fn" case looks pretty much as expected. The "nesting not covered fn inside a covered fn" case however seems a bit counterintuitive. Essentially the region of the outer function "covers" its whole lexical range. And the inner function does not generate any region at all. 🤷🏻‍♂️ r? `@richkadel`
2 parents 1f841fc + fe9271a commit 6024426

File tree

2 files changed

+109
-9
lines changed

2 files changed

+109
-9
lines changed

src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.no_cov_crate.txt

+58-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
6| | println!("called but not covered");
77
7| |}
88
8| |
9-
9| |#[no_coverage]
10-
10| |fn do_not_add_coverage_2() {
9+
9| |fn do_not_add_coverage_2() {
10+
10| | #![no_coverage]
1111
11| | println!("called but not covered");
1212
12| |}
1313
13| |
@@ -28,10 +28,60 @@
2828
28| 0| println!("not called but covered");
2929
29| 0|}
3030
30| |
31-
31| 1|fn main() {
32-
32| 1| do_not_add_coverage_1();
33-
33| 1| do_not_add_coverage_2();
34-
34| 1| add_coverage_1();
35-
35| 1| add_coverage_2();
36-
36| 1|}
31+
31| |// FIXME: These test-cases illustrate confusing results of nested functions.
32+
32| |// See https://github.com/rust-lang/rust/issues/93319
33+
33| |mod nested_fns {
34+
34| | #[no_coverage]
35+
35| | pub fn outer_not_covered(is_true: bool) {
36+
36| 1| fn inner(is_true: bool) {
37+
37| 1| if is_true {
38+
38| 1| println!("called and covered");
39+
39| 1| } else {
40+
40| 0| println!("absolutely not covered");
41+
41| 0| }
42+
42| 1| }
43+
43| | println!("called but not covered");
44+
44| | inner(is_true);
45+
45| | }
46+
46| |
47+
47| 1| pub fn outer(is_true: bool) {
48+
48| 1| println!("called and covered");
49+
49| 1| inner_not_covered(is_true);
50+
50| 1|
51+
51| 1| #[no_coverage]
52+
52| 1| fn inner_not_covered(is_true: bool) {
53+
53| 1| if is_true {
54+
54| 1| println!("called but not covered");
55+
55| 1| } else {
56+
56| 1| println!("absolutely not covered");
57+
57| 1| }
58+
58| 1| }
59+
59| 1| }
60+
60| |
61+
61| 1| pub fn outer_both_covered(is_true: bool) {
62+
62| 1| println!("called and covered");
63+
63| 1| inner(is_true);
64+
64| 1|
65+
65| 1| fn inner(is_true: bool) {
66+
66| 1| if is_true {
67+
67| 1| println!("called and covered");
68+
68| 1| } else {
69+
69| 0| println!("absolutely not covered");
70+
70| 0| }
71+
71| 1| }
72+
72| 1| }
73+
73| |}
74+
74| |
75+
75| 1|fn main() {
76+
76| 1| let is_true = std::env::args().len() == 1;
77+
77| 1|
78+
78| 1| do_not_add_coverage_1();
79+
79| 1| do_not_add_coverage_2();
80+
80| 1| add_coverage_1();
81+
81| 1| add_coverage_2();
82+
82| 1|
83+
83| 1| nested_fns::outer_not_covered(is_true);
84+
84| 1| nested_fns::outer(is_true);
85+
85| 1| nested_fns::outer_both_covered(is_true);
86+
86| 1|}
3787

src/test/run-make-fulldeps/coverage/no_cov_crate.rs

+51-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ fn do_not_add_coverage_1() {
66
println!("called but not covered");
77
}
88

9-
#[no_coverage]
109
fn do_not_add_coverage_2() {
10+
#![no_coverage]
1111
println!("called but not covered");
1212
}
1313

@@ -28,9 +28,59 @@ fn add_coverage_not_called() {
2828
println!("not called but covered");
2929
}
3030

31+
// FIXME: These test-cases illustrate confusing results of nested functions.
32+
// See https://github.com/rust-lang/rust/issues/93319
33+
mod nested_fns {
34+
#[no_coverage]
35+
pub fn outer_not_covered(is_true: bool) {
36+
fn inner(is_true: bool) {
37+
if is_true {
38+
println!("called and covered");
39+
} else {
40+
println!("absolutely not covered");
41+
}
42+
}
43+
println!("called but not covered");
44+
inner(is_true);
45+
}
46+
47+
pub fn outer(is_true: bool) {
48+
println!("called and covered");
49+
inner_not_covered(is_true);
50+
51+
#[no_coverage]
52+
fn inner_not_covered(is_true: bool) {
53+
if is_true {
54+
println!("called but not covered");
55+
} else {
56+
println!("absolutely not covered");
57+
}
58+
}
59+
}
60+
61+
pub fn outer_both_covered(is_true: bool) {
62+
println!("called and covered");
63+
inner(is_true);
64+
65+
fn inner(is_true: bool) {
66+
if is_true {
67+
println!("called and covered");
68+
} else {
69+
println!("absolutely not covered");
70+
}
71+
}
72+
}
73+
}
74+
3175
fn main() {
76+
let is_true = std::env::args().len() == 1;
77+
3278
do_not_add_coverage_1();
3379
do_not_add_coverage_2();
3480
add_coverage_1();
3581
add_coverage_2();
82+
83+
nested_fns::outer_not_covered(is_true);
84+
nested_fns::outer(is_true);
85+
nested_fns::outer_both_covered(is_true);
3686
}

0 commit comments

Comments
 (0)