Skip to content

Commit 0c6802b

Browse files
committed
fix clang-tidy warning
disable clang-tidy for backported std:expected since we don't want to diverge from upstream. Signed-off-by: Junwang Zhao <[email protected]>
1 parent 4025343 commit 0c6802b

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

src/iceberg/expected.h

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "iceberg/iceberg_export.h"
2828

29+
// NOLINTBEGIN
30+
2931
namespace iceberg {
3032

3133
namespace expected_detail {
@@ -196,7 +198,7 @@ class bad_expected_access;
196198
template <>
197199
class ICEBERG_EXPORT bad_expected_access<void> : public std::exception {
198200
public:
199-
virtual const char* what() const noexcept override { return "Bad expected access"; }
201+
const char* what() const noexcept override { return "Bad expected access"; }
200202

201203
protected:
202204
bad_expected_access() = default;
@@ -1020,9 +1022,9 @@ class ICEBERG_EXPORT [[nodiscard]] expected
10201022
using ctor_base = expected_detail::default_ctor_base<T, E>;
10211023

10221024
public:
1023-
typedef T value_type;
1024-
typedef E error_type;
1025-
typedef unexpected<E> unexpected_type;
1025+
using value_type = T;
1026+
using error_type = E;
1027+
using unexpected_type = unexpected<E>;
10261028

10271029
// default constructors
10281030

@@ -1488,10 +1490,12 @@ class ICEBERG_EXPORT [[nodiscard]] expected
14881490
static_assert(std::is_same_v<typename U::error_type, E>,
14891491
"The error type must be the same after calling the F");
14901492

1491-
if (has_value())
1493+
if (has_value()) {
14921494
return std::invoke(std::forward<F>(f), this->m_val);
1493-
else
1495+
1496+
} else {
14941497
return U(unexpect, error());
1498+
}
14951499
}
14961500
template <class F, class GE = E,
14971501
std::enable_if_t<std::is_constructible_v<GE, const GE&>>* = nullptr>
@@ -1502,10 +1506,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15021506
static_assert(std::is_same_v<typename U::error_type, E>,
15031507
"The error type must be the same after calling the F");
15041508

1505-
if (has_value())
1509+
if (has_value()) {
15061510
return std::invoke(std::forward<F>(f), this->m_val);
1507-
else
1511+
} else {
15081512
return U(unexpect, error());
1513+
}
15091514
}
15101515

15111516
template <class F, class GE = E,
@@ -1518,10 +1523,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15181523
static_assert(std::is_same_v<typename U::error_type, E>,
15191524
"The error type must be the same after calling the F");
15201525

1521-
if (has_value())
1526+
if (has_value()) {
15221527
return std::invoke(std::forward<F>(f), std::move(this->m_val));
1523-
else
1528+
} else {
15241529
return U(unexpect, std::move(error()));
1530+
}
15251531
}
15261532
template <class F, class GE = E,
15271533
std::enable_if_t<std::is_constructible_v<GE, const GE>>* = nullptr>
@@ -1533,10 +1539,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15331539
static_assert(std::is_same_v<typename U::error_type, E>,
15341540
"The error type must be the same after calling the F");
15351541

1536-
if (has_value())
1542+
if (has_value()) {
15371543
return std::invoke(std::forward<F>(f), std::move(this->m_val));
1538-
else
1544+
} else {
15391545
return U(unexpect, std::move(error()));
1546+
}
15401547
}
15411548

15421549
template <class F, class UT = T,
@@ -1548,10 +1555,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15481555
static_assert(std::is_same_v<typename G::value_type, T>,
15491556
"The value type must be the same after calling the F");
15501557

