Skip to content

Commit 0f52a3e

Browse files
phsymsunjay
authored andcommitted
Prevent dead-code-elimination before coverage (#5)
* Added an uncovered function. * Added RUSTFLAGS="-C link-dead-code" env variable to travis script * Updated readme with a note on dead-code elimination * Changed the wording in the sections added to the README and example
1 parent c49d31f commit 0f52a3e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ matrix:
99
allow_failures:
1010
- rust: nightly
1111

12+
env:
13+
global:
14+
- RUSTFLAGS="-C link-dead-code"
15+
1216
before_install:
1317
- sudo apt-get update
1418

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ for each test executable and store the results in separate directories.**
3737
Codecov will automatically find and upload the cobertura.xml files and
3838
merge the coverage for you.
3939

40+
Note that setting the environment variable `RUSTFLAGS="-C link-dead-code"`
41+
during tests build may improve coverage accuracy by preventing dead-code elimination.
42+
Do not set this variable when creating release builds since it will increase
43+
binary size.
44+
4045
After you've run the tests and created a cobertura.xml report, you can
4146
use [the Codecov global uploader][4] to push that report to Codecov.
4247
See below for further details.
@@ -71,6 +76,10 @@ matrix:
7176
allow_failures:
7277
- rust: nightly
7378

79+
env:
80+
global:
81+
- RUSTFLAGS="-C link-dead-code"
82+
7483
before_install:
7584
- sudo apt-get update
7685

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ pub fn which(face: &str) -> &'static str {
3535
}
3636
}
3737

38+
/// This function is not called during tests, so it will be considered dead code.
39+
/// By default, and because of dead-code elimination it won't be reported as uncovered
40+
/// since the function will be removed from executable.
41+
/// This is accounted for in the Travis configuration by passing the compiler flag
42+
/// `-C link-dead-code` when building the tests. This flag disables dead code
43+
/// elimination and allows this function to be reported correctly.
44+
pub fn not_called() {
45+
println!("This is dead code");
46+
unreachable!();
47+
}
48+
3849
#[cfg(test)]
3950
mod tests {
4051
use super::*;

0 commit comments

Comments
 (0)