Skip to content

Commit 208a620

Browse files
Merge pull request #882 from PowerGridModel/feature/clang-tidy
Cleanup clang tidy
2 parents 3f086d7 + bbc50fa commit 208a620

35 files changed

+169
-159
lines changed

.github/workflows/clang-tidy.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ concurrency:
4444

4545
jobs:
4646
clang-tidy:
47-
runs-on: ubuntu-22.04
47+
runs-on: ubuntu-24.04
4848
strategy:
4949
matrix:
5050
build-option: [ debug, release ]
@@ -60,10 +60,10 @@ jobs:
6060
- name: Install packages
6161
run: |
6262
sudo apt-get update
63-
sudo apt-get install -y ninja-build clang-15 clang-tidy-15
64-
sudo ln -s /usr/bin/clang-15 /usr/local/bin/clang
65-
sudo ln -s /usr/bin/clang++-15 /usr/local/bin/clang++
66-
sudo ln -s /usr/bin/clang-tidy-15 /usr/local/bin/clang-tidy
63+
sudo apt-get install -y ninja-build clang-18 clang-tidy-18
64+
sudo ln -s /usr/bin/clang-18 /usr/local/bin/clang
65+
sudo ln -s /usr/bin/clang++-18 /usr/local/bin/clang++
66+
sudo ln -s /usr/bin/clang-tidy-18 /usr/local/bin/clang-tidy
6767
6868
- name: Enable brew
6969
run: |