1551-
if (has_value())
1558+
if (has_value()) {
15521559
return G(std::in_place, this->m_val);
1553-
else
1560+
} else {
15541561
return std::invoke(std::forward<F>(f), error());
1562+
}
15551563
}
15561564
template <class F, class UT = T,
15571565
std::enable_if_t<std::is_constructible_v<UT, const UT&>>* = nullptr>
@@ -1562,10 +1570,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15621570
static_assert(std::is_same_v<typename G::value_type, T>,
15631571
"The value type must be the same after calling the F");
15641572

1565-
if (has_value())
1573+
if (has_value()) {
15661574
return G(std::in_place, this->m_val);
1567-
else
1575+
} else {
15681576
return std::invoke(std::forward<F>(f), error());
1577+
}
15691578
}
15701579
template <class F, class UT = T,
15711580
std::enable_if_t<std::is_constructible_v<UT, UT&&>>* = nullptr>
@@ -1576,10 +1585,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15761585
static_assert(std::is_same_v<typename G::value_type, T>,
15771586
"The value type must be the same after calling the F");
15781587

1579-
if (has_value())
1588+
if (has_value()) {
15801589
return G(std::in_place, std::move(this->m_val));
1581-
else
1590+
} else {
15821591
return std::invoke(std::forward<F>(f), std::move(error()));
1592+
}
15831593
}
15841594
template <class F, class UT = T,
15851595
std::enable_if_t<std::is_constructible_v<UT, const UT>>* = nullptr>
@@ -1590,10 +1600,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected
15901600
static_assert(std::is_same_v<typename G::value_type, T>,
15911601
"The value type must be the same after calling the F");
15921602

1593-
if (has_value())
1603+
if (has_value()) {
15941604
return G(std::in_place, std::move(this->m_val));
1595-
else
1605+
} else {
15961606
return std::invoke(std::forward<F>(f), std::move(error()));
1607+
}
15971608
}
15981609

15991610
template <class F, class GE = E,
@@ -1801,9 +1812,9 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
18011812
using ctor_base = expected_detail::default_ctor_base<T, E>;
18021813

18031814
public:
1804-
typedef T value_type;
1805-
typedef E error_type;
1806-
typedef unexpected<E> unexpected_type;
1815+
using value_type = T;
1816+
using error_type = E;
1817+
using unexpected_type = unexpected<E>;
18071818

18081819
constexpr expected() = default;
18091820
constexpr expected(const expected& rhs) = default;
@@ -2068,10 +2079,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
20682079
static_assert(std::is_same_v<typename U::error_type, E>,
20692080
"The error type must be the same after calling the F");
20702081

2071-
if (has_value())
2082+
if (has_value()) {
20722083
return std::invoke(std::forward<F>(f));
2073-
else
2084+
} else {
20742085
return U(unexpect, error());
2086+
}
20752087
}
20762088
template <class F, class GE = E,
20772089
std::enable_if_t<std::is_constructible_v<GE, const GE&>>* = nullptr>
@@ -2082,10 +2094,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
20822094
static_assert(std::is_same_v<typename U::error_type, E>,
20832095
"The error type must be the same after calling the F");
20842096

2085-
if (has_value())
2097+
if (has_value()) {
20862098
return std::invoke(std::forward<F>(f));
2087-
else
2099+
} else {
20882100
return U(unexpect, error());
2101+
}
20892102
}
20902103
template <class F, class GE = E,
20912104
std::enable_if_t<std::is_constructible_v<GE, GE&&>>* = nullptr>
@@ -2096,10 +2109,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
20962109
static_assert(std::is_same_v<typename U::error_type, E>,
20972110
"The error type must be the same after calling the F");
20982111

2099-
if (has_value())
2112+
if (has_value()) {
21002113
return std::invoke(std::forward<F>(f));
2101-
else
2114+
} else {
21022115
return U(unexpect, std::move(error()));
2116+
}
21032117
}
21042118
template <class F, class GE = E,
21052119
std::enable_if_t<std::is_constructible_v<GE, const GE>>* = nullptr>
@@ -2110,10 +2124,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
21102124
static_assert(std::is_same_v<typename U::error_type, E>,
21112125
"The error type must be the same after calling the F");
21122126

