Skip to content

Missed coverage for Constructor #1739

@AMS21

Description

@AMS21

Describe the bug
Tarpaulin shows that 2 lines of the constructor are not covered but if I step through with a debugger I can see that they are actually hit.

To Reproduce
Give a simple rust project with the following code:

lib.rs

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vector2<T> {
    pub x: T,
    pub y: T,
}

impl<T> Vector2<T> {
    pub const fn new(x: T, y: T) -> Self {
        Self { x, y }
    }

    pub fn length(&self) -> f32
    where
        T: Into<f32> + Copy,
    {
        let x: f32 = self.x.into();
        let y: f32 = self.y.into();

        x.hypot(y)
    }

    pub fn normalize(&self) -> Self
    where
        T: Into<f32> + Copy + From<f32>,
    {
        let length = self.length();
        if length == 0.0 {
            return *self;
        }

        Self {
            x: T::from(self.x.into() / length), // Tarpaulin claims this and the next line are uncovered
            y: T::from(self.y.into() / length),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_vector2_normalize() {
        let v = Vector2::new(3.0, 4.0);
        let normalized = v.normalize();
        assert!((normalized.length() - 1.0).abs() < f32::EPSILON);

        let v = Vector2::new(0.0, 0.0);
        let normalized = v.normalize();
        assert_eq!(normalized, v);
        assert_eq!(normalized.length(), 0.0);
    }
}

running the following command

cargo +nightly tarpaulin --verbose --locked --out Xml --engine llvm

produces the following output on my machine

2025-05-16T12:21:40.919547Z DEBUG cargo_tarpaulin: set up logging
2025-05-16T12:21:40.919562Z  INFO cargo_tarpaulin::config: Creating config
2025-05-16T12:21:40.960219Z  INFO cargo_tarpaulin: Running Tarpaulin
2025-05-16T12:21:40.960229Z  INFO cargo_tarpaulin: Building project
2025-05-16T12:21:40.960231Z  INFO cargo_tarpaulin::cargo: Cleaning project
   Compiling coverage_bug v0.1.0 (/mnt/data/dev/coverage_bug)
     Running `/usr/bin/sccache /home/andre/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name coverage_bug --edition=2024 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=153 --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --test --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=48db1fca60e8e9cb -C extra-filename=-6b78c43e30e4f40f --out-dir /mnt/data/dev/coverage_bug/target/debug/deps -L dependency=/mnt/data/dev/coverage_bug/target/debug/deps -Cdebuginfo=2 -Cstrip=none --cfg=tarpaulin -Cinstrument-coverage`
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.06s
2025-05-16T12:21:41.049231Z DEBUG cargo_tarpaulin::cargo: Linker paths: ["/home/andre/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"]
2025-05-16T12:21:41.049709Z DEBUG cargo_tarpaulin::source_analysis: Found function: Vector2<T>::new
2025-05-16T12:21:41.049786Z DEBUG cargo_tarpaulin::source_analysis: Found function: Vector2<T>::length
2025-05-16T12:21:41.049820Z DEBUG cargo_tarpaulin::source_analysis: Found function: Vector2<T>::normalize
2025-05-16T12:21:41.049898Z  INFO cargo_tarpaulin::process_handling: running /mnt/data/dev/coverage_bug/target/debug/deps/coverage_bug-6b78c43e30e4f40f
2025-05-16T12:21:41.049904Z DEBUG cargo_tarpaulin::process_handling: Current working dir: Ok("/mnt/data/dev/coverage_bug")
2025-05-16T12:21:41.049954Z  INFO cargo_tarpaulin::process_handling: Setting LLVM_PROFILE_FILE
2025-05-16T12:21:41.049958Z DEBUG cargo_tarpaulin::process_handling: Env vars: [("CARGO", "/home/andre/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"), ("CARGO_HOME", "/home/andre/.cargo"), ("CARGO_INCREMENTAL", "false"), ("HOME", "/home/andre"), ("LANG", "en_GB.UTF-8"), ("LANGUAGE", ""), ("LC_ADDRESS", "de_DE.UTF-8"), ("LC_IDENTIFICATION", "de_DE.UTF-8"), ("LC_MEASUREMENT", "de_DE.UTF-8"), ("LC_MONETARY", "de_DE.UTF-8"), ("LC_NAME", "de_DE.UTF-8"), ("LC_NUMERIC", "de_DE.UTF-8"), ("LC_PAPER", "de_DE.UTF-8"), ("LC_TELEPHONE", "de_DE.UTF-8"), ("LC_TIME", "de_DE.UTF-8"), ("LESS", "-R"), ("LIBVA_DRIVER_NAME", "nvidia"), ("LOGNAME", "andre"), ("PATH", "/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/rustup/bin:/home/andre/.cargo/bin), ("PWD", "/mnt/data/dev/coverage_bug"), ("RUSTC_WRAPPER", "/usr/bin/sccache"), ("RUSTUP_HOME", "/home/andre/.rustup"), ("RUSTUP_TOOLCHAIN", "nightly-x86_64-unknown-linux-gnu"), ("RUST_RECURSION_COUNT", "1"), ("SCCACHE_CACHE_SIZE", "50G"), ("SCCACHE_DIRECT", "true"), ("SHELL", "/bin/zsh"), ("SSL_CERT_DIR", "/etc/ssl/certs"), ("SSL_CERT_FILE", "/etc/ssl/cert.pem"), ("USER", "andre"), ("_", "/usr/bin/cargo"), ("RUST_BACKTRACE", "1"), ("CARGO_PKG_NAME", "coverage_bug"), ("CARGO_PKG_VERSION", "0.1.0"), ("CARGO_PKG_AUTHORS", ""), ("CARGO_MANIFEST_DIR", "/mnt/data/dev/coverage_bug"), ("LD_LIBRARY_PATH", "/home/andre/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/andre/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib"), ("RUSTFLAGS", "-Cdebuginfo=2 -Cstrip=none --cfg=tarpaulin -Cinstrument-coverage"), ("RUST_TEST_THREADS", "1"), ("LLVM_PROFILE_FILE", "/mnt/data/dev/coverage_bug/target/tarpaulin/profraws/coverage_bug-6b78c43e30e4f40f_%m-%p.profraw")]
2025-05-16T12:21:41.049991Z DEBUG cargo_tarpaulin::process_handling: Args: []

running 1 test
test tests::test_vector2_normalize ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

2025-05-16T12:21:41.050893Z  INFO cargo_tarpaulin::statemachine::instrumented: For binary: target/debug/deps/coverage_bug-6b78c43e30e4f40f
2025-05-16T12:21:41.050898Z  INFO cargo_tarpaulin::statemachine::instrumented: Generated: target/tarpaulin/profraws/coverage_bug-6b78c43e30e4f40f_4597535283048661607_0-140307.profraw
2025-05-16T12:21:41.050900Z  INFO cargo_tarpaulin::statemachine::instrumented: Merging coverage reports
2025-05-16T12:21:41.050937Z  INFO cargo_tarpaulin::statemachine::instrumented: Mapping coverage data to source
2025-05-16T12:21:41.052315Z  INFO cargo_tarpaulin::report: Coverage Results:
|| Uncovered Lines:
|| src/lib.rs: 32-33
|| Tested/Total Lines:
|| src/lib.rs: 9/11 +0.00%
|| 
81.82% coverage, 9/11 lines covered, +0.00% change in coverage

Expected behavior
As these lines are actually hit tarpaulin should show 100% coverage.

Versions

rustc 1.89.0-nightly (d97326eab 2025-05-15)
cargo 1.89.0-nightly (056f5f4f3 2025-05-09)
cargo-tarpaulin-tarpaulin 0.32.5

OS info:

Linux 6.14.6-2-cachyos x64/AMD64

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions