Open Quuxplusone opened 6 years ago
I added the beginner tag as this sounds not too complicated.
If someone tries to implement, don't hesitate to ask (here, IRC or mailing list) if you are stuck!
It seems that Clang Static Analyzer already handles this pretty well. For a
class like so:
class Foo {
int x;
public:
void cleanup() { delete this; }
void test1() {
cleanup();
test2();
}
void test2() {}
};
clang-tidy produces:
1 warning generated.
/tmp/tidytest/foo.cpp:13:5: warning: Use of memory after it is freed [clang-analyzer-cplusplus.NewDelete]
test2();
^
[...]
/tmp/tidytest/foo.cpp:9:20: note: Memory is released
void cleanup() { delete this; }
^
Because of that, I believe we do not need to add another check, especially
since without the usage of static analysis, we would need to implement some
kind of heuristics to cover common use cases of `delete this`. Therefore, I
think this ticket can be closed.
I'm not sure about present situation, but in past Static Analyzer worked only in single translation unit, so not all problematic cases could be detected.
It'll be great to bugprone check for delete this in class methods.