Skip to content

Commit d9df47a

Browse files
committed
fix sonar + clang-tidy
Signed-off-by: Martijn Govers <[email protected]>
1 parent 42af8b0 commit d9df47a

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/common/iterator_facade.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class IteratorFacade {
4545
return (self <=> other) == std::strong_ordering::equivalent;
4646
}
4747

48+
// NOLINTNEXTLINE(cert-dcl21-cpp) // pre-increment but clang-tidy incorrectly sees this as post-increment
4849
template <typename Self> constexpr std::add_lvalue_reference_t<Self> operator++(this Self& self) {
4950
if constexpr (requires { self.increment(); }) { // NOTE: IteratorFacade should be a friend class
5051
self.increment();
@@ -53,6 +54,7 @@ class IteratorFacade {
5354
}
5455
return self;
5556
}
57+
// NOLINTNEXTLINE(cert-dcl21-cpp) // pre-decrement but clang-tidy incorrectly sees this as post-decrement
5658
template <typename Self> constexpr std::add_lvalue_reference_t<Self> operator--(this Self& self) {
5759
if constexpr (requires { self.decrement(); }) { // NOTE: IteratorFacade should be a friend class
5860
self.decrement();
@@ -62,14 +64,14 @@ class IteratorFacade {
6264
return self;
6365
}
6466
template <typename Self>
65-
constexpr std::remove_cvref_t<Self> operator++(this Self& self, std::integral auto /*idx*/) {
67+
constexpr std::add_const_t<std::remove_reference_t<Self>> operator++(this Self& self, std::integral auto /*idx*/) {
6668
using Result = std::remove_cvref_t<Self>;
6769
Result result{self};
6870
++self;
6971
return result;
7072
}
7173
template <typename Self>
72-
constexpr std::remove_cvref_t<Self> operator--(this Self& self, std::integral auto /*idx*/) {
74+
constexpr std::add_const_t<std::remove_reference_t<Self>> operator--(this Self& self, std::integral auto /*idx*/) {
7375
using Result = std::remove_cvref_t<Self>;
7476
Result result{self};
7577
--self;

tests/cpp_unit_tests/test_iterator_facade.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ namespace {
1313
using power_grid_model::Idx;
1414
using power_grid_model::IdxRange;
1515
using power_grid_model::IdxVector;
16+
using power_grid_model::IntS;
1617
using power_grid_model::IteratorFacade;
1718
using power_grid_model::detail::iterator_facadeable_c;
1819

19-
enum class IteratorFacadeableCalls : Idx {
20+
enum class IteratorFacadeableCalls : IntS {
2021
none = 0,
2122
dereference = 1,
2223
advance = 2,
@@ -49,48 +50,50 @@ template <advance_type_c advance_type_t, typename UnderlyingType> class BaseTest
4950
? IteratorFacadeableCalls::decrement
5051
: IteratorFacadeableCalls::advance;
5152

52-
mutable std::optional<IteratorFacadeableCalls> last_call;
53-
5453
constexpr BaseTestIterator(std::remove_cvref_t<underlying> it) : IteratorFacade{*this}, it_{std::move(it)} {}
5554

5655
constexpr auto operator*() -> reference {
57-
last_call = IteratorFacadeableCalls::dereference;
56+
last_call_ = IteratorFacadeableCalls::dereference;
5857
return *it_;
5958
}
6059
constexpr auto operator*() const -> std::add_lvalue_reference_t<std::add_const_t<value_type>> {
61-
last_call = IteratorFacadeableCalls::dereference;
60+
last_call_ = IteratorFacadeableCalls::dereference;
6261
return *it_;
6362
}
6463

6564
friend constexpr auto operator<=>(BaseTestIterator const& first,
6665
BaseTestIterator const& second) -> std::strong_ordering {
67-
first.last_call = IteratorFacadeableCalls::distance_to;
66+
first.last_call_ = IteratorFacadeableCalls::distance_to;
6867
return *first.it_ <=> *second.it_;
6968
}
7069

7170
constexpr decltype(auto) operator+=(difference_type n) {
72-
last_call = IteratorFacadeableCalls::advance;
71+
last_call_ = IteratorFacadeableCalls::advance;
7372
it_ += n;
7473
return *this;
7574
}
7675
friend constexpr auto operator-(BaseTestIterator const& first, BaseTestIterator const& second) -> difference_type {
77-
first.last_call = IteratorFacadeableCalls::distance_to;
76+
first.last_call_ = IteratorFacadeableCalls::distance_to;
7877
return first.it_ - second.it_;
7978
}
8079

80+
constexpr std::optional<IteratorFacadeableCalls> const& last_call() const { return last_call_; }
81+
constexpr void reset() { last_call_ = std::nullopt; }
82+
8183
private:
8284
underlying it_;
85+
mutable std::optional<IteratorFacadeableCalls> last_call_; // mutable to allow const methods to set it
8386

8487
constexpr void increment()
8588
requires std::same_as<advance_type_t, with_increment_decrement_t>
8689
{
87-
last_call = IteratorFacadeableCalls::increment;
90+
last_call_ = IteratorFacadeableCalls::increment;
8891
++it_;
8992
}
9093
constexpr void decrement()
9194
requires std::same_as<advance_type_t, with_increment_decrement_t>
9295
{
93-
last_call = IteratorFacadeableCalls::decrement;
96+
last_call_ = IteratorFacadeableCalls::decrement;
9497
--it_;
9598
}
9699
};
@@ -170,26 +173,29 @@ TEST_CASE_TEMPLATE("Test IteratorFacade", T, TestIdxVectorIterator, TestIdxVecto
170173
}
171174
SUBCASE("Incremented/Decremented calls") {
172175
auto it = TestIterator{vec.begin() + 5};
173-
REQUIRE(!it.last_call.has_value());
176+
REQUIRE(!it.last_call().has_value());
174177

175178
++it;
176-
REQUIRE(it.last_call.has_value());
177-
CHECK(it.last_call.value() == TestIterator::increment_style);
178-
it.last_call.reset();
179+
REQUIRE(it.last_call().has_value());
180+
CHECK(it.last_call().value() == TestIterator::increment_style);
181+
it.reset();
182+
REQUIRE(!it.last_call().has_value());
179183

180184
it++;
181-
REQUIRE(it.last_call.has_value());
182-
CHECK(it.last_call.value() == TestIterator::increment_style);
183-
it.last_call.reset();
185+
REQUIRE(it.last_call().has_value());
186+
CHECK(it.last_call().value() == TestIterator::increment_style);
187+
it.reset();
188+
REQUIRE(!it.last_call().has_value());
184189

185190
--it;
186-
REQUIRE(it.last_call.has_value());
187-
CHECK(it.last_call.value() == TestIterator::decrement_style);
188-
it.last_call.reset();
191+
REQUIRE(it.last_call().has_value());
192+
CHECK(it.last_call().value() == TestIterator::decrement_style);
193+
it.reset();
194+
REQUIRE(!it.last_call().has_value());
189195

190196
it--;
191-
REQUIRE(it.last_call.has_value());
192-
CHECK(it.last_call.value() == TestIterator::decrement_style);
193-
it.last_call.reset();
197+
REQUIRE(it.last_call().has_value());
198+
CHECK(it.last_call().value() == TestIterator::decrement_style);
199+
it.reset();
194200
}
195201
}

0 commit comments

Comments
 (0)