Std::ofstream std::ifstream gives false positive "Opened file never closed" error

Hi,

I think sonarcloud generates a false positive for std::ofstream and std::ifstream objects which handles resources themselves. We get “Opened file never closed” error for the following code:

bool createFile(const std::string &filename, const std::string &contents) {
    std::ofstream destFile(filename.c_str(), std::ios::out | std::ios::binary);
    if (destFile.fail()) {
        return false;
    }
    destFile.write(contents.data(), contents.size());
    return !destFile.fail();
}

However, created destFile object will implicitly call close method to clear it’s resources when it goes out of scope.

Do you have any suggestions on how to solve this, rather than suppressing the rule?

Thanks

Hi @manuyavuz-pointr,

thank you for your report, I created a ticket to track it: CPP-2272.