Clang tidy未检测到缺少虚拟析构函数。

huangapple go评论82阅读模式
英文:

Clang tidy does not detect missing virtual destructor

问题

请参考以下 MWE 代码:
reference.cpp:

#include <iostream>

class A {
public:
    virtual void foo() = 0;
};

class B : public A {
public:
    void foo() override {
        std::cout << "test";
    }
};

如果我在这上面运行 clang-tidy,命令如下:clang-tidy -checks="cppcoreguidelines-virtual-class-destructor" ./reference.cpp --,我将获得如下期望的输出:

42 warnings generated.
/workspace/test/reference.cpp:3:7: warning: destructor of 'A' is public and non-virtual [cppcoreguidelines-virtual-class-destructor]
class A {
      ^
/workspace/test/reference.cpp:3:7: note: make it public and virtual
class A {
      ^
/workspace/test/reference.cpp:8:7: warning: destructor of 'B' is public and non-virtual [cppcoreguidelines-virtual-class-destructor]
class B : public A {
      ^
/workspace/test/reference.cpp:8:7: note: make it public and virtual
class B : public A {
      ^
Suppressed 40 warnings (40 in non-user code).

让我们修复 A:

#include <iostream>

class A {
public:
    virtual void foo() = 0;
    virtual ~A() = default;
};

class B : public A {
public:
    void foo() override {
        std::cout << "test";
    }
};

然后再次运行 clang-tidy:

40 warnings generated.
Suppressed 40 warnings (40 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

所以,A 已经修复了,但是 B 中仍然没有虚析构函数,也没有警告。要如何修复它?

PS:

clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 14.0.5
  Optimized build.
  Default target: x86_64-redhat-linux-gnu
  Host CPU: skylake
英文:

Consider following MWE:
reference.cpp:

#include &lt;iostream&gt;

class A {
    public:
        virtual void foo() = 0;
};

class B : public A {
    public:
        void foo() override {
            std::cout &lt;&lt; &quot;test&quot;;
        }
};

If I run clang-tidy on that, clang-tidy -checks=&quot;cppcoreguidelines-virtual-class-destructor&quot; ./reference.cpp -- I'll get expected output:

42 warnings generated.
/workspace/test/reference.cpp:3:7: warning: destructor of &#39;A&#39; is public and non-virtual [cppcoreguidelines-virtual-class-destructor]
class A {
      ^
/workspace/test/reference.cpp:3:7: note: make it public and virtual
class A {
      ^
/workspace/test/reference.cpp:8:7: warning: destructor of &#39;B&#39; is public and non-virtual [cppcoreguidelines-virtual-class-destructor]
class B : public A {
      ^
/workspace/test/reference.cpp:8:7: note: make it public and virtual
class B : public A {
      ^
Suppressed 40 warnings (40 in non-user code).

Let's fix A:

#include &lt;iostream&gt;

class A {
    public:
        virtual void foo() = 0;
        virtual ~A() = default;
};

class B : public A {
    public:
        void foo() override {
            std::cout &lt;&lt; &quot;test&quot;;
        }
};

And run clang-tidy again:

40 warnings generated.
Suppressed 40 warnings (40 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

So, A is fixed, but there's still no virtual destructor in B. And no warning. How do I fix it?

PS:

clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 14.0.5
  Optimized build.
  Default target: x86_64-redhat-linux-gnu
  Host CPU: skylake

答案1

得分: 3

派生类的析构函数如果任何基类析构函数是虚拟的,就会自动变为 virtual

在派生类中手动添加它将会是多余的,可能会导致性能下降,因为这会阻止隐式声明移动特殊成员函数。

英文:

Destructors of derived class are automatically virtual if any base class destructor is virtual.

Adding it manually to the derived class would be redundant and potentially a pessimization, because it would prevent the implicit declaration of move special member functions.

huangapple
  • 本文由 发表于 2023年7月28日 05:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定