Skip to content

Nested functions with #[no_coverage] lead to non-obvious results. #93319

Open
@Swatinem

Description

@Swatinem

The fact that coverage is based on source ranges of function bodies means that an inner functions ranges are being "shadowed" by the outer function. When rendered, the source ranges appear to be covered (by the outer function) even though they are not.

An example from #92695:

    pub fn outer(is_true: bool) {
        println!("called and covered");
        inner_not_covered(is_true);

        #[no_coverage]
        fn inner_not_covered(is_true: bool) {
            if is_true {
                println!("called but not covered");
            } else {
                println!("absolutely not covered");
            }
        }
    }

Pretty printing the coverage report looks like this:

   45|      1|    pub fn outer(is_true: bool) {
   46|      1|        println!("called and covered");
   47|      1|        inner_not_covered(is_true);
   48|      1|
   49|      1|        #[no_coverage]
   50|      1|        fn inner_not_covered(is_true: bool) {
   51|      1|            if is_true {
   52|      1|                println!("called but not covered");
   53|      1|            } else {
   54|      1|                println!("absolutely not covered");
   55|      1|            }
   56|      1|        }
   57|      1|    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-code-coverageArea: Source-based code coverage (-Cinstrument-coverage)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions