Skip to content

Commit eaec88f

Browse files
ClaytonKnittelcopybara-github
authored andcommitted
Pass a const reference to the user-provided lambda in RepeatedField's proto2::erase_if.
It is an anti-pattern to mutate elements in a repeated field as part of this callback, which is meant to only determine whether to erase an element. PiperOrigin-RevId: 882806181
1 parent 40d48c3 commit eaec88f

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/google/protobuf/repeated_field.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,8 @@ inline size_t RepeatedField<Element>::SpaceUsedExcludingSelfLong() const {
13841384
// Like C++20's std::erase_if, for RepeatedField
13851385
template <typename T, typename Pred>
13861386
size_t erase_if(RepeatedField<T>& cont, Pred pred) {
1387-
auto it = std::remove_if(cont.begin(), cont.end(), pred);
1387+
auto it = std::remove_if(cont.begin(), cont.end(),
1388+
[&pred](const auto& elem) { return pred(elem); });
13881389
const size_t removed = cont.end() - it;
13891390
cont.Truncate(cont.size() - removed);
13901391
return removed;

0 commit comments

Comments
 (0)