Skip to content

Commit 3ac2b36

Browse files
committed
🚨 Fix remaining clang-tidy warnings
1 parent 2aa3ae3 commit 3ac2b36

File tree

11 files changed

+44
-52
lines changed

11 files changed

+44
-52
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ target_link_libraries(safe_arithmetic INTERFACE boost_mp11)
2222

2323
if(PROJECT_IS_TOP_LEVEL)
2424
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
25-
# clang_tidy_interface(safe_arithmetic)
25+
clang_tidy_interface(safe_arithmetic)
2626
add_subdirectory(test)
2727
endif()

include/safe/big_integer/detail/algorithms.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
#include <safe/big_integer/detail/storage.hpp>
44

5-
#include <algorithm>
6-
#include <cstdint>
5+
#include <cstddef>
76

87
namespace safe::_big_integer::detail {
98
[[nodiscard]] constexpr auto reverse_zip_transform(auto op) {
109
return [=](auto &result, auto const &lhs, auto const &rhs) -> void {
11-
auto i = result.num_elems;
12-
do {
13-
i--;
10+
for (auto i = result.num_elems; i > std::size_t{};) {
11+
--i;
1412
result.set(i, op(lhs.get(i), rhs.get(i)));
15-
} while (i > std::size_t{});
13+
}
1614
};
1715
}
1816

@@ -41,4 +39,4 @@ namespace safe::_big_integer::detail {
4139
}
4240
};
4341
}
44-
} // namespace safe::_big_integer::detail
42+
} // namespace safe::_big_integer::detail

include/safe/big_integer/detail/compare.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ template <std::size_t LhsNumBits, std::size_t RhsNumBits>
1111
[[nodiscard]] constexpr auto unsigned_compare(storage<LhsNumBits> const &lhs,
1212
storage<RhsNumBits> const &rhs)
1313
-> std::strong_ordering {
14-
auto i = std::max(lhs.num_elems, rhs.num_elems);
15-
do {
16-
i--;
17-
18-
if (lhs.get(i) < rhs.get(i)) {
14+
for (auto i = std::max(lhs.num_elems, rhs.num_elems); i > std::size_t{};) {
15+
--i;
16+
auto const l = lhs.get(i);
17+
auto const r = rhs.get(i);
18+
if (l < r) {
1919
return std::strong_ordering::less;
2020
}
21-
if (lhs.get(i) > rhs.get(i)) {
21+
if (l > r) {
2222
return std::strong_ordering::greater;
2323
}
24-
} while (i > 0);
25-
24+
}
2625
return std::strong_ordering::equal;
2726
}
2827

include/safe/big_integer/detail/divides.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ template <std::size_t LhsNumBits, std::size_t RhsNumBits>
1515
storage<RhsNumBits> b) {
1616
using ret_t = storage<std::max(LhsNumBits, RhsNumBits) + 1>;
1717

18-
ret_t ret = b;
18+
auto ret = ret_t{b};
1919

2020
auto const not_big_enough = [&]() -> bool {
2121
auto a_prime = a;
@@ -93,4 +93,4 @@ constexpr static auto divmod = [](auto &quotient, auto &remainder,
9393
}
9494
}
9595
};
96-
} // namespace safe::_big_integer::detail
96+
} // namespace safe::_big_integer::detail

