Skip to content

Commit de0a404

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 de0a404

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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++20
10+
11+
// <numeric>
12+
13+
// Check that functions are marked [[nodiscard]]
14+
15+
#include <bit>
16+
#include <numeric>
17+
18+
#include "test_macros.h"
19+
20+
void test() {
21+
// [bit.rotate]
22+
std::rotl(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
23+
std::rotr(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
25+
// clang-format off
26+
#if TEST_STD_VER >= 26
27+
// [numeric.sat]
28+
std::add_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
std::sub_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
std::mul_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
std::div_sat(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
std::saturate_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
#endif // TEST_STD_VER >= 26
34+
// clang-format on
35+
}

libcxx/test/std/numerics/bit/bitops.rot/nodiscard.verify.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)