[llvm] 230946f - [ADT] Mark reverse and concat as nodiscard (#115611)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 9 12:19:09 PST 2024
Author: Jakub Kuderski
Date: 2024-11-09T15:19:05-05:00
New Revision: 230946fad69c952dc434aa3e2f92853c1ee8d304
URL: https://github.com/llvm/llvm-project/commit/230946fad69c952dc434aa3e2f92853c1ee8d304
DIFF: https://github.com/llvm/llvm-project/commit/230946fad69c952dc434aa3e2f92853c1ee8d304.diff
LOG: [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.
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 43c9b80edff78e..ace5f60b572d75 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -416,7 +416,8 @@ static constexpr bool HasFreeFunctionRBegin =
} // namespace detail
// Returns an iterator_range over the given container which iterates in reverse.
-template <typename ContainerTy> auto reverse(ContainerTy &&C) {
+// Does not mutate the container.
+template <typename ContainerTy> [[nodiscard]] auto reverse(ContainerTy &&C) {
if constexpr (detail::HasFreeFunctionRBegin<ContainerTy>)
return make_range(adl_rbegin(C), adl_rend(C));
else
@@ -1182,11 +1183,13 @@ template <typename ValueT, typename... RangeTs> class concat_range {
} // end namespace detail
-/// Concatenated range across two or more ranges.
+/// Returns a concatenated range across two or more ranges. Does not modify the
+/// ranges.
///
/// The desired value type must be explicitly specified.
template <typename ValueT, typename... RangeTs>
-detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) {
+[[nodiscard]] detail::concat_range<ValueT, RangeTs...>
+concat(RangeTs &&...Ranges) {
static_assert(sizeof...(RangeTs) > 1,
"Need more than one range to concatenate!");
return detail::concat_range<ValueT, RangeTs...>(
More information about the llvm-commits
mailing list