Skip to content

Commit a2f3830

Browse files
committed
[libc++][string] Applied [[nodiscard]] to non-member functions
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
1 parent d162c91 commit a2f3830

File tree

2 files changed

+105
-46
lines changed

2 files changed

+105
-46
lines changed

libcxx/include/string

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,46 +3851,52 @@ swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Tra
38513851
__lhs.swap(__rhs);
38523852
}
38533853

3854-
_LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
3855-
_LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
3856-
_LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
3857-
_LIBCPP_EXPORTED_FROM_ABI long long stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
3858-
_LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
3859-
3860-
_LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
3861-
_LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
3862-
_LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
3863-
3864-
_LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
3865-
_LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
3866-
_LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
3867-
_LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
3868-
_LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
3869-
_LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
3870-
_LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
3871-
_LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
3872-
_LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
3854+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
3855+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3856+
stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
3857+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
3858+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3859+
stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
3860+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3861+
stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
3862+
3863+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
3864+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
3865+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
3866+
3867+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
3868+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
3869+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
3870+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
3871+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
3872+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
3873+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
3874+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
3875+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
38733876

38743877
# if _LIBCPP_HAS_WIDE_CHARACTERS
3875-
_LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3876-
_LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3877-
_LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3878-
_LIBCPP_EXPORTED_FROM_ABI long long stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3879-
_LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3880-
3881-
_LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
3882-
_LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
3883-
_LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
3884-
3885-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
3886-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
3887-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
3888-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
3889-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
3890-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
3891-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
3892-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
3893-
_LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
3878+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3879+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3880+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3881+
stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3882+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3883+
stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3884+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3885+
stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3886+
3887+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
3888+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
3889+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
3890+
3891+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
3892+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
3893+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
3894+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
3895+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
3896+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
3897+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
3898+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
3899+
[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
38943900
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
38953901

38963902
template <class _CharT, class _Traits, class _Allocator>
@@ -3899,7 +3905,7 @@ _LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocato
38993905

39003906
template <class _CharT, class _Allocator>
39013907
struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {
3902-
_LIBCPP_HIDE_FROM_ABI size_t
3908+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t
39033909
operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {
39043910
return std::__do_string_hash(__val.data(), __val.data() + __val.size());
39053911
}
@@ -3970,30 +3976,31 @@ erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
39703976
// Literal suffixes for basic_string [basic.string.literals]
39713977
inline namespace literals {
39723978
inline namespace string_literals {
3973-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
3979+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
39743980
operator""s(const char* __str, size_t __len) {
39753981
return basic_string<char>(__str, __len);
39763982
}
39773983

39783984
# if _LIBCPP_HAS_WIDE_CHARACTERS
3979-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t>
3980-
operator""s(const wchar_t* __str, size_t __len) {
3985+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3986+
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t> operator""s(const wchar_t* __str, size_t __len) {
39813987
return basic_string<wchar_t>(__str, __len);
39823988
}
39833989
# endif
39843990

39853991
# if _LIBCPP_HAS_CHAR8_T
3986-
inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
3992+
[[__nodiscard__]] inline
3993+
_LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
39873994
return basic_string<char8_t>(__str, __len);
39883995
}
39893996
# endif
39903997

3991-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t>
3992-
operator""s(const char16_t* __str, size_t __len) {
3998+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3999+
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t> operator""s(const char16_t* __str, size_t __len) {
39934000
return basic_string<char16_t>(__str, __len);
39944001
}
39954002

3996-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
4003+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
39974004
operator""s(const char32_t* __str, size_t __len) {
39984005
return basic_string<char32_t>(__str, __len);
39994006
}

libcxx/test/libcxx/diagnostics/string.nodiscard.verify.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,55 @@ void test() {
130130
str.subview(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
131131
#endif
132132
}
133+
134+
void test_nonmembers() {
135+
// Numeric conversions
136+
137+
std::string str;
138+
139+
std::stoi(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
140+
std::stol(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
141+
std::stoll(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
142+
std::stoull(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
143+
144+
std::stof(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
145+
std::stod(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
146+
std::stold(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
147+
148+
std::to_string(94); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
149+
std::to_string(82U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
150+
std::to_string(94L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
151+
std::to_string(82UL); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
152+
std::to_string(94LL); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
153+
std::to_string(82ULL); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
154+
std::to_string(94.0F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
155+
std::to_string(82.0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
156+
std::to_string(94.0L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
157+
158+
// std::hash<>
159+
160+
std::hash<std::string> hash;
161+
162+
hash(str); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
163+
164+
#if TEST_STD_VER >= 14
165+
// string literals
166+
167+
using namespace std::string_literals;
168+
169+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
170+
""s; // const char*
171+
# if !defined(TEST_HAS_NO_WIDE_CHARACTERS)
172+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
173+
L""s; // const wchar_t*
174+
# endif
175+
# if !defined(TEST_HAS_NO_CHAR8_T)
176+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
177+
u8""s; // const char8_t*
178+
# endif
179+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
180+
u""s; // const char16_t*
181+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
182+
U""s; // const char32_t*
183+
#endif
184+
}

0 commit comments

Comments
 (0)