2113-
if (has_value())
2127+
if (has_value()) {
21142128
return std::invoke(std::forward<F>(f));
2115-
else
2129+
} else {
21162130
return U(unexpect, std::move(error()));
2131+
}
21172132
}
21182133

21192134
template <class F>
@@ -2124,10 +2139,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
21242139
static_assert(std::is_same_v<typename G::value_type, T>,
21252140
"The value type must be the same after calling the F");
21262141

2127-
if (has_value())
2142+
if (has_value()) {
21282143
return G();
2129-
else
2144+
} else {
21302145
return std::invoke(std::forward<F>(f), error());
2146+
}
21312147
}
21322148
template <class F>
21332149
constexpr auto or_else(F&& f) const& {
@@ -2137,10 +2153,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
21372153
static_assert(std::is_same_v<typename G::value_type, T>,
21382154
"The value type must be the same after calling the F");
21392155

2140-
if (has_value())
2156+
if (has_value()) {
21412157
return G();
2142-
else
2158+
} else {
21432159
return std::invoke(std::forward<F>(f), error());
2160+
}
21442161
}
21452162
template <class F>
21462163
constexpr auto or_else(F&& f) && {
@@ -2150,10 +2167,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
21502167
static_assert(std::is_same_v<typename G::value_type, T>,
21512168
"The value type must be the same after calling the F");
21522169

2153-
if (has_value())
2170+
if (has_value()) {
21542171
return G();
2155-
else
2172+
} else {
21562173
return std::invoke(std::forward<F>(f), std::move(error()));
2174+
}
21572175
}
21582176
template <class F>
21592177
constexpr auto or_else(F&& f) const&& {
@@ -2163,10 +2181,11 @@ class ICEBERG_EXPORT [[nodiscard]] expected<void, E>
21632181
static_assert(std::is_same_v<typename G::value_type, T>,
21642182
"The value type must be the same after calling the F");
21652183

2166-
if (has_value())
2184+
if (has_value()) {
21672185
return G();
2168-
else
2186+
} else {
21692187
return std::invoke(std::forward<F>(f), std::move(error()));
2188+
}
21702189
}
21712190

21722191
template <class F, class GE = E,
@@ -2331,3 +2350,5 @@ ICEBERG_EXPORT constexpr void swap(
23312350
}
23322351

23332352
} // namespace iceberg
2353+
2354+
// NOLINTEND

