S2095 reports resources that are not closed. It seems to ignore when the resources are closed using scoped objects to close the resource. E.g. a custom written scope_exit
.
Example:
class scope_exit {
public:
using ExitFunc = std::function<void()>;
explicit scope_exit(ExitFunc exitFunc) :
_exitFunc(std::move(exitFunc)) {
}
~scope_exit() {
if (_exitFunc) {
_exitFunc();
}
}
scope_exit(const scope_exit &) = delete;
scope_exit& operator=(const scope_exit &) = delete;
scope_exit(scope_exit &&other) noexcept {
_exitFunc.swap(other._exitFunc);
other._exitFunc = nullptr;
}
scope_exit& operator=(scope_exit &&other) noexcept {
if (this != &other) {
_exitFunc();
_exitFunc.swap(other._exitFunc);
other._exitFunc = nullptr;
}
return *this;
}
private:
ExitFunc _exitFunc;
};
int main() {
FILE* file = fopen("c:\\temp\\somefile.bin", "rb");
scope_exit scoped([file] {
fclose(file);
});
return 0;
}
I can see how this is a complex situation to analyze. Is there any way I can modify the scope_exit class to indicate to SonarLint that is in fact handling the resource?