[libc++][ranges] Applied [[nodiscard]] to common_view (#173176)
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html - https://wg21.link/ranges - https://wg21.link/range.common Towards #172124
This commit is contained in:
parent
c15a73e96d
commit
b6bf708a05
2 changed files with 68 additions and 7 deletions
|
|
@ -56,16 +56,16 @@ public:
|
|||
return __base_;
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
|
||||
if constexpr (random_access_range<_View> && sized_range<_View>)
|
||||
return ranges::begin(__base_);
|
||||
else
|
||||
return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::begin(__base_));
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
|
||||
requires range<const _View>
|
||||
{
|
||||
if constexpr (random_access_range<const _View> && sized_range<const _View>)
|
||||
|
|
@ -74,14 +74,14 @@ public:
|
|||
return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::begin(__base_));
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto end() {
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
|
||||
if constexpr (random_access_range<_View> && sized_range<_View>)
|
||||
return ranges::begin(__base_) + ranges::size(__base_);
|
||||
else
|
||||
return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::end(__base_));
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto end() const
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
|
||||
requires range<const _View>
|
||||
{
|
||||
if constexpr (random_access_range<const _View> && sized_range<const _View>)
|
||||
|
|
@ -90,13 +90,13 @@ public:
|
|||
return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::end(__base_));
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto size()
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
|
||||
requires sized_range<_View>
|
||||
{
|
||||
return ranges::size(__base_);
|
||||
}
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto size() const
|
||||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
|
||||
requires sized_range<const _View>
|
||||
{
|
||||
return ranges::size(__base_);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// REQUIRES: std-at-least-c++20
|
||||
|
||||
// Check that functions are marked [[nodiscard]]
|
||||
|
||||
#include <ranges>
|
||||
#include <utility>
|
||||
|
||||
struct NonCommonView : std::ranges::view_base {
|
||||
int* begin() const;
|
||||
const int* end() const;
|
||||
|
||||
int* base();
|
||||
|
||||
int* begin();
|
||||
const int* end();
|
||||
|
||||
constexpr std::size_t size() const { return 0; };
|
||||
};
|
||||
static_assert(!std::ranges::common_range<NonCommonView>);
|
||||
|
||||
void test() {
|
||||
NonCommonView range;
|
||||
|
||||
auto v = std::views::common(range);
|
||||
|
||||
// [range.common.view]
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::move(v).base();
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
v.begin();
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::as_const(v).begin();
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
v.end();
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::as_const(v).end();
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
v.size();
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::as_const(v).size();
|
||||
|
||||
// [range.common.overview]
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::views::common(v);
|
||||
|
||||
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
|
||||
std::views::common(range);
|
||||
}
|
||||
Loading…
Reference in a new issue