Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply static analysis suggestions #1201

Merged
merged 27 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a7ec8b3
Constexpr inline is redundant.
jcoupey Dec 10, 2024
35338e6
Do not use alternative operators.
jcoupey Dec 10, 2024
8b460d2
Do not use size to check for emptyness.
jcoupey Dec 10, 2024
925c3ee
Use std::format in a couple places.
jcoupey Dec 10, 2024
b8325fd
Use default operator== whenever possible.
jcoupey Dec 10, 2024
1c9c105
Remove nested if statements.
jcoupey Dec 10, 2024
88711dc
Use raw string literal.
jcoupey Dec 10, 2024
7ffe35a
Use a lambda instead of taking the address of library functions.
jcoupey Dec 10, 2024
42b59cd
Make sure to use operator |= with bool variables.
jcoupey Dec 10, 2024
1f9cf28
Use transparent equality and hasher with std::unordered_set<std::stri…
jcoupey Dec 10, 2024
7869819
Handle part of the send_then_receive work in functions.
jcoupey Dec 10, 2024
b7574a4
Reduce duplication in ssl_send_then_receive.
jcoupey Dec 10, 2024
499ba86
Apply some readability fixes.
jcoupey Dec 10, 2024
de07144
Add pointer qualification to auto-typed variable deduced to pointer.
jcoupey Dec 10, 2024
91f5e38
Use small underlying type for enums.
jcoupey Dec 10, 2024
6a30fb4
Remove some headers.
jcoupey Dec 10, 2024
d1700d3
Replace iterator-based loop with some range stuff.
jcoupey Dec 11, 2024
5c43145
Update tidy rules.
jcoupey Dec 11, 2024
0ca4451
Const all the things!
jcoupey Dec 11, 2024
aca2d5a
Explicit lambda capture.
jcoupey Dec 11, 2024
9870e04
Avoid superfluous const param in function declarations.
jcoupey Dec 11, 2024
7328ff9
Missing commas in clang-tidy config.
jcoupey Dec 11, 2024
e7ebca3
Remove unused function.
jcoupey Dec 11, 2024
4909481
Remove some magic numbers.
jcoupey Dec 11, 2024
94a0dd3
Consistent parameter naming.
jcoupey Dec 11, 2024
16faa6c
Remove some more magic numbers.
jcoupey Dec 12, 2024
485ee2b
Mention tidying round in changelog.
jcoupey Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ Checks: >
-bugprone-too-small-loop-variable,
-clang-analyzer-*,
cppcoreguidelines-*,
- cppcoreguidelines-init-variables,
- cppcoreguidelines-narrowing-conversions,
- cppcoreguidelines-non-private-member-variables-in-classes,
- cppcoreguidelines-owning-memory,
- cppcoreguidelines-pro-bounds-pointer-arithmetic,
- cppcoreguidelines-pro-type-member-init,
- cppcoreguidelines-special-member-functions,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-init-variables,
-cppcoreguidelines-narrowing-conversions,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-member-init,
-cppcoreguidelines-special-member-functions,
google-*,
-google-default-arguments,
-google-explicit-constructor,
Expand All @@ -23,21 +25,24 @@ Checks: >
llvm-*,
-llvm-header-guard,
misc-*,
-misc-include-cleaner,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-return-braced-init-list,
-modernize-use-trailing-return-type,
-modernize-use-nodiscard,
performance-*,
- performance-move-const-arg,
-performance-avoid-endl,
-performance-move-const-arg,
readability-*,
-readability-function-size,
-readability-identifier-length,
-readability-function-cognitive-complexity,
-readability-named-parameter,
-readability-convert-member-functions-to-static,
-readability-implicit-bool-conversion,
-readability-avoid-const-params-in-decls
-readability-avoid-const-params-in-decls,
-readability-uppercase-literal-suffix,

WarningsAsErrors: '*'
HeaderFilterRegex: '.*'
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#### Internals

