Skip to content

Commit 645f9ee

Browse files
authored
Fixing a [[nodiscard]] error in DXC (microsoft#4860)
This was found while moving to c++20 by an anonymous internal contributor. Integrating the finding to mainline, and was already fixed upstream by @rnk (dd870f6929ee) std::unique iterates on elements of the container, and removes all but the first element matching the predicate, but does not resize the container. This means the following code doesn't assert. ```cpp std::vector<int> v = { 1, 1, 2, 2, 3, 3 }; assert(v.size() == 6); std::unique(v.begin(), v.end()); assert(v.size() == 6); ``` The function returns a past-end iterator, and all the undefined elements are at the end of the container, meaning we can safely erase them, and fix the container size. Signed-off-by: Nathan Gauër <[email protected]>
1 parent b42bbdd commit 645f9ee

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tools/clang/include/clang/Serialization/ContinuousRangeMap.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,17 @@ class ContinuousRangeMap {
117117

118118
~Builder() {
119119
std::sort(Self.Rep.begin(), Self.Rep.end(), Compare());
120-
std::unique(Self.Rep.begin(), Self.Rep.end(),
121-
[](const_reference A, const_reference B) {
122-
// FIXME: we should not allow any duplicate keys, but there are a lot of
123-
// duplicate 0 -> 0 mappings to remove first.
124-
assert((A == B || A.first != B.first) &&
125-
"ContinuousRangeMap::Builder given non-unique keys");
126-
return A == B;
127-
});
120+
Self.Rep.erase(
121+
std::unique(
122+
Self.Rep.begin(), Self.Rep.end(),
123+
[](const_reference A, const_reference B) {
124+
// FIXME: we should not allow any duplicate keys, but there are
125+
// a lot of duplicate 0 -> 0 mappings to remove first.
126+
assert((A == B || A.first != B.first) &&
127+
"ContinuousRangeMap::Builder given non-unique keys");
128+
return A == B;
129+
}),
130+
Self.Rep.end());
128131
}
129132

130133
void insert(const value_type &Val) {

0 commit comments

Comments
 (0)