taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
920 stars 57 forks source link

docs: Use #[coverage(off)] instead of #[no_coverage] #314

Closed taiki-e closed 1 year ago

taiki-e commented 1 year ago

Closes #312

See https://github.com/rust-lang/rust/pull/114656 and https://github.com/taiki-e/cargo-llvm-cov/issues/312 for the context.

Exclude function from coverage

To exclude the specific function from coverage, use the #[coverage(off)] attribute.

Since #[coverage(off)] is unstable, it is recommended to use it together with cfg(coverage) or cfg(coverage_nightly) set by cargo-llvm-cov.

#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

#[cfg_attr(coverage_nightly, coverage(off))]
fn exclude_from_coverage() {
    // ...
}

...

If you want to ignore all #[test]-related code, consider using [coverage-helper] crate version 0.2+.

cargo-llvm-cov excludes code contained in the directory named tests from the report by default, so you can also use it instead of coverage-helper crate.

Note: #[coverage(off)] was previously named #[no_coverage]. When using #[no_coverage] in the old nightly, replace feature(coverage_attribute) with feature(no_coverage), coverage(off) with no_coverage, and coverage-helper 0.2+ with coverage-helper 0.1.