Skip to content

Commit 230946f

Browse files
authored
[ADT] Mark reverse and concat as nodiscard (#115611)
It may not be immediately obvious if these two functions modify the given ranges or return a view over them. We have seen downstream code that mistakenly assumed the given range would be mutated. Add the `[[nodiscard]]` attribute to prevent these errors. Also clarify the lack of mutation in the documentation comments.
1 parent f8fea5d commit 230946f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ static constexpr bool HasFreeFunctionRBegin =
416416
} // namespace detail
417417

418418
// Returns an iterator_range over the given container which iterates in reverse.
419-
template <typename ContainerTy> auto reverse(ContainerTy &&C) {
419+
// Does not mutate the container.
420+
template <typename ContainerTy> [[nodiscard]] auto reverse(ContainerTy &&C) {
420421
if constexpr (detail::HasFreeFunctionRBegin<ContainerTy>)
421422
return make_range(adl_rbegin(C), adl_rend(C));
422423
else
@@ -1182,11 +1183,13 @@ template <typename ValueT, typename... RangeTs> class concat_range {
11821183

11831184
} // end namespace detail
11841185

1185-
/// Concatenated range across two or more ranges.
1186+
/// Returns a concatenated range across two or more ranges. Does not modify the
1187+
/// ranges.
11861188
///
11871189
/// The desired value type must be explicitly specified.
11881190
template <typename ValueT, typename... RangeTs>
1189-
detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) {
1191+
[[nodiscard]] detail::concat_range<ValueT, RangeTs...>
1192+
concat(RangeTs &&...Ranges) {
11901193
static_assert(sizeof...(RangeTs) > 1,
11911194
"Need more than one range to concatenate!");
11921195
return detail::concat_range<ValueT, RangeTs...>(

0 commit comments

Comments
 (0)