| Issue |
183101
|
| Summary |
clang-tidy: forbid inheriting from classes with non-virtual destructors
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
AssortedBits
|
## goal
In my project, I want to disallow the following memory leak:
1. class `A` has a non-virtual destructor
2. class `B` inherits from class `A`, and has data members
3. someone calls `delete` upon an `A*` that points to an instance of `B`
that is:
```
class A {};
class B : public A
{
int data;
};
A* a = new B();
delete a; //leaks B::data, because A::~A() is not virtual.
```
## ideas for implementation
There are multiple ways to implement this.
The actual precise rule would be kinda complicated:
> Disallow inheriting from a type (class or struct) when the destructor on all base-most types (remember multiple inheritance 😵💫) up the inheritance chain are not `virtual`, **and** the inheriting type contains data members.
A very simple good-enough rule, however, would be:
> Require `override` keyword on destructors of all types (classes or structs) that inherit from any other type.
The compiler would then catch the rest.
Would have some false-failures, because there's "no harm, no foul" cases where inheriting classes add no data members and have no side-effects in destructors. But, fine, those are rare, and I'd happily hand-exempt those if I had this rule.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs