xd009642 / tarpaulin

A code coverage tool for Rust projects
https://crates.io/crates/cargo-tarpaulin
Apache License 2.0
2.5k stars 180 forks source link

Wrong report for generics #1624

Open 623637646 opened 3 weeks ago

623637646 commented 3 weeks ago

Describe the bug

Wrong code coverage when using Generics.

Using commend: cargo tarpaulin --out html

Platform: Mac 14.5

To Reproduce

Demo code:

pub fn print_123_if_ok<T>(event: Result<T, String>) {
    if let Result::Ok(value) = event {
        println!("123");
    }
}

#[cfg(test)]
mod tests {
    use crate::print_123_if_ok;

    #[test]
    fn test_my_function_none() {
        print_123_if_ok(Ok(1));
    }
}

The report is "66.67% coverage, 2/3 lines covered".

Screenshot 2024-09-19 at 2 34 44 PM

Expected behavior

The report should be "100% coverage, 3/3 lines covered".

623637646 commented 3 weeks ago

Another case which should be 100% coverage:

pub struct Disposal<F>
where
    F: FnOnce(),
{
    action: Option<F>,
}

impl<F> Drop for Disposal<F>
where
    F: FnOnce(),
{
    fn drop(&mut self) {
        if let Some(action) = self.action.take() {
            action();
        }
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_drop() {
        let disposal = super::Disposal {
            action: Some(|| {
                println!("Dropping");
            }),
        };
        drop(disposal);
    }
}
Screenshot 2024-09-19 at 2 45 36 PM
xd009642 commented 3 weeks ago

You can also try adding in --engine llvm to your tarpaulin call and seeing if that improves things :eyes:

623637646 commented 3 weeks ago

@xd009642 I tried cargo tarpaulin --out Html --engine llvm. The issue is still there.