llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.35k stars 12.14k forks source link

Pure virtual destructors (`virtual ~Class() = 0;`) should not trigger `warning: class 'Class' defines a destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions,hicpp-special-member-functions]` #115037

Open SwuduSusuwu opened 3 weeks ago

SwuduSusuwu commented 3 weeks ago
$ cat Class.hxx
class Class {
  public:
  virtual ~Class() = 0;
};
$ cat .clang-tidy
---
Checks: '*'
$ clang-tidy Class.hxx
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "class.hxx"
No compilation database found in /data/data/com.termux/files/home/SubStack or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
2 warnings generated.
/data/data/com.termux/files/home/SubStack/Class.hxx:1:7: warning: class 'Class' defines a destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions,hicpp-special-member-functions]
    1 | class Class {
      |       ^

Pure virtual functions cannot have constructors, thus clang-tidy should not warn that a pure virtual destructor has no constructor to go along.

llvmbot commented 3 weeks ago

@llvm/issue-subscribers-clang-tidy

Author: None (SwuduSusuwu)

``` $ cat Class.hxx class Class { public: virtual ~Class() = 0; }; $ cat .clang-tidy --- Checks: '*' $ clang-tidy Class.hxx Error while trying to load a compilation database: Could not auto-detect compilation database for file "class.hxx" No compilation database found in /data/data/com.termux/files/home/SubStack or any parent directory fixed-compilation-database: Error while opening fixed database: No such file or directory json-compilation-database: Error while opening JSON database: No such file or directory Running without flags. 2 warnings generated. /data/data/com.termux/files/home/SubStack/Class.hxx:1:7: warning: class 'Class' defines a destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions,hicpp-special-member-functions] 1 | class Class { | ^ ``` Pure virtual functions cannot have constructors, thus `clang-tidy` should not warn that a pure virtual destructor has no constructor to go along.
nicovank commented 3 weeks ago

Pure virtual classes cannot have constructors.

As far as I understand, they can, but since the base class cannot be directly instantiated the constructors cannot be used by clients of the code. However, not defining them can lead to issues with move semantics if the pure virtual base class has any members for which moving is desirable. Small Godbolt illustrating possible issue: https://godbolt.org/z/TseTsTc35