Should I exclude virtual destructor from code coverage? [closed]

1 week ago 11
ARTICLE AD BOX

We have a virtual destructor declared in our class:

class HeightCalculator { public: HeightCalculator() = default; virtual ~HeightCalculator() = default;

LCOV then complains that we don’t cover the destructor. This seems trivial to me and I don’t feel like testing various abstractions is necessary. What is best practice here?

jonrsharpe's user avatar

jonrsharpe

123k31 gold badges277 silver badges488 bronze badges

Oskar Paulsson's user avatar

5

It probably depends on how you use the coverage information. Without that context, it doesn't matter whether you measure it or note.

2025-11-25 14:14:30 +00:00

Commented 2 days ago

Do you have any tests that hold a polymorphic HeightCalculator? If not then you may as well remove the definition. If you do then LCOV is reporting a false positive

2025-11-25 14:37:40 +00:00

Commented 2 days ago

I don't think it's possible to have a test for polymorphic destruction. You can perform polymorphic delete inside a test, and that will require a virtual destructor... but that doesn't test for correctness: the possible outcomes are success, undefined behavior, and (possibly-but-not-guaranteed) compilation failure.

2025-11-25 15:44:44 +00:00

Commented 2 days ago

I don't know if it is feasible to get this covered or not, but don't fall into the trap of tool satisfaction either. Reaching a 100% code coverage should not be your goal. If you don't cover something and it isn't a risk (like a defaulted virtual destructor) then you should stop.

2025-11-25 15:51:09 +00:00

Commented 2 days ago

You can't determine whether having a virtual destructor is correct just based on LCOV. It depends on how it is used and whether or not it is inherited from. There's no binary yes/no answer. Also; please provide a minimal reproducible example.

2025-11-25 19:11:15 +00:00

Commented yesterday

Read Entire Article