useStlAlgorithm: ranges algorithms + justifica returnDanglingLifetime

This commit is contained in:
JailDesigner committed 2026-05-18 16:28:36 +02:00
1 parent d7db02bafd
commit 822bed0748
5 files changed
+15 -25

No files matched your search

+5 -10
View File
@@ -1,6 +1,8 @@
#include "game/entities/animation_component.hpp"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <stdexcept>
#include <utility>
@@ -33,8 +35,7 @@ namespace {
if (speeds.empty()) {
return std::vector<float>(frame_count, K_DEFAULT_FRAME_SECONDS);
}
// cppcheck-suppress useStlAlgorithm
for (auto& s : speeds) { s = sanitizeSpeed(s); }
std::ranges::transform(speeds, speeds.begin(), sanitizeSpeed);
if (speeds.size() == frame_count) { return speeds; }
if (speeds.size() > frame_count) {
speeds.resize(frame_count);
@@ -50,10 +51,7 @@ namespace {
// component vàlid.
auto parseFrames(const fkyaml::node& anim) -> std::vector<int> {
std::vector<int> frames;
for (const auto& f : anim["frames"]) {
// cppcheck-suppress useStlAlgorithm
frames.push_back(f.get_value<int>());
}
std::ranges::transform(anim["frames"], std::back_inserter(frames), [](const fkyaml::node& f) { return f.template get_value<int>(); });
if (frames.empty()) { frames.push_back(0); }
return frames;
}
@@ -66,10 +64,7 @@ namespace {
std::vector<float> speeds_secs;
const auto& fm = anim["frame_ms"];
if (fm.is_sequence()) {
for (const auto& v : fm) {
// cppcheck-suppress useStlAlgorithm
speeds_secs.push_back(static_cast<float>(v.get_value<int>()) / 1000.0F);
}
std::ranges::transform(fm, std::back_inserter(speeds_secs), [](const fkyaml::node& v) { return static_cast<float>(v.template get_value<int>()) / 1000.0F; });
} else {
const float SECS = static_cast<float>(fm.get_value<int>()) / 1000.0F;
speeds_secs.assign(frame_count, SECS);
@@ -2,6 +2,7 @@
#include <algorithm>
#include <filesystem>
#include <iterator>
#include <string>
#include <vector>
@@ -41,10 +42,7 @@ namespace {
if (!yaml.contains("animations") || !yaml["animations"].contains("swing")) { return; }
const auto& sw = yaml["animations"]["swing"];
if (!sw.contains("active_frames")) { return; }
for (const auto& f : sw["active_frames"]) {
// cppcheck-suppress useStlAlgorithm
active_out.push_back(f.get_value<int>());
}
std::ranges::transform(sw["active_frames"], std::back_inserter(active_out), [](const fkyaml::node& f) { return f.template get_value<int>(); });
}
} // namespace
+3 -2
View File
@@ -1,5 +1,7 @@
#include "game/entities/entity_loader.hpp"
#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <unordered_map>
@@ -51,8 +53,7 @@ namespace EntityLoader {
const auto& list = root["entities"];
if (!list.is_sequence()) { return spawns; }
spawns.reserve(list.size());
// cppcheck-suppress useStlAlgorithm
for (const auto& item : list) { spawns.push_back(item); }
std::ranges::copy(list, std::back_inserter(spawns));
return spawns;
}
+2 -2
View File
@@ -87,7 +87,7 @@ auto EntityManager::add(std::unique_ptr<Entity> entity) -> Entity* {
entity->setEntityManager(this);
Entity* raw = entity.get();
entities_.push_back(std::move(entity));
// cppcheck-suppress returnDanglingLifetime
// cppcheck-suppress returnDanglingLifetime -- fals positiu: std::move transfereix ownership del unique_ptr al vector, però l'objecte apuntat per `raw` no es mou ni es destrueix. El punter continua vàlid mentre el vector el posseesca.
return raw;
}
@@ -124,7 +124,7 @@ auto EntityManager::addSarcophagus(std::unique_ptr<Sarcophagus> sarc) -> Sarcoph
Sarcophagus* raw = sarc.get();
entities_.push_back(std::move(sarc));
++total_sarcs_;
// cppcheck-suppress returnDanglingLifetime
// cppcheck-suppress returnDanglingLifetime -- fals positiu: std::move transfereix ownership al vector però l'objecte no es mou. `raw` continua vàlid (vegeu `add` més amunt).
return raw;
}