OutpostUniverse / OP2Utility

C++ library for working with Outpost 2 related files and tasks.
MIT License
4 stars 0 forks source link

Redundant check in inner loop #320

Closed DanRStevens closed 4 years ago

DanRStevens commented 4 years ago

Within XFile::GetFilenamesFromDirectory there is a check against filenames.size() == 0 which appears to be redundant. The loop conditions already skip the loop when size() == 0 (initially), so this check within the loop does nothing. It may have made sense if the size was updated to be 0, however, the size can never be updated to 0 until the loop is finished anyway. Finally, other similar methods (regex filter) don't have a corresponding check.

The method in question is:

std::vector<std::string> XFile::GetFilenamesFromDirectory(const std::string& directory, const std::string& extension)
{
    std::vector<std::string> filenames = GetFilenamesFromDirectory(directory);

    // Loop starts at index size - 1 and ends after index 0 executes
    for (std::size_t i = filenames.size(); i-- > 0; )
    {
        if (filenames.size() == 0) {
            return filenames;
        }

        if (fs::path(filenames[i]).extension().string() != extension) {
            filenames.erase(filenames.begin() + i);
        }
    }

    return filenames;
}