include/safe/big_integer/detail/storage.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ template <std::size_t NumBits> struct storage {
1919
std::array<elem_t, num_elems> elems{};
2020

2121
constexpr storage() = default;
22-
constexpr storage(std::array<elem_t, num_elems> const &new_elems)
22+
constexpr explicit storage(std::array<elem_t, num_elems> const &new_elems)
2323
: elems{new_elems} {}
2424

2525
template <std::size_t RhsNumBits>
26-
constexpr storage(storage<RhsNumBits> const &rhs) {
26+
constexpr explicit storage(storage<RhsNumBits> const &rhs) {
2727
for (auto i = std::size_t{}; i < NumBits; i++) {
2828
set(i, rhs.get(i));
2929
}
@@ -76,25 +76,28 @@ template <std::size_t NumBits> struct storage {
7676
template <typename T>
7777
requires(sizeof(T) <= 4 && std::signed_integral<std::remove_cvref_t<T>>)
7878
[[nodiscard]] constexpr auto to_storage(T v) -> storage<sizeof(T) * 8> {
79-
return {{static_cast<uint32_t>(static_cast<int32_t>(v))}};
79+
return storage<sizeof(T) * 8>{
80+
{static_cast<uint32_t>(static_cast<int32_t>(v))}};
8081
}
8182

8283
template <typename T>
8384
requires(sizeof(T) <= 4 && std::unsigned_integral<std::remove_cvref_t<T>>)
8485
[[nodiscard]] constexpr auto to_storage(T v) -> storage<(sizeof(T) * 8) + 1> {
85-
return {{static_cast<uint32_t>(v)}};
86+
return storage<(sizeof(T) * 8) + 1>{{static_cast<uint32_t>(v)}};
8687
}
8788

8889
template <typename T>
8990
requires(sizeof(T) == 8 && std::signed_integral<std::remove_cvref_t<T>>)
9091
[[nodiscard]] constexpr auto to_storage(T v) -> storage<64> {
91-
return {{static_cast<uint32_t>(v), static_cast<uint32_t>(v >> 32)}};
92+
return storage<64>{
93+
{static_cast<uint32_t>(v), static_cast<uint32_t>(v >> 32)}};
9294
}
9395

9496
template <typename T>
9597
requires(sizeof(T) == 8 && std::unsigned_integral<std::remove_cvref_t<T>>)
9698
[[nodiscard]] constexpr auto to_storage(T v) -> storage<65> {
97-
return {{static_cast<uint32_t>(v), static_cast<uint32_t>(v >> 32)}};
99+
return storage<65>{
100+
{static_cast<uint32_t>(v), static_cast<uint32_t>(v >> 32)}};
98101
}
99102

100103
template <std::size_t NumBits>
@@ -134,8 +137,8 @@ template <std::size_t NumBits>
134137
requires(NumBits > 32 && NumBits <= 64)
135138
[[nodiscard]] constexpr auto to_integral(storage<NumBits> const &value)
136139
-> int64_t {
137-
return (static_cast<int64_t>(value.get(1)) << 32) |
138-
(static_cast<int64_t>(value.get(0)));
140+
return (static_cast<uint64_t>(value.get(1)) << 32u) |
141+
(static_cast<uint64_t>(value.get(0)));
139142
}
140143

141144
template <std::size_t NumBits>

include/safe/big_integer/interface/big_integer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace safe::_big_integer::interface {
1212
template <std::size_t NumBits> struct big_integer {
1313
detail::storage<NumBits> unsafe_storage{};
1414

15-
constexpr big_integer(auto value)
15+
// NOLINTNEXTLINE(google-explicit-constructor)
16+
constexpr explicit(false) big_integer(auto value)
1617
: unsafe_storage{detail::to_storage(value)} {}
1718
constexpr big_integer() = default;
1819

include/safe/dsl/detail/triint.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ template <typename LhsT, typename RhsT>
9292
constexpr auto num_bits =
9393
std::numeric_limits<int_t>::digits + (std::is_signed_v<int_t> ? 1 : 0);
9494

95-
for (int i = 0; i < num_bits; i++) {
96-
auto const digit = triint<int_t>{0, static_cast<int_t>(1 << i)};
95+
for (auto i = std::size_t{}; i < num_bits; ++i) {
96+
auto const digit = triint<int_t>{0, static_cast<int_t>(1u << i)};
9797
auto const a = lhs & digit;
9898
auto const b = rhs & digit;
9999
auto const sum = full_adder(a, b, carry, carry);

include/safe/dsl/is_subset.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct is_subset<ival_t<lhs_min, lhs_max>, ival_t<rhs_min, rhs_max>> {
1313

1414
constexpr static bool value = lhs_min >= rhs_min && lhs_max <= rhs_max;
1515

16-
[[nodiscard]] constexpr operator bool() const { return value; }
16+
[[nodiscard]] constexpr explicit operator bool() const { return value; }
1717
};
1818

1919
template <auto lhs_variable_bits, auto lhs_constant_bits,
@@ -32,7 +32,7 @@ struct is_subset<mask_t<lhs_variable_bits, lhs_constant_bits>,
3232
constexpr static bool value = lhs_has_no_var_bits_outside_rhs_var_bits &&
3333
lhs_const_bits_match_rhs_const_bits;
3434

35-
[[nodiscard]] constexpr operator bool() const { return value; }
35+
[[nodiscard]] constexpr explicit operator bool() const { return value; }
3636
};
3737

3838
template <auto lhs_min, auto lhs_max, auto rhs_variable_bits,
@@ -48,7 +48,7 @@ struct is_subset<ival_t<lhs_min, lhs_max>,
4848
lhs_min >= rhs_ival::min &&
4949
lhs_max <= rhs_ival::max;
5050

51-
[[nodiscard]] constexpr operator bool() const { return value; }
51+
[[nodiscard]] constexpr explicit operator bool() const { return value; }
5252
};
5353

5454
template <auto lhs_variable_bits, auto lhs_constant_bits, auto rhs_min,
@@ -63,11 +63,11 @@ struct is_subset<mask_t<lhs_variable_bits, lhs_constant_bits>,
6363
constexpr static bool value =
6464
lhs_ival::min >= rhs_min && lhs_ival::max <= rhs_max;
6565

66-
[[nodiscard]] constexpr operator bool() const { return value; }
66+
[[nodiscard]] constexpr explicit operator bool() const { return value; }
6767
};
6868

6969
template <Operand LhsT, Operand RhsT>
7070
[[nodiscard]] constexpr auto operator<=(LhsT, RhsT) -> bool {
71-
return detail::eval_v<is_subset<LhsT, RhsT>>;
71+
return static_cast<bool>(detail::eval_v<is_subset<LhsT, RhsT>>);
7272
}
7373
} // namespace safe::dsl

include/safe/dsl/is_superset.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ template <typename T, typename U> using is_superset = is_subset<U, T>;
99

1010
template <Operand LhsT, Operand RhsT>
1111
[[nodiscard]] constexpr auto operator>=(LhsT, RhsT) -> bool {
12-
return detail::eval_v<is_superset<LhsT, RhsT>>;
12+
return static_cast<bool>(detail::eval_v<is_superset<LhsT, RhsT>>);
1313
}
1414
} // namespace safe::dsl

include/safe/dsl/mask.hpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include <safe/dsl/fwd.hpp>
77
#include <safe/dsl/ival.hpp>
88

9-
#include <bit>
10-
#include <type_traits>
11-
129
namespace safe::dsl {
1310
template <auto VariableBits, auto ConstantBits = 0>
1411
struct mask_t : public detail::primitive {
@@ -32,14 +29,11 @@ namespace detail {
3229
* Takes a bitmask and sets all digits to the right of the first
3330
* '1' to '1'.
3431
*/
35-
[[nodiscard]] constexpr auto fill_in_bitmask(auto value) {
36-
auto prev_value = value;
37-
38-
do {
32+
template <typename T> [[nodiscard]] constexpr auto fill_in_bitmask(T value) {
33+
for (T prev_value{}; prev_value != value;) {
3934
prev_value = value;
40-
value = value | (value >> 1);
41-
} while (prev_value != value);
42-
35+
value |= value >> 1;
36+
}
4337
return value;
4438
}
4539

0 commit comments

Comments
 (0)