Skip to content

Commit 8b9e307

Browse files
committed
[libc++][numeric] Marked saturation artithmetic functions as [[nodiscard]]
...according to Coding Guidelines: - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant - #166524 (comment)
1 parent 77b9301 commit 8b9e307

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

libcxx/include/__numeric/saturation_arithmetic.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,27 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Rp __saturate_cast(_Tp __x) noexcept {
121121
#if _LIBCPP_STD_VER >= 26
122122

123123
template <__signed_or_unsigned_integer _Tp>
124-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
124+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
125125
return std::__add_sat(__x, __y);
126126
}
127127

128128
template <__signed_or_unsigned_integer _Tp>
129-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
129+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
130130
return std::__sub_sat(__x, __y);
131131
}
132132

133133
template <__signed_or_unsigned_integer _Tp>
134-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
134+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
135135
return std::__mul_sat(__x, __y);
136136
}
137137

138138
template <__signed_or_unsigned_integer _Tp>
139-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
139+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
140140
return std::__div_sat(__x, __y);
141141
}
142142

143143
template <__signed_or_unsigned_integer _Rp, __signed_or_unsigned_integer _Tp>
144-
_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
144+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
145145
return std::__saturate_cast<_Rp>(__x);
146146
}
147147

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++26
10+
11+
// <numeric>
12+
13+
#include <numeric>
14+
15+
// clang-format off
16+
void test() {
17+
std::add_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
18+
std::sub_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
19+
std::mul_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
std::div_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
std::saturate_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
}
23+
// clang-format on

0 commit comments

Comments
 (0)