- Iterator type required by `TWRoute::replace` function (#1103)
- Address some of the SonaQube and clang-tidy reports (#1200)

#### CI

Expand Down
2 changes: 1 addition & 1 deletion libvroom_examples/libvroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void log_solution(const vroom::Solution& sol, bool geometry) {
std::cout << type;

// Add job/pickup/delivery/break ids.
if (step.step_type != vroom::STEP_TYPE::START and
if (step.step_type != vroom::STEP_TYPE::START &&
step.step_type != vroom::STEP_TYPE::END) {
std::cout << " " << step.id;
}
Expand Down
17 changes: 9 additions & 8 deletions src/algorithms/heuristics/heuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline void seed_route(const Input& input,
continue;
}

bool is_pickup = (current_job.type == JOB_TYPE::PICKUP);
const bool is_pickup = (current_job.type == JOB_TYPE::PICKUP);

if (route.size() + (is_pickup ? 2 : 1) > vehicle.max_tasks) {
continue;
Expand All @@ -57,9 +57,9 @@ inline void seed_route(const Input& input,
higher_amount < current_job.delivery);
}
if (init == INIT::EARLIEST_DEADLINE) {
Duration current_deadline = is_pickup
? input.jobs[job_rank + 1].tws.back().end
: current_job.tws.back().end;
const Duration current_deadline =
is_pickup ? input.jobs[job_rank + 1].tws.back().end
: current_job.tws.back().end;
try_validity = (current_deadline < earliest_deadline);
}
if (init == INIT::FURTHEST) {
Expand Down Expand Up @@ -177,8 +177,9 @@ inline Eval fill_route(const Input& input,
const auto current_eval =
utils::addition_cost(input, job_rank, vehicle, route.route, r);

double current_cost = static_cast<double>(current_eval.cost) -
lambda * static_cast<double>(regrets[job_rank]);
const double current_cost =
static_cast<double>(current_eval.cost) -
lambda * static_cast<double>(regrets[job_rank]);

if (current_cost < best_cost &&
(vehicle.ok_for_range_bounds(route_eval + current_eval)) &&
Expand Down Expand Up @@ -266,15 +267,15 @@ inline Eval fill_route(const Input& input,
current_eval = p_add + d_adds[delivery_r];
}

double current_cost =
const double current_cost =
current_eval.cost -
lambda * static_cast<double>(regrets[job_rank]);

if (current_cost < best_cost) {
modified_with_pd.push_back(job_rank + 1);

// Update best cost depending on validity.
bool valid =
const bool valid =
(vehicle.ok_for_range_bounds(route_eval + current_eval)) &&
route
.is_valid_addition_for_capacity_inclusion(input,
Expand Down
8 changes: 4 additions & 4 deletions src/algorithms/kruskal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ UndirectedGraph<T> minimum_spanning_tree(const UndirectedGraph<T>& graph) {
std::iota(representative.begin(), representative.end(), 0);

for (const auto& edge : edges) {
Index first_vertex = edge.get_first_vertex();
Index second_vertex = edge.get_second_vertex();
const Index first_vertex = edge.get_first_vertex();
const Index second_vertex = edge.get_second_vertex();

Index first_rep = representative[first_vertex];
Index second_rep = representative[second_vertex];
const Index first_rep = representative[first_vertex];
const Index second_rep = representative[second_vertex];
if (first_rep != second_rep) {
// Adding current edge won't create a cycle as vertices are in
// separate connected components.
Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/local_search/insertion_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ compute_best_insertion_single(const Input& input,
for (Index rank = sol_state.insertion_ranks_begin[v][j];
rank < sol_state.insertion_ranks_end[v][j];
++rank) {
Eval current_eval =
const Eval current_eval =
utils::addition_cost(input, j, v_target, route.route, rank);
if (current_eval.cost < result.eval.cost &&
v_target.ok_for_range_bounds(sol_state.route_evals[v] +
Expand Down Expand Up @@ -120,7 +120,7 @@ RouteInsertion compute_best_insertion_pd(const Input& input,
valid_delivery_insertions[d_rank] =
route.is_valid_addition_for_tw_without_max_load(input, j + 1, d_rank);
}
found_valid |= valid_delivery_insertions[d_rank];
found_valid |= static_cast<bool>(valid_delivery_insertions[d_rank]);
}

if (!found_valid) {
Expand All @@ -131,7 +131,7 @@ RouteInsertion compute_best_insertion_pd(const Input& input,
for (Index pickup_r = sol_state.insertion_ranks_begin[v][j];
pickup_r < sol_state.insertion_ranks_end[v][j];
++pickup_r) {
Eval p_add =
const Eval p_add =
utils::addition_cost(input, j, v_target, route.route, pickup_r);
if (result.eval < p_add) {
// Even without delivery insertion more expensive than current best.
Expand Down
41 changes: 21 additions & 20 deletions src/algorithms/local_search/local_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ void LocalSearch<Route,
continue;
}

Priority u_priority = _input.jobs[u].priority;
const Priority u_priority = _input.jobs[u].priority;
const auto& u_pickup = _input.jobs[u].pickup;
const auto& u_delivery = _input.jobs[u].delivery;

Expand Down Expand Up @@ -504,7 +504,7 @@ void LocalSearch<Route,
t_rank,
u);

bool better_if_valid =
const bool better_if_valid =
(best_priorities[source] < priority_gain) ||
(best_priorities[source] == priority_gain &&
best_gains[source][source] < r.gain());
Expand Down Expand Up @@ -552,11 +552,11 @@ void LocalSearch<Route,

const auto& job_s_type = _input.jobs[s_job_rank].type;

bool both_s_single =
const bool both_s_single =
(job_s_type == JOB_TYPE::SINGLE) &&
(_input.jobs[s_next_job_rank].type == JOB_TYPE::SINGLE);

bool is_s_pickup =
const bool is_s_pickup =
(job_s_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[source][s_rank] == s_rank + 1);

Expand Down Expand Up @@ -596,11 +596,11 @@ void LocalSearch<Route,

const auto& job_t_type = _input.jobs[t_job_rank].type;

bool both_t_single =
const bool both_t_single =
(job_t_type == JOB_TYPE::SINGLE) &&
(_input.jobs[t_next_job_rank].type == JOB_TYPE::SINGLE);

bool is_t_pickup =
const bool is_t_pickup =
(job_t_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[target][t_rank] == t_rank + 1);

Expand Down Expand Up @@ -718,11 +718,11 @@ void LocalSearch<Route,
const auto t_next_job_rank = _sol[target].route[t_rank + 1];
const auto& job_t_type = _input.jobs[t_job_rank].type;

bool both_t_single =
const bool both_t_single =
(job_t_type == JOB_TYPE::SINGLE) &&
(_input.jobs[t_next_job_rank].type == JOB_TYPE::SINGLE);

bool is_t_pickup =
const bool is_t_pickup =
(job_t_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[target][t_rank] == t_rank + 1);

Expand Down Expand Up @@ -1223,11 +1223,11 @@ void LocalSearch<Route,
const auto job_s_type = _input.jobs[_sol[source].route[s_rank]].type;
const auto s_next_job_rank = _sol[source].route[s_rank + 1];

bool both_s_single =
const bool both_s_single =
(job_s_type == JOB_TYPE::SINGLE) &&
(_input.jobs[s_next_job_rank].type == JOB_TYPE::SINGLE);

bool is_s_pickup =
const bool is_s_pickup =
(job_s_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[source][s_rank] == s_rank + 1);

Expand All @@ -1244,12 +1244,12 @@ void LocalSearch<Route,
for (unsigned t_rank = s_rank + 3; t_rank < end_t_rank; ++t_rank) {
const auto& job_t_type = _input.jobs[_sol[target].route[t_rank]].type;

bool both_t_single =
const bool both_t_single =
(job_t_type == JOB_TYPE::SINGLE) &&
(_input.jobs[_sol[target].route[t_rank + 1]].type ==
JOB_TYPE::SINGLE);

bool is_t_pickup =
const bool is_t_pickup =
(job_t_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[target][t_rank] == t_rank + 1);

Expand Down Expand Up @@ -1316,12 +1316,12 @@ void LocalSearch<Route,

const auto& job_t_type = _input.jobs[_sol[target].route[t_rank]].type;

bool both_t_single =
const bool both_t_single =
(job_t_type == JOB_TYPE::SINGLE) &&
(_input.jobs[_sol[source].route[t_rank + 1]].type ==
JOB_TYPE::SINGLE);

bool is_t_pickup =
const bool is_t_pickup =
(job_t_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[target][t_rank] == t_rank + 1);

Expand Down Expand Up @@ -1375,7 +1375,7 @@ void LocalSearch<Route,
_sol_state.weak_insertion_ranks_begin[source][s_job_rank];
if (_input.jobs[s_job_rank].type == JOB_TYPE::DELIVERY) {
// Don't move a delivery before its matching pickup.
Index begin_candidate =
const Index begin_candidate =
_sol_state.matching_pickup_rank[source][s_rank] + 1;
begin_t_rank = std::max(begin_t_rank, begin_candidate);
}
Expand Down Expand Up @@ -1425,11 +1425,12 @@ void LocalSearch<Route,
for (unsigned s_rank = 0; s_rank < _sol[source].size() - 1; ++s_rank) {
const auto& job_type = _input.jobs[_sol[source].route[s_rank]].type;

bool both_single = (job_type == JOB_TYPE::SINGLE) &&
(_input.jobs[_sol[source].route[s_rank + 1]].type ==
JOB_TYPE::SINGLE);
const bool both_single =
(job_type == JOB_TYPE::SINGLE) &&
(_input.jobs[_sol[source].route[s_rank + 1]].type ==
JOB_TYPE::SINGLE);

bool is_pickup =
const bool is_pickup =
(job_type == JOB_TYPE::PICKUP) &&
(_sol_state.matching_delivery_rank[source][s_rank] == s_rank + 1);

Expand Down Expand Up @@ -1548,7 +1549,7 @@ void LocalSearch<Route,
}

// Matching delivery rank in source route.
unsigned s_d_rank =
const Index s_d_rank =
_sol_state.matching_delivery_rank[source][s_p_rank];

if (!_input.vehicle_ok_with_job(target,
Expand Down
84 changes: 40 additions & 44 deletions src/algorithms/local_search/route_split_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,26 @@ compute_best_route_split_choice(const Input& input,
continue;
}

if (current_end_eval < second_best_end_eval) {
// Worth checking end route full validity.

if (empty_routes[v_rank].is_valid_addition_for_tw(input,
end_delivery,
source.route.begin() +
r,
source.route.end(),
0,
0)) {
if (current_end_eval < first_best_end_eval) {
// We have a new first choice.
second_v_end = first_v_end;
second_best_end_eval = first_best_end_eval;

first_v_end = v_rank;
first_best_end_eval = current_end_eval;
} else {
// We have a new second choice.
second_v_end = v_rank;
second_best_end_eval = current_end_eval;
}
if (current_end_eval < second_best_end_eval &&
// Worth checking end route full validity.
empty_routes[v_rank].is_valid_addition_for_tw(input,
end_delivery,
source.route.begin() +
r,
source.route.end(),
0,
0)) {
if (current_end_eval < first_best_end_eval) {
// We have a new first choice.
second_v_end = first_v_end;
second_best_end_eval = first_best_end_eval;

first_v_end = v_rank;
first_best_end_eval = current_end_eval;
} else {
// We have a new second choice.
second_v_end = v_rank;
second_best_end_eval = current_end_eval;
}
}
}
Expand Down Expand Up @@ -160,28 +158,26 @@ compute_best_route_split_choice(const Input& input,
continue;
}

if (current_begin_eval < second_best_begin_eval) {
// Worth checking begin route full validity.

if (empty_routes[v_rank].is_valid_addition_for_tw(input,
begin_delivery,
source.route.begin(),
source.route.begin() +
r,
0,
0)) {
if (current_begin_eval < first_best_begin_eval) {
// We have a new first choice.
second_v_begin = first_v_begin;
second_best_begin_eval = first_best_begin_eval;

first_v_begin = v_rank;
first_best_begin_eval = current_begin_eval;
} else {
// We have a new second choice.
second_v_begin = v_rank;
second_best_begin_eval = current_begin_eval;
}
if (current_begin_eval < second_best_begin_eval &&
// Worth checking begin route full validity.
empty_routes[v_rank].is_valid_addition_for_tw(input,
begin_delivery,
source.route.begin(),
source.route.begin() +
r,
0,
0)) {
if (current_begin_eval < first_best_begin_eval) {
// We have a new first choice.
second_v_begin = first_v_begin;
second_best_begin_eval = first_best_begin_eval;

first_v_begin = v_rank;
first_best_begin_eval = current_begin_eval;
} else {
// We have a new second choice.
second_v_begin = v_rank;
second_best_begin_eval = current_begin_eval;
}
}
}
Expand Down
Loading