docs/advanced_documentation/build-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export CXX=clang++-18 # or g++-13
210210
export CC=clang-18 # gcc-13
211211
export CMAKE_PREFIX_PATH=/home/linuxbrew/.linuxbrew
212212
export LLVM_COV=llvm-cov-18
213-
export CLANG_TIDY=clang-tidy-15 # only if you want to use one of the clang-tidy presets
213+
export CLANG_TIDY=clang-tidy-18 # only if you want to use one of the clang-tidy presets
214214
```
215215
216216
### Ubuntu Software Packages

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/serialization/deserializer.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ using nlohmann::json;
4444
// visitor for json conversion
4545
struct JsonMapArrayData {
4646
size_t size{};
47-
msgpack::sbuffer buffer{};
47+
msgpack::sbuffer buffer;
4848
};
4949

5050
struct JsonSAXVisitor {
@@ -130,8 +130,8 @@ struct JsonSAXVisitor {
130130
throw SerializationError{ss.str()};
131131
}
132132

133-
std::stack<JsonMapArrayData> data_buffers{};
134-
msgpack::sbuffer root_buffer{};
133+
std::stack<JsonMapArrayData> data_buffers;
134+
msgpack::sbuffer root_buffer;
135135
};
136136

137137
// visitors for parsing
@@ -192,8 +192,15 @@ struct MapArrayVisitor : DefaultErrorVisitor<MapArrayVisitor<map_array>> {
192192
std::same_as<map_array, visit_map_t> || std::same_as<map_array, visit_map_array_t>;
193193
static constexpr bool enable_array =
194194
std::same_as<map_array, visit_array_t> || std::same_as<map_array, visit_map_array_t>;
195-
static constexpr std::string_view static_err_msg =
196-
enable_map ? (enable_array ? "Map or an array expected." : "Map expected.") : "Array expected.";
195+
static constexpr std::string_view static_err_msg = [] {
196+
if constexpr (enable_map && enable_array) {
197+
return "Map or an array expected.";
198+
} else if constexpr (enable_map) {
199+
return "Map expected.";
200+
} else {
201+
return "Array expected.";
202+
}
203+
}();
197204

198205
Idx size{};
199206
bool is_map{};
@@ -228,7 +235,7 @@ struct MapArrayVisitor : DefaultErrorVisitor<MapArrayVisitor<map_array>> {
228235
struct StringVisitor : DefaultErrorVisitor<StringVisitor> {
229236
static constexpr std::string_view static_err_msg = "String expected.";
230237

231-
std::string_view str{};
238+
std::string_view str;
232239
bool visit_str(const char* v, uint32_t size) {
233240
str = {v, size};
234241
return true;

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/serialization/serializer.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ struct MapArray {
7676
struct JsonConverter : msgpack::null_visitor {
7777
static constexpr char sep_char = ' ';
7878

79-
Idx indent;
80-
Idx max_indent_level;
81-
std::stringstream ss{};
82-
std::stack<MapArray> map_array{};
79+
Idx indent{};
80+
Idx max_indent_level{};
81+
std::stringstream ss{}; // NOLINT(readability-redundant-member-init)
82+
std::stack<MapArray> map_array{}; // NOLINT(readability-redundant-member-init)
8383

8484
void print_indent() {
8585
if (indent < 0) {
@@ -293,7 +293,7 @@ class Serializer {
293293
std::vector<ComponentBuffer> component_buffers_; // list of components, then all scenario flatten
294294

295295
// msgpack pakcer
296-
msgpack::sbuffer msgpack_buffer_{};
296+
msgpack::sbuffer msgpack_buffer_;
297297
msgpack::packer<msgpack::sbuffer> packer_;
298298
bool use_compact_list_{};
299299
std::map<MetaComponent const*, std::vector<MetaAttribute const*>> attributes_;
@@ -506,12 +506,10 @@ class Serializer {
506506
}
507507

508508
void pack_element_in_list(columnar_t /*tag*/, BufferView const& element_buffer, MetaComponent const& /*component*/,
509-
std::span<MetaAttribute const* const> attributes) {
509+
[[maybe_unused]] std::span<MetaAttribute const* const> attributes) {
510510
assert(is_columnar(element_buffer));
511511
assert(element_buffer.reordered_attribute_buffers.size() == attributes.size());
512512

513-
(void)attributes; // suppress unused variable in release mode
514-
515513
pack_array(element_buffer.reordered_attribute_buffers.size());
516514
for (auto const& attribute_buffer : element_buffer.reordered_attribute_buffers) {
517515
if (check_nan(attribute_buffer, element_buffer.idx)) {
@@ -606,7 +604,7 @@ class Serializer {
606604
});
607605
}
608606
void pack_attribute(AttributeBuffer<void const> const& attribute_buffer, Idx idx) {
609-
return ctype_func_selector(attribute_buffer.meta_attribute->ctype, [&]<class T> {
607+
ctype_func_selector(attribute_buffer.meta_attribute->ctype, [this, &attribute_buffer, idx]<class T> {
610608
packer_.pack(*(reinterpret_cast<T const*>(attribute_buffer.data) + idx));
611609
});
612610
}

power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ template <symmetry_tag sym_type> struct BranchShortCircuitSolverOutput {
5454

5555
// fault math calculation parameters and math output
5656
struct FaultCalcParam {
57-
DoubleComplex y_fault{};
57+
DoubleComplex y_fault;
5858
FaultType fault_type{};
5959
FaultPhase fault_phase{};
6060
};
@@ -126,7 +126,7 @@ static_assert(sensor_calc_param_type<PowerSensorCalcParam<asymmetric_t>>);
126126
struct TransformerTapRegulatorCalcParam {
127127
double u_set{};
128128
double u_band{};
129-
DoubleComplex z_compensation{};
129+
DoubleComplex z_compensation;
130130
IntS status{};
131131
};
132132

@@ -193,7 +193,7 @@ struct SourceCalcParam {
193193
DoubleComplex y1;
194194
DoubleComplex y0;
195195

196-
template <symmetry_tag sym> inline ComplexTensor<sym> y_ref() const {
196+
template <symmetry_tag sym> ComplexTensor<sym> y_ref() const {
197197
if constexpr (is_symmetric_v<sym>) {
198198
return y1;
199199
} else {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,20 @@ using IntSVector = std::vector<IntS>;
9191
template <class T, class... Ts>
9292
concept is_in_list_c = (std::same_as<std::remove_const_t<T>, Ts> || ...);
9393

94+
namespace capturing {
95+
// perfect forward into void
96+
template <class... T>
97+
constexpr void into_the_void(T&&... /*ignored*/) { // NOLINT(cppcoreguidelines-missing-std-forward)
98+
// do nothing; the constexpr allows all compilers to optimize this away
99+
}
100+
} // namespace capturing
101+
94102
// functor to include all
95103
struct IncludeAll {
96-
template <class... T> constexpr bool operator()(T&&... /*ignored*/) const { return true; }
104+
template <class... T> consteval bool operator()(T&&... args) const {
105+
capturing::into_the_void(std::forward<T>(args)...);
106+
return true;
107+
}
97108
};
98109
constexpr IncludeAll include_all{};
99110

power_grid_model_c/power_grid_model/include/power_grid_model/common/grouped_index_vector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class DenseGroupedIdxVector {
161161

162162
IdxVector const* dense_vector_{};
163163
Idx group_{};
164-
std::pair<group_iterator, group_iterator> group_range_{};
164+
std::pair<group_iterator, group_iterator> group_range_;
165165

166166
friend class boost::iterator_core_access;
167167

power_grid_model_c/power_grid_model/include/power_grid_model/common/iterator_like_concepts.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ concept iterator_like = requires(T const t) {
1616

1717
template <typename T, typename ElementType>
1818
concept forward_iterator_like = std::regular<T> && iterator_like<T, ElementType> && requires(T t) {
19-
{ t++ } -> std::same_as<T>;
20-
{ ++t } -> std::same_as<T&>;
19+
{ t++ } -> std::same_as<T>; // NOLINT(bugprone-inc-dec-in-conditions)
20+
{ ++t } -> std::same_as<T&>; // NOLINT(bugprone-inc-dec-in-conditions)
2121
};
2222

2323
template <typename T, typename ElementType>
2424
concept bidirectional_iterator_like = forward_iterator_like<T, ElementType> && requires(T t) {
25-
{ t-- } -> std::same_as<T>;
26-
{ --t } -> std::same_as<T&>;
25+
{ t-- } -> std::same_as<T>; // NOLINT(bugprone-inc-dec-in-conditions)
26+
{ --t } -> std::same_as<T&>; // NOLINT(bugprone-inc-dec-in-conditions)
2727
};
2828

2929
template <typename T, typename ElementType>

power_grid_model_c/power_grid_model/include/power_grid_model/common/three_phase_tensor.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ inline void add_diag(Eigen::ArrayBase<DerivedA>& x, Eigen::ArrayBase<DerivedB> c
281281
}
282282
template <rk2_tensor DerivedA, column_vector DerivedB>
283283
inline void add_diag(Eigen::ArrayBase<DerivedA>&& x, Eigen::ArrayBase<DerivedB> const& y) {
284-
x.matrix().diagonal() += y.matrix();
284+
std::move(x).matrix().diagonal() += y.matrix();
285285
}
286286

287287
// zero tensor
@@ -348,17 +348,22 @@ inline auto all_zero(RealValue<asymmetric_t> const& value) { return (value == Re
348348
//
349349
// The function assumes that the current value is normalized and new value should be normalized with scalar
350350
template <symmetry_tag sym, class Proxy>
351-
inline void update_real_value(RealValue<sym> const& new_value, Proxy&& current_value, double scalar) {
351+
inline void
352+
update_real_value(RealValue<sym> const& new_value, Proxy&& current_value,
353+
double scalar) { // NOLINT(cppcoreguidelines-missing-std-forward) // perfect forward into void
352354
if constexpr (is_symmetric_v<sym>) {
353355
if (!is_nan(new_value)) {
354-
current_value = scalar * new_value;
356+
std::forward<Proxy>(current_value) = scalar * new_value;
357+
} else {
358+
capturing::into_the_void<Proxy>(std::forward<Proxy>(current_value));
355359
}
356360
} else {
357361
for (size_t i = 0; i != 3; ++i) {
358362
if (!is_nan(new_value(i))) {
359-
current_value(i) = scalar * new_value(i);
363+
current_value(i) = scalar * new_value(i); // can't forward due to runtime element access
360364
}
361365
}
366+
capturing::into_the_void<Proxy>(std::forward<Proxy>(current_value));
362367
}
363368
}
364369

power_grid_model_c/power_grid_model/include/power_grid_model/component/fault.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class Fault final : public Base {
193193
double r_f_;
194194
double x_f_;
195195

196-
void check_sanity() {
196+
void check_sanity() const {
197197
using enum FaultPhase;
198198

199199
auto const check_supported = [&](auto const& iterable) {
@@ -203,15 +203,19 @@ class Fault final : public Base {
203203
};
204204
switch (fault_type_) {
205205
case FaultType::three_phase:
206-
return check_supported(std::array{FaultPhase::nan, default_value, abc});
206+
check_supported(std::array{FaultPhase::nan, default_value, abc});
207+
return;
207208
case FaultType::single_phase_to_ground:
208-
return check_supported(std::array{FaultPhase::nan, default_value, a, b, c});
209+
check_supported(std::array{FaultPhase::nan, default_value, a, b, c});
210+
return;
209211
case FaultType::two_phase:
210212
[[fallthrough]];
211213
case FaultType::two_phase_to_ground:
212-
return check_supported(std::array{FaultPhase::nan, default_value, ab, ac, bc});
214+
check_supported(std::array{FaultPhase::nan, default_value, ab, ac, bc});
215+
return;
213216
case FaultType::nan:
214-
return check_supported(std::array{FaultPhase::nan, default_value, abc, a, b, c, ab, ac, bc});
217+
check_supported(std::array{FaultPhase::nan, default_value, abc, a, b, c, ab, ac, bc});
218+
return;
215219
default:
216220
throw InvalidShortCircuitType(fault_type_);
217221
}

0 commit comments

Comments
 (0)