Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent branch coverage reports for generic functions #394

Open
NicoHaefele opened this issue Oct 30, 2024 · 0 comments
Open

Inconsistent branch coverage reports for generic functions #394

NicoHaefele opened this issue Oct 30, 2024 · 0 comments

Comments

@NicoHaefele
Copy link

👋 Hi,

first of all, thanks for the great work on cargo-llvm-cov.

I noticed that the branch coverage report is not consistent across different output formats.
I am aware that branch coverage is still unstable and relies on nightly rustc,
but I couldn't find an issue for this specific problem.
Hence, I am opening this issue to discuss the observed behaviour
and to provide a minimal example to reproduce the issue.

Given the following Rust code:

pub fn maybe_increment_by<T>(mut value: T, increment: Option<T>) -> T
where
    T: std::ops::AddAssign + Copy,
{
    if let Some(increment) = increment {
        value += increment;
    }
    value
}

#[test]
#[cfg(test)]
fn test_maybe_increment_by() {
    assert_eq!(maybe_increment_by::<u32>(1, Some(2)), 3);
}

The expected behaviour is that the branch coverage is 50% or 1/2,
because the test currently does not account for the branch where increment is None.
All output formats seem to show the correct branch coverage.

However, if I change the test case to cover the None case like this:

#[test]
#[cfg(test)]
fn test_maybe_increment_by() {
    assert_eq!(maybe_increment_by::<u32>(1, Some(2)), 3);
    assert_eq!(maybe_increment_by::<u64>(1, Some(2)), 3);
    assert_eq!(maybe_increment_by::<u64>(1, None), 1);
}

You could argue that the expected branch coverage should be 100% or 2/2.
Some formats, like --html or the standard report format show the expected branch coverage:

cargo +nightly llvm-cov --branch
Filename         Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~/src/lib.rs          10                 0   100.00%           2                 0   100.00%          14                 0   100.00%           2                 0   100.00%
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                 10                 0   100.00%           2                 0   100.00%          14                 0   100.00%           2                 0   100.00%

Other formats, like --cobertura or --lcov show a different branch coverage of 75% or 3/4.
The 3/4 branches are most likely reported due to Rust's monomorphization and the different
types used for the generic function maybe_increment_by.

Now I am wondering if this is a bug or intended behaviour.
If it is the latter, sorry for the noise and thanks in advance for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant