Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2676. MISRA. The result of...
menu mobile close menu
Additional information
toggle menu Contents

V2676. MISRA. The result of 'std::remove', 'std::remove_if', 'std::unique' and 'empty' should be used.

Jul 30 2026

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.

This diagnostic rule is relevant only for C++.

Calling certain functions does not make sense if their results are not used. The result of calling the std::remove, std::remove_if, std::unique, std::empty functions, or the empty container member function should be used in the code. For example, it can be passed to another function, stored in a variable for further logic, or checked in a condition.

If the return value of these functions is not handled in any way, the code usually does not behave as expected. In this case, the state of the objects or the data structure stays the same, which causes the program to behave incorrectly later on during execution.

The first example:

void remove_odd(std::vector<int> &v)
{
  std::remove_if(v.begin(), v.end(),
                 [](auto item)
                 { 
                   return (item & 1) != 0;
                 });
}

Calling the std::remove_if function, despite its name, does not remove elements from the vector. It simply moves them within the vector so that the returned iterator points to the first element that satisfies the predicate. The range of elements from this iterator to the end of the vector is marked for deletion.

To complete the operation and remove the necessary elements from the vector, pass the returned iterator to the std:vector<int>::erase member function:

void remove_odd(std::vector<int> &v)
{
  auto it = std::remove_if(v.begin(), v.end(),
                           [](auto item)
                           {
                             return (item & 1) != 0;
                           });
  v.erase(it, v.end());
}

The second example:

template <typename T>
void copy_elements(const std::vector<T> &src,
                         std::vector<T> &dst)
{
  dst.empty();
  dst.reserve(src.size());
  for (const T &elem : src)
  {
    dst.push_back(elem);
  }
}

The developer intended to clear the resulting vector before populating it. However, calling std::vector<T>::empty does not clear the container; it simply indicates whether it is empty.

To clear the vector, use the std:vector<T>::clear member function:

template <typename T>
void copy_elements(const std::vector<T> &src,
                         std::vector<T> &dst)
{
  dst.clear();
  dst.reserve(src.size());
  for (const T &elem : src)
  {
    dst.push_back(elem);
  }
}

What a quick draw in the Wild West of Coding —
you caught the bug in sec!
But we catch them in milliseconds. How about a duel?

Try free