Skip to content

Commit 9012de5

Browse files
committed
reproduce sonar cloud issue locally
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
1 parent 0fe8e85 commit 9012de5

1 file changed

Lines changed: 47 additions & 32 deletions

File tree

  • power_grid_model_c/power_grid_model/include/power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/topology.hpp

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,48 @@ class Topology {
344344
return fill_in;
345345
}
346346

347+
// TODO(mgovers): remove this before merge!!!!!
348+
void reproduce_issue() {
349+
std::vector<int> v1{1, 2, 3, 4, 5};
350+
std::vector<int> v2{6, 7, 8, 9, 10};
351+
std::string dummy;
352+
353+
// Versions with modifying: should raise sonar cloud warnings
354+
for (auto const& t : std::views::zip(v1, v2)) {
355+
get<0>(t) = get<1>(t); // Compiles
356+
dummy += std::to_string(get<0>(t)); // Compiles and warns
357+
}
358+
for (auto&& t : std::views::zip(v1, v2)) {
359+
get<0>(t) = get<1>(t); // Compiles
360+
dummy += std::to_string(get<0>(t)); // Compiles and warns
361+
}
362+
for (auto const& [t1, t2] : std::views::zip(v1, v2)) {
363+
t1 = t2; // Compiles
364+
dummy += std::to_string(t1); // Compiles and warns
365+
}
366+
for (auto&& [t1, t2] : std::views::zip(v1, v2)) {
367+
t1 = t2; // Compiles
368+
dummy += std::to_string(t1); // Compiles and warns
369+
}
370+
// Versions with modifying that do not compile
371+
for (auto const& t : std::views::zip(v1, v2) | std::views::as_const) {
372+
// get<0>(t) = get<1>(t); // Does not compile
373+
dummy += std::to_string(get<0>(t)); // Compiles and warns
374+
}
375+
for (auto&& t : std::views::zip(v1, v2) | std::views::as_const) {
376+
// get<0>(t) = get<1>(t); // Does not compile
377+
dummy += std::to_string(get<0>(t)); // Compiles and warns
378+
}
379+
for (auto const& t : std::views::zip(std::as_const(v1), std::as_const(v2))) {
380+
// get<0>(t) = get<1>(t); // Does not compile
381+
dummy += std::to_string(get<0>(t)); // Compiles and warns
382+
}
383+
for (auto&& t : std::views::zip(std::as_const(v1), std::as_const(v2))) {
384+
// get<0>(t) = get<1>(t); // Does not compile
385+
dummy += std::to_string(get<0>(t)); // Compiles and warns
386+
}
387+
}
388+
347389
void couple_branch() {
348390
auto const get_group_pos_if = []([[maybe_unused]] Idx math_group, IntS status, Idx2D const& math_idx) {
349391
if (status == 0) {
@@ -354,7 +396,8 @@ class Topology {
354396
};
355397
// k as branch number for 2-way branch
356398
for (auto&& [idx, branch_node_idx, branch_connected] :
357-
std::views::zip(std::views::iota(0), comp_topo_.branch_node_idx, comp_conn_.branch_connected)) {
399+
std::views::zip(std::views::iota(0), std::as_const(comp_topo_.branch_node_idx),
400+
std::as_const(comp_conn_.branch_connected))) {
358401
assert(std::ssize(branch_connected) == 2); // NOSONAR(R354)
359402

360403
auto const [i, j] = branch_node_idx;
@@ -386,39 +429,11 @@ class Topology {
386429
// set branch idx in coupling
387430
comp_coup_.branch[idx] = Idx2D{.group = math_group, .pos = branch_pos};
388431
}
389-
for (auto&& [idx, branch_node_idx, branch_connected] :
390-
std::views::zip(std::views::iota(0), comp_topo_.branch_node_idx, comp_conn_.branch_connected)) {
391-
assert(std::ssize(branch_connected) == 2); // NOSONAR(R354)
392-
393-
auto const [i, j] = branch_node_idx;
394-
IntS const i_status = branch_connected[0];
395-
IntS const j_status = branch_connected[1];
396-
Idx2D const i_math = comp_coup_.node[i];
397-
Idx2D const j_math = comp_coup_.node[j];
398-
Idx const math_group = [&]() {
399-
if (i_status != 0 && i_math.group != -1) {
400-
return i_math.group;
401-
}
402-
if (j_status != 0 && j_math.group != -1) {
403-
return j_math.group;
404-
}
405-
return Idx{-1};
406-
}();
407-
// skip if no math model connected
408-
if (math_group == -1) {
409-
continue;
410-
}
411-
assert(i_status || j_status);
412-
// get and set branch idx in math model
413-
[[maybe_unused]] BranchIdx const branch_idx{get_group_pos_if(math_group, i_status, i_math),
414-
get_group_pos_if(math_group, j_status, j_math)};
415-
// current branch position index in math model
416-
[[maybe_unused]] auto const branch_pos = math_topology_[math_group].n_branch();
417-
}
418432
// k as branch number for 3-way branch
419433
for (auto&& [idx, i, i_status, j_math] :
420-
std::views::zip(std::views::iota(0), comp_topo_.branch3_node_idx, comp_conn_.branch3_connected,
421-
std::views::drop(comp_coup_.node, comp_topo_.n_node))) {
434+
std::views::zip(std::views::iota(0), std::as_const(comp_topo_.branch3_node_idx),
435+
std::as_const(comp_conn_.branch3_connected),
436+
std::views::drop(std::as_const(comp_coup_.node), std::as_const(comp_topo_.n_node)))) {
422437
std::array<Idx2D, 3> const i_math{
423438
comp_coup_.node[i[0]],
424439
comp_coup_.node[i[1]],

0 commit comments

Comments
 (0)