[libcxx-commits] [libcxx] d52d761 - [libc++][complex] Applied `[[nodiscard]]` (#171027)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Dec 13 01:20:01 PST 2025
Author: Hristo Hristov
Date: 2025-12-13T11:19:57+02:00
New Revision: d52d761c23cb6ef7b6f128f735183228614ceb6f
URL: https://github.com/llvm/llvm-project/commit/d52d761c23cb6ef7b6f128f735183228614ceb6f
DIFF: https://github.com/llvm/llvm-project/commit/d52d761c23cb6ef7b6f128f735183228614ceb6f.diff
LOG: [libc++][complex] Applied `[[nodiscard]]` (#171027)
`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
There appears to be an issue with annotating `operator*` and
`operator/`, see: https://llvm.org/PR171031
---------
Co-authored-by: A. Jiang <de34 at live.cn>
Added:
libcxx/test/libcxx/numerics/complex.number/nodiscard.verify.cpp
Modified:
libcxx/include/complex
libcxx/test/std/numerics/complex.number/complex.tuple/get.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/complex b/libcxx/include/complex
index d8ec3d95c10ed..49ab388113542 100644
--- a/libcxx/include/complex
+++ b/libcxx/include/complex
@@ -319,8 +319,8 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c)
: __re_(__c.real()), __im_(__c.imag()) {}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
@@ -432,8 +432,8 @@ public:
_LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
_LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
@@ -529,8 +529,8 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
_LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
@@ -630,8 +630,8 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; }
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; }
@@ -732,7 +732,7 @@ inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __
// 26.3.6 operators:
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(__x);
__t += __y;
@@ -740,7 +740,7 @@ operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator+(const complex<_Tp>& __x, const _Tp& __y) {
complex<_Tp> __t(__x);
__t += __y;
@@ -748,7 +748,7 @@ operator+(const complex<_Tp>& __x, const _Tp& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator+(const _Tp& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(__y);
__t += __x;
@@ -756,7 +756,7 @@ operator+(const _Tp& __x, const complex<_Tp>& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(__x);
__t -= __y;
@@ -764,7 +764,7 @@ operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator-(const complex<_Tp>& __x, const _Tp& __y) {
complex<_Tp> __t(__x);
__t -= __y;
@@ -772,7 +772,7 @@ operator-(const complex<_Tp>& __x, const _Tp& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator-(const _Tp& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(-__y);
__t += __x;
@@ -780,13 +780,13 @@ operator-(const _Tp& __x, const complex<_Tp>& __y) {
}
template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());
}
template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {
_Tp __a = __z.real();
_Tp __b = __z.imag();
@@ -797,7 +797,7 @@ operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator*(const complex<_Tp>& __x, const _Tp& __y) {
complex<_Tp> __t(__x);
__t *= __y;
@@ -805,7 +805,7 @@ operator*(const complex<_Tp>& __x, const _Tp& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator*(const _Tp& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(__y);
__t *= __x;
@@ -813,13 +813,13 @@ operator*(const _Tp& __x, const complex<_Tp>& __y) {
}
template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {
return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());
}
template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {
_Tp __a = __z.real();
_Tp __b = __z.imag();
@@ -831,13 +831,13 @@ operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator/(const complex<_Tp>& __x, const _Tp& __y) {
return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
operator/(const _Tp& __x, const complex<_Tp>& __y) {
complex<_Tp> __t(__x);
__t /= __y;
@@ -845,12 +845,14 @@ operator/(const _Tp& __x, const complex<_Tp>& __y) {
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+operator+(const complex<_Tp>& __x) {
return __x;
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+operator-(const complex<_Tp>& __x) {
return complex<_Tp>(-__x.real(), -__x.imag());
}
@@ -912,12 +914,13 @@ struct __libcpp_complex_overload_traits<_Tp, false, true> {
// real
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) {
return __c.real();
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
+typename __libcpp_complex_overload_traits<_Tp>::_ValueType
real(_Tp __re) {
return __re;
}
@@ -925,12 +928,13 @@ real(_Tp __re) {
// imag
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) {
return __c.imag();
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
+typename __libcpp_complex_overload_traits<_Tp>::_ValueType
imag(_Tp) {
return 0;
}
@@ -938,36 +942,36 @@ imag(_Tp) {
// abs
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {
return std::hypot(__c.real(), __c.imag());
}
// arg
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {
return std::atan2(__c.imag(), __c.real());
}
template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {
return std::atan2l(0.L, __re);
}
template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {
return std::atan2(0., __re);
}
template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {
return std::atan2f(0.F, __re);
}
// norm
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) {
if (std::__constexpr_isinf(__c.real()))
return std::abs(__c.real());
if (std::__constexpr_isinf(__c.imag()))
@@ -976,7 +980,8 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const comple
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+typename __libcpp_complex_overload_traits<_Tp>::_ValueType
norm(_Tp __re) {
typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
return static_cast<_ValueType>(__re) * __re;
@@ -985,12 +990,14 @@ norm(_Tp __re) {
// conj
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
+conj(const complex<_Tp>& __c) {
return complex<_Tp>(__c.real(), -__c.imag());
}
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
conj(_Tp __re) {
typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
return _ComplexType(__re);
@@ -999,7 +1006,7 @@ conj(_Tp __re) {
// proj
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
complex<_Tp> __r = __c;
if (std::isinf(__c.real()) || std::isinf(__c.imag()))
__r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
@@ -1007,14 +1014,16 @@ inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {
}
template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
+proj(_Tp __re) {
if (std::isinf(__re))
__re = std::abs(__re);
return complex<_Tp>(__re);
}
template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
+proj(_Tp __re) {
typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
return _ComplexType(__re);
}
@@ -1022,7 +1031,7 @@ inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_Co
// polar
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {
if (std::isnan(__rho) || std::signbit(__rho))
return complex<_Tp>(_Tp(NAN), _Tp(NAN));
if (std::isnan(__theta)) {
@@ -1047,21 +1056,21 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta =
// log
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {
return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
}
// log10
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
return std::log(__x) / std::log(_Tp(10));
}
// sqrt
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
if (std::isinf(__x.imag()))
return complex<_Tp>(_Tp(INFINITY), __x.imag());
if (std::isinf(__x.real())) {
@@ -1075,7 +1084,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
// exp
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
_Tp __i = __x.imag();
if (__i == 0) {
return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
@@ -1097,24 +1106,27 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {
// pow
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {
return std::exp(__y * std::log(__x));
}
template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> > pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> >
+pow(const complex<_Tp>& __x, const complex<_Up>& __y) {
typedef complex<__promote_t<_Tp, _Up> > result_type;
return std::pow(result_type(__x), result_type(__y));
}
template <class _Tp, class _Up, __enable_if_t<is_floating_point<_Tp>::value && is_arithmetic<_Up>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> > pow(const complex<_Tp>& __x, const _Up& __y) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> >
+pow(const complex<_Tp>& __x, const _Up& __y) {
typedef complex<__promote_t<_Tp, _Up> > result_type;
return std::pow(result_type(__x), result_type(__y));
}
template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value && is_floating_point<_Up>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> > pow(const _Tp& __x, const complex<_Up>& __y) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<__promote_t<_Tp, _Up> >
+pow(const _Tp& __x, const complex<_Up>& __y) {
typedef complex<__promote_t<_Tp, _Up> > result_type;
return std::pow(result_type(__x), result_type(__y));
}
@@ -1129,7 +1141,7 @@ inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) {
// asinh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
@@ -1154,7 +1166,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {
// acosh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
@@ -1183,7 +1195,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {
// atanh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.imag())) {
return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));
@@ -1209,7 +1221,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {
// sinh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
return complex<_Tp>(__x.real(), _Tp(NAN));
if (__x.real() == 0 && !std::isfinite(__x.imag()))
@@ -1222,7 +1234,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {
// cosh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
if (__x.real() == 0 && !std::isfinite(__x.imag()))
@@ -1237,7 +1249,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {
// tanh
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
if (std::isinf(__x.real())) {
if (!std::isfinite(__x.imag()))
return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
@@ -1257,7 +1269,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {
// asin
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
return complex<_Tp>(__z.imag(), -__z.real());
}
@@ -1265,7 +1277,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {
// acos
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
@@ -1297,7 +1309,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {
// atan
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
return complex<_Tp>(__z.imag(), -__z.real());
}
@@ -1305,7 +1317,7 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {
// sin
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
return complex<_Tp>(__z.imag(), -__z.real());
}
@@ -1313,14 +1325,14 @@ _LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {
// cos
template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {
return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
}
// tan
template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {
complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
return complex<_Tp>(__z.imag(), -__z.real());
}
@@ -1398,7 +1410,7 @@ struct tuple_element<_Ip, complex<_Tp>> {
};
template <size_t _Ip, class _Xp>
-_LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
static_assert(_Ip < 2, "Index value is out of range.");
if constexpr (_Ip == 0) {
return __z.__re_;
@@ -1408,7 +1420,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept {
}
template <size_t _Ip, class _Xp>
-_LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
static_assert(_Ip < 2, "Index value is out of range.");
if constexpr (_Ip == 0) {
return std::move(__z.__re_);
@@ -1418,7 +1430,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept {
}
template <size_t _Ip, class _Xp>
-_LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept {
static_assert(_Ip < 2, "Index value is out of range.");
if constexpr (_Ip == 0) {
return __z.__re_;
@@ -1428,7 +1440,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept
}
template <size_t _Ip, class _Xp>
-_LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept {
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept {
static_assert(_Ip < 2, "Index value is out of range.");
if constexpr (_Ip == 0) {
return std::move(__z.__re_);
diff --git a/libcxx/test/libcxx/numerics/complex.number/nodiscard.verify.cpp b/libcxx/test/libcxx/numerics/complex.number/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..8133015806f36
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/complex.number/nodiscard.verify.cpp
@@ -0,0 +1,127 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <complex>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <complex>
+#include <utility>
+
+#include "test_macros.h"
+
+template <typename T>
+void test() {
+ const std::complex<T> c;
+ const std::complex<T> d;
+
+ c.real(); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ c.imag(); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ c + d; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ c + T(1); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ T(1) + c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ c - d; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ c - T(1); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ T(1) - c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if 0 // TODO: Enable when https://llvm.org/PR171031 is resolved.
+ c* d; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+ c* T(1); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ T(1) * c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if 0 // TODO: Enable when https://llvm.org/PR171031 is resolved.
+ c / d; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
+ c / T(1); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ T(1) / c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ +c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ -c; // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::real(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::imag(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::real(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::imag(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::abs(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::arg(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::arg(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::norm(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::norm(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::conj(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::conj(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::proj(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::proj(T(1)); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // expected-warning at +1 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::polar(T(93), T(0));
+
+ std::log(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::log10(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::sqrt(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::exp(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::complex<double> ic;
+
+ std::pow(c, c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::pow(c, ic); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::pow(ic, c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::asinh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::acosh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::atanh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::sinh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::cosh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::tanh(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::asin(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::acos(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::atan(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::sin(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::cos(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::tan(c); // expected-warning 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if TEST_STD_VER >= 26
+ const std::complex<T> cc;
+
+ // expected-warning at +1 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(c);
+ // expected-warning at +1 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(std::move(c));
+ // expected-warning at +1 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(c);
+ // expected-warning at +1 3 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::get<0>(std::move(cc));
+#endif
+}
+
+void test() {
+ test<float>();
+ test<double>();
+ test<long double>();
+}
diff --git a/libcxx/test/std/numerics/complex.number/complex.tuple/get.verify.cpp b/libcxx/test/std/numerics/complex.number/complex.tuple/get.verify.cpp
index 6a6e3a968d50f..1913adbc29d58 100644
--- a/libcxx/test/std/numerics/complex.number/complex.tuple/get.verify.cpp
+++ b/libcxx/test/std/numerics/complex.number/complex.tuple/get.verify.cpp
@@ -31,25 +31,25 @@ void test() {
{
C c{T{27}, T{28}};
// expected-error-re@*:* 3{{static assertion failed {{.*}}Index value is out of range.}}
- std::get<3>(c);
+ (void)std::get<3>(c);
}
// &&
{
C c{T{27}, T{28}};
// expected-error-re@*:* 3 {{static assertion failed {{.*}}Index value is out of range.}}
- std::get<3>(std::move(c));
+ (void)std::get<3>(std::move(c));
}
// const &
{
const C c{T{27}, T{28}};
// expected-error-re@*:* 3 {{static assertion failed {{.*}}Index value is out of range.}}
- std::get<3>(c);
+ (void)std::get<3>(c);
}
// const &&
{
const C c{T{27}, T{28}};
// expected-error-re@*:* 3 {{static assertion failed {{.*}}Index value is out of range.}}
- std::get<3>(std::move(c));
+ (void)std::get<3>(std::move(c));
}
}
More information about the libcxx-commits
mailing list