test/core/expected_test.cc

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ constexpr bool EqualityTester(T const& lhs, U const& rhs) {
247247
struct Type1 {
248248
std::string value;
249249

250-
Type1(std::string const& v) : value(v) {}
251-
Type1(const char* v) : value(v) {}
250+
explicit Type1(std::string v) : value(std::move(v)) {}
251+
explicit Type1(const char* v) : value(v) {}
252252
Type1(Type1 const&) = delete;
253253
Type1& operator=(Type1 const&) = delete;
254254
Type1(Type1&& other) = default;
@@ -261,8 +261,8 @@ struct Type1 {
261261
struct Type2 {
262262
std::string value;
263263

264-
Type2(std::string const& v) : value(v) {}
265-
Type2(const char* v) : value(v) {}
264+
explicit Type2(std::string v) : value(std::move(v)) {}
265+
explicit Type2(const char* v) : value(v) {}
266266
Type2(Type2 const&) = delete;
267267
Type2& operator=(Type2 const&) = delete;
268268
Type2(Type2&& other) = default;
@@ -289,9 +289,9 @@ TEST(ExpectedTest, EqualityTest) {
289289

290290
// compare with same type expected<T, E>
291291
{
292-
Expected const value1 = "value1";
293-
Expected const value2 = "value2";
294-
Expected const value1Copy = "value1";
292+
Expected const value1("value1");
293+
Expected const value2("value2");
294+
Expected const value1Copy("value1");
295295
Expected const error1 = iceberg::unexpected<E>("error1");
296296
Expected const error2 = iceberg::unexpected<E>("error2");
297297
Expected const error1Copy = iceberg::unexpected<E>("error1");
@@ -311,9 +311,9 @@ TEST(ExpectedTest, EqualityTest) {
311311
ASSERT_FALSE((std::is_same_v<E, E2>));
312312
using Expected2 = iceberg::expected<T2, E2>;
313313

314-
Expected const value1 = "value1";
315-
Expected2 const value2 = "value2";
316-
Expected2 const value1Same = "value1";
314+
Expected const value1("value1");
315+
Expected2 const value2("value2");
316+
Expected2 const value1Same("value1");
317317
Expected const error1 = iceberg::unexpected<E>("error1");
318318
Expected2 const error2 = iceberg::unexpected<E2>("error2");
319319
Expected2 const error1Same = iceberg::unexpected<E2>("error1");
@@ -327,10 +327,10 @@ TEST(ExpectedTest, EqualityTest) {
327327

328328
// compare with same value type T
329329
{
330-
Expected const value1 = "value1";
330+
Expected const value1("value1");
331331
Expected const error1 = iceberg::unexpected<E>("error1");
332-
T const value2 = "value2";
333-
T const value1Same = "value1";
332+
T const value2("value2");
333+
T const value1Same("value1");
334334

335335
EXPECT_TRUE(EqualityTester<true>(value1, value1Same));
336336
EXPECT_TRUE(EqualityTester<false>(value1, value2));
@@ -342,10 +342,10 @@ TEST(ExpectedTest, EqualityTest) {
342342
using T2 = Type2;
343343
ASSERT_FALSE((std::is_same_v<T, T2>));
344344

345-
Expected const value1 = "value1";
345+
Expected const value1("value1");
346346
Expected const error1 = iceberg::unexpected<E>("error1");
347-
T2 const value2 = "value2";
348-
T2 const value1Same = "value1";
347+
T2 const value2("value2");
348+
T2 const value1Same("value1");
349349

350350
EXPECT_TRUE(EqualityTester<true>(value1, value1Same));
351351
EXPECT_TRUE(EqualityTester<false>(value1, value2));
@@ -354,7 +354,7 @@ TEST(ExpectedTest, EqualityTest) {
354354

355355
// compare with same error type unexpected<E>
356356
{
357-
Expected const value1 = "value1";
357+
Expected const value1("value1");
358358
Expected const error1 = iceberg::unexpected<E>("error1");
359359
auto const error2 = iceberg::unexpected<E>("error2");
360360
auto const error1Same = iceberg::unexpected<E>("error1");
@@ -369,7 +369,7 @@ TEST(ExpectedTest, EqualityTest) {
369369
using E2 = Type1;
370370
ASSERT_FALSE((std::is_same_v<E, E2>));
371371

372-
Expected const value1 = "value1";
372+
Expected const value1("value1");
373373
Expected const error1 = iceberg::unexpected<E>("error1");
374374
auto const error2 = iceberg::unexpected<E2>("error2");
375375
auto const error1Same = iceberg::unexpected<E2>("error1");
@@ -561,7 +561,7 @@ struct ToType<NoThrowConvertible::Yes> {
561561
ToType(ToType const&) = default;
562562
ToType(ToType&&) = default;
563563

564-
ToType(FromType const&) noexcept {}
564+
explicit ToType(FromType const&) noexcept {}
565565
};
566566

567567
template <>
@@ -570,7 +570,7 @@ struct ToType<NoThrowConvertible::No> {
570570
ToType(ToType const&) = default;
571571
ToType(ToType&&) = default;
572572

573-
ToType(FromType const&) noexcept(false) {}
573+
explicit ToType(FromType const&) noexcept(false) {}
574574
};
575575

576576
} // namespace

0 commit comments

Comments
 (0)