# V2020. The loop body contains the 'break;' / 'continue;' statement. This may complicate the control flow.

This diagnostic rule was added at users' request.

The diagnostic detects the use of 'break;' and 'continue;' statements inside loop bodies. The diagnostic also aids in code refactoring and prevents errors when legacy code inside a loop is changed to a new one.

Here is a synthetic example:

    namespace fs = std::filesystem;

    std::vector<fs::path> existingPaths;

    void SaveExistingPaths(const std::vector<fs::path> &paths)
    {
      for (auto &&path: paths)
      {
        if (!fs::exists(path))
        {
          break; // <=
        }

        existingPaths.emplace_back(path);
      }
    }

In the above code snippet, all existing paths were supposed to be stored in a separate container. If the path doesn't exist, the loop will break since 'break;' is used. In this case, the 'continue;' statement should have been used instead. However, removing the 'break;' and 'continue;' statements from the loop would be even better.

    namespace fs = std::filesystem;

    std::vector<fs::path> existingPaths;

    void SaveExistingPaths(const std::vector<fs::path> &paths)
    {
      for (auto &&path: paths)
      {
        if (fs::exists(path))
        {
          existingPaths.emplace_back(path);
        }
      }
    }

Note. This diagnostic is disabled by default in order to prevent issuing too many warnings.

You can enable this diagnostic rule for the analyzed file by the following comment:

    //+V::2020

You can also add this comment to the standard header file. This will enable the diagnostic rule for all files that include this header file. For example, you can add the comment in 'stdafx.h'.

You can use the '#pragma pvs' directive to enable the diagnostic for a certain block of code.

    namespace fs = std::filesystem;

    std::vector<fs::path> existingPaths;

    #pragma pvs(push)
    #pragma pvs(enable: 2020)
    void SaveExistingPaths(const std::vector<fs::path> &paths)
    {

      for (auto &&path: paths)
      {
        if (!fs::exists(path))
        {
          break; // <= V2020
        }

        existingPaths.emplace_back(path);
      }
    }
    #pragma pvs(pop)