Skip to content

Inconsistent coverage reports with files with 0% coverage #85718

Closed

Description

When generating test coverage for a module that is placed behind a feature gate, the source file is omitted from the coverage report unless at least one function has been used in a test. This seems strange, since files that are not behind features are included with 0% test coverage.

Coverage is generated using the steps in the source-based coverage guide, including passing --all-features to cargo test and cargo cov (although I'm unsure if the latter is necessary).

For example, we have an io feature in our codebase. This feature enables the use of the io module:

#[cfg(feature = "io")]
mod io;

If we try and generate a coverage report without making use of anything in this module, but with --all-features enabled, the source file is not included:

genhtml coverage/coverage.info --legend --title="Coverage" --output-directory=coverage
Processing file src/lib.rs
Processing file ...

# `io.rs` is not processed

When we add a test function that uses some element of the io module, it gets included:

genhtml coverage/coverage.info --legend --title="Coverage" --output-directory=coverage
Processing file src/lib.rs
Processing file ...
Processing file src/io.rs

But this is now counted as 100% coverage, although only a couple of lines of code are utilised in the test.

This seems very strange, since we have another file (error.rs) that is not behind any features, and always shows up in coverage results, even though coverage of this file is 0%.

Also, if the #[cfg(feature = "io")] directive before the module include is removed, the problem still persists. It seems very inconsistent, and I'm not quite sure why this is all happening!

Using rustc v1.52.1, on the nightly channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions