-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[libc++][variant] Applied [[nodiscard]]
#172058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
H-G-Hristov
wants to merge
1
commit into
llvm:main
Choose a base branch
from
H-G-Hristov:hgh/libcxx/nodiscard-to-variant
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+93
−17
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
802a27e to
f5d97e7
Compare
`[[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/variant
f5d97e7 to
7c9edbd
Compare
84 tasks
Member
|
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) Changes
Full diff: https://github.com/llvm/llvm-project/pull/172058.diff 2 Files Affected:
diff --git a/libcxx/include/variant b/libcxx/include/variant
index df587ccf23843..b93009b3353cb 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -288,7 +288,7 @@ _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
class _LIBCPP_EXPORTED_FROM_ABI bad_variant_access : public exception {
public:
- const char* what() const _NOEXCEPT override;
+ [[__nodiscard__]] const char* what() const _NOEXCEPT override;
};
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
@@ -1282,11 +1282,11 @@ public:
return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
}
- _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
return __impl_.valueless_by_exception();
}
- _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
template < bool _Dummy = true,
enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
@@ -1329,7 +1329,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
@@ -1343,21 +1343,23 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto&& __generic_get(_Vp&& __v) {
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(variant<_Types...>& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&
+get(variant<_Types...>& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return std::__generic_get<_Ip>(__v);
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(variant<_Types...>&& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
+get(variant<_Types...>&& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
return std::__generic_get<_Ip>(std::move(__v));
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
get(const variant<_Types...>& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
@@ -1365,7 +1367,7 @@ get(const variant<_Types...>& __v) {
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
get(const variant<_Types...>&& __v) {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
@@ -1373,25 +1375,25 @@ get(const variant<_Types...>&& __v) {
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
static_assert(!is_void_v<_Tp>);
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
static_assert(!is_void_v<_Tp>);
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
static_assert(!is_void_v<_Tp>);
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
static_assert(!is_void_v<_Tp>);
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
}
@@ -1403,7 +1405,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
get_if(variant<_Types...>* __v) noexcept {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
@@ -1411,7 +1413,7 @@ get_if(variant<_Types...>* __v) noexcept {
}
template <size_t _Ip, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
get_if(const variant<_Types...>* __v) noexcept {
static_assert(_Ip < sizeof...(_Types));
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
@@ -1419,13 +1421,13 @@ get_if(const variant<_Types...>* __v) noexcept {
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
static_assert(!is_void_v<_Tp>);
return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
template <class _Tp, class... _Types>
-_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
static_assert(!is_void_v<_Tp>);
return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
@@ -1606,7 +1608,7 @@ struct hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>
using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
# endif
- _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
using __variant_detail::__visitation::__variant;
size_t __res =
__v.valueless_by_exception()
diff --git a/libcxx/test/libcxx/utilities/variant/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/variant/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..b74a66f446311
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/variant/nodiscard.verify.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++17
+
+// Check that functions are marked [[nodiscard]]
+
+#include <variant>
+
+#include <test_macros.h>
+
+void test() {
+ {
+ std::bad_variant_access ex;
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ ex.what();
+ }
+
+ {
+ struct First {};
+ struct Second {};
+
+ std::variant<First, Second> v;
+ const std::variant<First, Second> cv;
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cv.valueless_by_exception();
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cv.index();
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::holds_alternative<First>(v);
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(v);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(cv);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(std::move(v));
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(std::move(cv));
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<First>(v);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<First>(cv);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<First>(std::move(v));
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<First>(std::move(cv));
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get_if<0>(&v);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get_if<0>(&cv);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get_if<First>(&v);
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get_if<First>(&cv);
+ }
+
+ {
+ std::hash< std::variant<int, float>> hash;
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ hash(std::variant<int, float>{});
+ }
+}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[[nodiscard]]should be applied to functions where discarding the return value is most likely a correctness issue.