This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] The check should ignore final classes
ClosedPublic

Authored by steakhal on Jun 2 2022, 9:26 AM.

Details

Summary

The cppcoreguidelines-virtual-class-destructor supposed to enforce
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-non-virtual

Quote:

A base class destructor should be either public and virtual, or
protected and non-virtual

[emphasis mine]

However, this check still rules the following case:

class MostDerived final : public Base {
public:
  MostDerived() = default;
  ~MostDerived() = default;
  void func() final;
};

Even though MostDerived class is marked final, thus it should not be
considered as a base class. Consequently, the rule is satisfied, yet
the check still flags this code.

In this patch, I'm proposing to ignore final classes since they cannot
be base classes.

Diff Detail

Event Timeline

steakhal created this revision.Jun 2 2022, 9:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2022, 9:26 AM
steakhal requested review of this revision.Jun 2 2022, 9:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2022, 9:26 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Hmm, MostDerived does have a public virtual destructor in your example already - if the base class destructor is virtual, the child class destructor is virtual. In that sense the check should not warn.

Seems like there's some deeper problem in the check?

Hmm, MostDerived does have a public virtual destructor in your example already - if the base class destructor is virtual, the child class destructor is virtual. In that sense the check should not warn.

Seems like there's some deeper problem in the check?

Not quite. None of the destructors are virtual in the example. In fact, the MostDerived has a public and non-virtual destructor.

Hmm, MostDerived does have a public virtual destructor in your example already - if the base class destructor is virtual, the child class destructor is virtual. In that sense the check should not warn.

Seems like there's some deeper problem in the check?

Not quite. None of the destructors are virtual in the example. In fact, the MostDerived has a public and non-virtual destructor.

Doesn't Base have a virtual destructor? As per the standard:

If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly-declared) is virtual.

To clarify: I'm not opposed to the patch, I'm just trying to understand if the AST is indeed implementing the Standard correctly and classifying the Derived destructor as virtual.

Oh, I see the unit test now, indeed Base does not have a virtual destructor. LGTM then!

carlosgalvezp added inline comments.Jun 3 2022, 1:19 AM
clang-tools-extra/docs/ReleaseNotes.rst
170

I believe the convention is to use double-backtick for C++ keywords and functions, see line 160.

steakhal added inline comments.Jun 3 2022, 2:00 AM
clang-tools-extra/docs/ReleaseNotes.rst
170

Thanks, I'll fix it. I should learn rst for once.

steakhal updated this revision to Diff 435054.Jun 8 2022, 12:00 AM
steakhal marked an inline comment as done.
  • rebase
  • use double-backticks for the keyword final in the Release Notes.

polite ping

whisperity accepted this revision.Jun 19 2022, 11:36 PM

Thank you!

clang-tools-extra/docs/ReleaseNotes.rst
170

(Nit: classes marked final)

This revision is now accepted and ready to land.Jun 19 2022, 11:36 PM

(Please ensure a more appropriate commit message that actually mentions the check when committing, @steakhal.)

This revision was automatically updated to reflect the committed changes.
steakhal marked an inline comment as done.