From aef6b0cc00fb9ba7961bfe69f13c0d5fb01c173a Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 20 Nov 2024 16:06:38 +0000 Subject: [PATCH] Fix several new issues detected by SonarCloud --- client/renderSDL/RenderHandler.cpp | 4 ++-- client/renderSDL/RenderHandler.h | 4 ++-- client/renderSDL/SDLImage.cpp | 11 +++++++---- lib/CCreatureSet.cpp | 2 +- lib/spells/CSpellHandler.cpp | 4 ++-- server/battles/BattleActionProcessor.cpp | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/client/renderSDL/RenderHandler.cpp b/client/renderSDL/RenderHandler.cpp index ccfd5bec8..c76af1242 100644 --- a/client/renderSDL/RenderHandler.cpp +++ b/client/renderSDL/RenderHandler.cpp @@ -55,7 +55,7 @@ std::shared_ptr RenderHandler::getAnimationFile(const AnimationPath & return result; } -std::optional RenderHandler::getPathForScaleFactor(ResourcePath path, std::string factor) +std::optional RenderHandler::getPathForScaleFactor(const ResourcePath & path, const std::string & factor) { if(path.getType() == EResType::IMAGE) { @@ -80,7 +80,7 @@ std::optional RenderHandler::getPathForScaleFactor(ResourcePath pa return std::nullopt; } -std::pair RenderHandler::getScalePath(ResourcePath p) +std::pair RenderHandler::getScalePath(const ResourcePath & p) { auto path = p; int scaleFactor = 1; diff --git a/client/renderSDL/RenderHandler.h b/client/renderSDL/RenderHandler.h index d7b028762..43df617a1 100644 --- a/client/renderSDL/RenderHandler.h +++ b/client/renderSDL/RenderHandler.h @@ -29,8 +29,8 @@ class RenderHandler : public IRenderHandler std::map> fonts; std::shared_ptr getAnimationFile(const AnimationPath & path); - std::optional getPathForScaleFactor(ResourcePath path, std::string factor); - std::pair getScalePath(ResourcePath p); + std::optional getPathForScaleFactor(const ResourcePath & path, const std::string & factor); + std::pair getScalePath(const ResourcePath & p); AnimationLayoutMap & getAnimationLayout(const AnimationPath & path); void initFromJson(AnimationLayoutMap & layout, const JsonNode & config); diff --git a/client/renderSDL/SDLImage.cpp b/client/renderSDL/SDLImage.cpp index 449b3bbfe..84d37e7ee 100644 --- a/client/renderSDL/SDLImage.cpp +++ b/client/renderSDL/SDLImage.cpp @@ -283,7 +283,10 @@ std::shared_ptr SDLImageShared::scaleInteger(int factor, SDL if (factor <= 0) throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor)); - if (palette && surf && surf->format->palette) + if (!surf) + return shared_from_this(); + + if (palette && surf->format->palette) SDL_SetSurfacePalette(surf, palette); SDL_Surface * scaled = nullptr; @@ -306,7 +309,7 @@ std::shared_ptr SDLImageShared::scaleInteger(int factor, SDL // erase our own reference SDL_FreeSurface(scaled); - if (surf && surf->format->palette) + if (surf->format->palette) SDL_SetSurfacePalette(surf, originalPalette); return ret; @@ -314,8 +317,8 @@ std::shared_ptr SDLImageShared::scaleInteger(int factor, SDL std::shared_ptr SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const { - float scaleX = float(size.x) / fullSize.x; - float scaleY = float(size.y) / fullSize.y; + float scaleX = static_cast(size.x) / fullSize.x; + float scaleY = static_cast(size.y) / fullSize.y; if (palette && surf->format->palette) SDL_SetSurfacePalette(surf, palette); diff --git a/lib/CCreatureSet.cpp b/lib/CCreatureSet.cpp index d96f4e79d..f99232333 100644 --- a/lib/CCreatureSet.cpp +++ b/lib/CCreatureSet.cpp @@ -725,7 +725,7 @@ int CStackInstance::getExpRank() const int CStackInstance::getLevel() const { - return std::max(1, static_cast(getType()->getLevel())); + return std::max(1, getType()->getLevel()); } void CStackInstance::giveStackExp(TExpType exp) diff --git a/lib/spells/CSpellHandler.cpp b/lib/spells/CSpellHandler.cpp index e1154b107..79619ac51 100644 --- a/lib/spells/CSpellHandler.cpp +++ b/lib/spells/CSpellHandler.cpp @@ -544,8 +544,8 @@ void CSpell::serializeJson(JsonSerializeFormat & handler) ///CSpell::AnimationInfo CSpell::AnimationItem::AnimationItem() : verticalPosition(VerticalPosition::TOP), - pause(0), - transparency(1) + transparency(1), + pause(0) { } diff --git a/server/battles/BattleActionProcessor.cpp b/server/battles/BattleActionProcessor.cpp index 6855fd6c9..767cfc065 100644 --- a/server/battles/BattleActionProcessor.cpp +++ b/server/battles/BattleActionProcessor.cpp @@ -917,7 +917,7 @@ void BattleActionProcessor::makeAttack(const CBattleInfoCallback & battle, const handleAttackBeforeCasting(battle, ranged, attacker, defender); // If the attacker or defender is not alive before the attack action, the action should be skipped. - if((attacker && !attacker->alive()) || (defender && !defender->alive())) + if((!attacker->alive()) || (defender && !defender->alive())) return; FireShieldInfo fireShield;