Skip to content

Conversation

@H-G-Hristov
Copy link
Contributor

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.

Towards #172124

`[[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.empty Towards llvm#172124
@H-G-Hristov H-G-Hristov requested a review from a team as a code owner December 22, 2025 05:09
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2025

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

Changes

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.

Towards #172124


Full diff: https://github.com/llvm/llvm-project/pull/173215.diff

2 Files Affected:

  • (modified) libcxx/include/__ranges/empty_view.h (+5-5)
  • (added) libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp (+30)
diff --git a/libcxx/include/__ranges/empty_view.h b/libcxx/include/__ranges/empty_view.h index fc08492110f53..54d62b3c77a78 100644 --- a/libcxx/include/__ranges/empty_view.h +++ b/libcxx/include/__ranges/empty_view.h @@ -29,11 +29,11 @@ template <class _Tp> requires is_object_v<_Tp> class empty_view : public view_interface<empty_view<_Tp>> { public: - _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* begin() noexcept { return nullptr; } - _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* end() noexcept { return nullptr; } - _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* data() noexcept { return nullptr; } - _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 0; } - _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return true; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* begin() noexcept { return nullptr; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* end() noexcept { return nullptr; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* data() noexcept { return nullptr; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 0; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return true; } }; template <class _Tp> diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp new file mode 100644 index 0000000000000..7d62b57d7f92e --- /dev/null +++ b/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +void test() { + auto v = std::views::empty<int>; + + // [range.empty.view] + + // 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}} + v.end(); + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + v.data(); + // 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}} + v.empty(); +} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

2 participants