1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Fixes for various minor issues detected by Sonar Cloud

This commit is contained in:
Ivan Savenko 2024-06-01 11:48:30 +00:00
parent df83fa33a1
commit b8beb4fb13
29 changed files with 58 additions and 61 deletions

View File

@ -603,7 +603,7 @@ void AIGateway::heroGotLevel(const CGHeroInstance * hero, PrimarySkill pskill, s
if(hPtr.validAndSet())
{
std::unique_lock<std::mutex> lockGuard(nullkiller->aiStateMutex);
std::unique_lock lockGuard(nullkiller->aiStateMutex);
nullkiller->heroManager->update();
@ -683,7 +683,7 @@ void AIGateway::showBlockingDialog(const std::string & text, const std::vector<C
sel = components.size();
{
std::unique_lock<std::mutex> mxLock(nullkiller->aiStateMutex);
std::unique_lock mxLock(nullkiller->aiStateMutex);
// TODO: Find better way to understand it is Chest of Treasures
if(hero.validAndSet()
@ -804,7 +804,7 @@ void AIGateway::makeTurn()
auto day = cb->getDate(Date::DAY);
logAi->info("Player %d (%s) starting turn, day %d", playerID, playerID.toString(), day);
boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
boost::shared_lock gsLock(CGameState::mutex);
setThreadName("AIGateway::makeTurn");
if(nullkiller->isOpenMap())
@ -864,7 +864,7 @@ void AIGateway::performObjectInteraction(const CGObjectInstance * obj, HeroPtr h
{
makePossibleUpgrades(h.get());
std::unique_lock<std::mutex> lockGuard(nullkiller->aiStateMutex);
std::unique_lock lockGuard(nullkiller->aiStateMutex);
if(!h->visitedTown->garrisonHero || !nullkiller->isHeroLocked(h->visitedTown->garrisonHero))
moveCreaturesToHero(h->visitedTown);
@ -1529,7 +1529,7 @@ void AIGateway::requestActionASAP(std::function<void()> whatToDo)
{
setThreadName("AIGateway::requestActionASAP::whatToDo");
SET_GLOBAL_STATE(this);
boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
boost::shared_lock gsLock(CGameState::mutex);
whatToDo();
});

View File

@ -216,7 +216,7 @@ void Nullkiller::decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, i
void Nullkiller::resetAiState()
{
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
std::unique_lock lockGuard(aiStateMutex);
lockedResources = TResources();
scanDepth = ScanDepth::MAIN_FULL;
@ -236,7 +236,7 @@ void Nullkiller::updateAiState(int pass, bool fast)
{
boost::this_thread::interruption_point();
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
std::unique_lock lockGuard(aiStateMutex);
auto start = std::chrono::high_resolution_clock::now();

View File

@ -217,7 +217,7 @@ ExchangeResult HeroExchangeMap::tryExchangeNoLock(const ChainActor * other)
ExchangeResult result;
{
boost::shared_lock<boost::shared_mutex> lock(sync, boost::try_to_lock);
boost::shared_lock lock(sync, boost::try_to_lock);
if(!lock.owns_lock())
{
@ -237,7 +237,7 @@ ExchangeResult HeroExchangeMap::tryExchangeNoLock(const ChainActor * other)
}
{
boost::unique_lock<boost::shared_mutex> uniqueLock(sync, boost::try_to_lock);
boost::unique_lock uniqueLock(sync, boost::try_to_lock);
if(!uniqueLock.owns_lock())
{

View File

@ -773,7 +773,7 @@ void VCAI::makeTurn()
auto day = cb->getDate(Date::DAY);
logAi->info("Player %d (%s) starting turn, day %d", playerID, playerID.toString(), day);
boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
boost::shared_lock gsLock(CGameState::mutex);
setThreadName("VCAI::makeTurn");
switch(cb->getDate(Date::DAY_OF_WEEK))
@ -2479,7 +2479,7 @@ void VCAI::requestActionASAP(std::function<void()> whatToDo)
{
setThreadName("VCAI::requestActionASAP::whatToDo");
SET_GLOBAL_STATE(this);
boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
boost::shared_lock gsLock(CGameState::mutex);
whatToDo();
});

View File

@ -1481,7 +1481,7 @@ void CPlayerInterface::playerBlocked(int reason, bool start)
void CPlayerInterface::update()
{
// Make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
boost::shared_lock gsLock(CGameState::mutex);
// While mutexes were locked away we may be have stopped being the active interface
if (LOCPLINT != this)

View File

@ -173,7 +173,7 @@ void CServerHandler::threadRunNetwork()
try {
networkHandler->run();
}
catch (const TerminationRequestedException & e)
catch (const TerminationRequestedException &)
{
logGlobal->info("Terminating network thread");
return;

View File

@ -408,7 +408,7 @@ void CClient::handlePack(CPack * pack)
apply->applyOnClBefore(this, pack);
logNetwork->trace("\tMade first apply on cl: %s", typeid(*pack).name());
{
boost::unique_lock<boost::shared_mutex> lock(CGameState::mutex);
boost::unique_lock lock(CGameState::mutex);
gs->apply(pack);
}
logNetwork->trace("\tApplied on gs: %s", typeid(*pack).name());

View File

@ -33,7 +33,7 @@ class ConditionalWait
void set(bool value)
{
boost::unique_lock<std::mutex> lock(mx);
std::unique_lock lock(mx);
isBusyValue = value;
}
@ -59,15 +59,14 @@ public:
bool isBusy()
{
std::unique_lock<std::mutex> lock(mx);
std::unique_lock lock(mx);
return isBusyValue;
}
void waitWhileBusy()
{
std::unique_lock<std::mutex> un(mx);
while(isBusyValue)
cond.wait(un);
std::unique_lock un(mx);
cond.wait(un, [this](){ return !isBusyValue;});
if (isTerminating)
throw TerminationRequestedException();

View File

@ -48,7 +48,7 @@ void AdventureMapShortcuts::setState(EAdventureState newState)
state = newState;
}
EAdventureState AdventureMapShortcuts::getState()
EAdventureState AdventureMapShortcuts::getState() const
{
return state;
}

View File

@ -94,6 +94,6 @@ public:
bool optionMapViewActive();
void setState(EAdventureState newState);
EAdventureState getState();
EAdventureState getState() const;
void onMapViewMoved(const Rect & visibleArea, int mapLevel);
};

View File

@ -72,7 +72,7 @@ void BattleConsole::showAll(Canvas & to)
to.drawText(line2, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, visibleText[1]);
}
std::vector<std::string> BattleConsole::getVisibleText()
std::vector<std::string> BattleConsole::getVisibleText() const
{
// high priority texts that hide battle log entries
for(const auto & text : {consoleText, hoverText})

View File

@ -72,7 +72,7 @@ private:
std::vector<std::string> splitText(const std::string &text);
/// select line(s) that will be visible in UI
std::vector<std::string> getVisibleText();
std::vector<std::string> getVisibleText() const;
public:
BattleConsole(const BattleInterface & owner, std::shared_ptr<CPicture> backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size);

View File

@ -773,7 +773,7 @@ void BattleWindow::bOpenActiveUnit()
const auto * unit = owner.stacksController->getActiveStack();
if (unit)
GH.windows().createAndPushWindow<CStackWindow>(unit, false);;
GH.windows().createAndPushWindow<CStackWindow>(unit, false);
}
void BattleWindow::bOpenHoveredUnit()

View File

@ -478,7 +478,7 @@ void TownSelector::updateListItems()
{
if(y >= line && (y - line) < 3)
{
auto getImageIndex = [](FactionID factionID, bool enabled){ return factionID.toFaction()->town->clientInfo.icons[true][!enabled] + 2; };
auto getImageIndex = [](FactionID targetFactionID, bool enabled){ return targetFactionID.toFaction()->town->clientInfo.icons[true][!enabled] + 2; };
towns[factionID] = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), getImageIndex(factionID, townsEnabled[factionID]), 0, x_offset + 48 * x, 32 * (y - line));
townsArea[factionID] = std::make_shared<LRClickableArea>(Rect(x_offset + 48 * x, 32 * (y - line), 48, 32), [this, getImageIndex, factionID](){
townsEnabled[factionID] = !townsEnabled[factionID];

View File

@ -698,7 +698,7 @@ void SelectionTab::selectNewestFile()
{
time_t newestTime = 0;
std::string newestFile = "";
for(int i = (int)allItems.size() - 1; i >= 0; i--)
for(int i = static_cast<int>(allItems.size()) - 1; i >= 0; i--)
if(allItems[i]->lastWrite > newestTime)
{
newestTime = allItems[i]->lastWrite;

View File

@ -112,10 +112,10 @@ void CHighScoreScreen::addButtons()
buttons.clear();
buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaignClick(); }, EShortcut::HIGH_SCORES_CAMPAIGNS));
buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonScenarioClick(); }, EShortcut::HIGH_SCORES_SCENARIOS));
buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }, EShortcut::HIGH_SCORES_RESET));
buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }, EShortcut::GLOBAL_RETURN));
buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [this](){ buttonCampaignClick(); }, EShortcut::HIGH_SCORES_CAMPAIGNS));
buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [this](){ buttonScenarioClick(); }, EShortcut::HIGH_SCORES_SCENARIOS));
buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [this](){ buttonResetClick(); }, EShortcut::HIGH_SCORES_RESET));
buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [this](){ buttonExitClick(); }, EShortcut::GLOBAL_RETURN));
}
void CHighScoreScreen::addHighScores()

View File

@ -117,7 +117,7 @@ void CTextInput::setFilterNumber(int minValue, int maxValue)
onTextFiltering = std::bind(&CTextInput::numberFilter, _1, _2, minValue, maxValue);
}
std::string CTextInput::getVisibleText()
std::string CTextInput::getVisibleText() const
{
return hasFocus() ? currentText + composedText + "_" : currentText;
}
@ -216,7 +216,6 @@ void CTextInput::textEdited(const std::string & enteredText)
composedText = enteredText;
updateLabel();
//onTextEdited(currentText + composedText);
}
void CTextInput::filenameFilter(std::string & text, const std::string &oldText)

View File

@ -49,7 +49,6 @@ class CTextInput final : public CFocusable
using TextEditedCallback = std::function<void(const std::string &)>;
using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
private:
std::string currentText;
std::string composedText;
ETextAlignment originalAlignment;
@ -66,7 +65,7 @@ private:
//min-max should be set via something like std::bind
static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
std::string getVisibleText();
std::string getVisibleText() const;
void createLabel(bool giveFocusToInput);
void updateLabel();

View File

@ -1039,11 +1039,11 @@ void CCastleBuildings::openTownHall()
void CCastleBuildings::enterAnyThievesGuild()
{
std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
for(auto & town : towns)
for(auto & ownedTown : towns)
{
if(town->builtBuildings.count(BuildingID::TAVERN))
if(ownedTown->builtBuildings.count(BuildingID::TAVERN))
{
LOCPLINT->showThievesGuildWindow(town);
LOCPLINT->showThievesGuildWindow(ownedTown);
return;
}
}

View File

@ -208,7 +208,7 @@ void CHeroWindow::update()
boost::algorithm::replace_first(helpBox, "%s", CGI->generaltexth->allTexts[43]);
garr = std::make_shared<CGarrisonInt>(Point(15, 485), 8, Point(), curHero);
auto split = std::make_shared<CButton>(Point(539, 519), AnimationPath::builtin("hsbtns9.def"), CButton::tooltip(CGI->generaltexth->allTexts[256], helpBox), [&](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT);
auto split = std::make_shared<CButton>(Point(539, 519), AnimationPath::builtin("hsbtns9.def"), CButton::tooltip(CGI->generaltexth->allTexts[256], helpBox), [this](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT);
garr->addSplitBtn(split);
}
if(!arts)

View File

@ -1010,10 +1010,10 @@ CGarrisonWindow::CGarrisonWindow(const CArmedInstance * up, const CGHeroInstance
garr = std::make_shared<CGarrisonInt>(Point(92, 127), 4, Point(0,96), up, down, removableUnits);
{
auto split = std::make_shared<CButton>(Point(88, 314), AnimationPath::builtin("IDV6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [&](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT );
auto split = std::make_shared<CButton>(Point(88, 314), AnimationPath::builtin("IDV6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [this](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT );
garr->addSplitBtn(split);
}
quit = std::make_shared<CButton>(Point(399, 314), AnimationPath::builtin("IOK6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
quit = std::make_shared<CButton>(Point(399, 314), AnimationPath::builtin("IOK6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT);
std::string titleText;
if(down->tempOwner == up->tempOwner)

View File

@ -253,7 +253,7 @@ bool CLegacyConfigParser::endLine()
void TextLocalizationContainer::registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
assert(!modContext.empty());
assert(!language.empty());
@ -269,7 +269,7 @@ void TextLocalizationContainer::registerStringOverride(const std::string & modCo
void TextLocalizationContainer::addSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
assert(!vstd::contains(subContainers, &container));
subContainers.push_back(&container);
@ -277,7 +277,7 @@ void TextLocalizationContainer::addSubContainer(const TextLocalizationContainer
void TextLocalizationContainer::removeSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
assert(vstd::contains(subContainers, &container));
@ -286,7 +286,7 @@ void TextLocalizationContainer::removeSubContainer(const TextLocalizationContain
const std::string & TextLocalizationContainer::deserialize(const TextIdentifier & identifier) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
if(stringsLocalizations.count(identifier.get()) == 0)
{
@ -307,7 +307,7 @@ const std::string & TextLocalizationContainer::deserialize(const TextIdentifier
void TextLocalizationContainer::registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
assert(!modContext.empty());
assert(!Languages::getLanguageOptions(language).identifier.empty());
@ -339,7 +339,7 @@ void TextLocalizationContainer::registerString(const std::string & modContext, c
bool TextLocalizationContainer::validateTranslation(const std::string & language, const std::string & modContext, const JsonNode & config) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
bool allPresent = true;
@ -398,14 +398,14 @@ void TextLocalizationContainer::loadTranslationOverrides(const std::string & lan
bool TextLocalizationContainer::identifierExists(const TextIdentifier & UID) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
return stringsLocalizations.count(UID.get());
}
void TextLocalizationContainer::exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
for (auto const & subContainer : subContainers)
subContainer->exportAllTexts(storage);
@ -436,7 +436,7 @@ std::string TextLocalizationContainer::getModLanguage(const std::string & modCon
void TextLocalizationContainer::jsonSerialize(JsonNode & dest) const
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
for(auto & s : stringsLocalizations)
{
@ -712,7 +712,7 @@ std::string CGeneralTextHandler::getInstalledEncoding()
std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(const std::string & prefix)
{
std::lock_guard<std::recursive_mutex> globalLock(globalTextMutex);
std::lock_guard globalLock(globalTextMutex);
std::vector<std::string> result;
for(const auto & entry : stringsLocalizations)

View File

@ -140,7 +140,7 @@ public:
{
struct Helper : public Serializeable
{
void serialize(Handler &h)
void serialize(Handler &h) const
{}
};
Helper helper;

View File

@ -200,7 +200,7 @@ std::shared_ptr<CSkill> CSkillHandler::loadFromJson(const std::string & scope, c
major = json["obligatoryMajor"].Bool();
minor = json["obligatoryMinor"].Bool();
auto skill = std::make_shared<CSkill>(SecondarySkill((si32)index), identifier, major, minor);
auto skill = std::make_shared<CSkill>(SecondarySkill(index), identifier, major, minor);
skill->modScope = scope;
skill->onlyOnWaterMap = json["onlyOnWaterMap"].Bool();

View File

@ -148,7 +148,7 @@ void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
bool messageQueueEmpty = dataToSend.empty();
dataToSend.push_back(headerVector);
if (message.size() > 0)
if (!message.empty())
dataToSend.push_back(message);
if (messageQueueEmpty)
@ -159,7 +159,7 @@ void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
{
boost::system::error_code ec;
boost::asio::write(*socket, boost::asio::buffer(headerVector), ec );
if (message.size() > 0)
if (!message.empty())
boost::asio::write(*socket, boost::asio::buffer(message), ec );
}
}

View File

@ -63,7 +63,7 @@ struct DLL_LINKAGE LobbyClientConnected : public CLobbyPackToPropagate
else
version = ESerializationVersion::RELEASE_150;
}
catch (const std::runtime_error & e)
catch (const std::runtime_error &)
{
version = ESerializationVersion::RELEASE_150;
}

View File

@ -181,7 +181,7 @@ std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, CRandomG
for (auto & point : points)
{
point = point + Point2D(0.5f, 0.5f);
};
}
// For 8XM8 map, only 650 out of 15971 points are in the range
@ -193,4 +193,4 @@ std::set<Point2D> PenroseTiling::generatePenroseTiling(size_t numZones, CRandomG
return finalPoints;
}
VCMI_LIB_NAMESPACE_END
VCMI_LIB_NAMESPACE_END

View File

@ -124,7 +124,7 @@ void Graphics::load()
void Graphics::loadHeroAnimations()
{
for(auto & elem : VLC->heroclassesh->objects)
for(const auto & elem : VLC->heroclassesh->objects)
{
for(auto templ : VLC->objtypeh->getHandlerFor(Obj::HERO, elem->getIndex())->getTemplates())
{

View File

@ -985,7 +985,7 @@ ui8 CVCMIServer::getIdOfFirstUnallocatedPlayer() const
void CVCMIServer::multiplayerWelcomeMessage()
{
int humanPlayer = 0;
for (auto & pi : si->playerInfos)
for (const auto & pi : si->playerInfos)
if(pi.second.isControlledByHuman())
humanPlayer++;
@ -996,9 +996,9 @@ void CVCMIServer::multiplayerWelcomeMessage()
std::vector<std::string> optionIds;
if(si->extraOptionsInfo.cheatsAllowed)
optionIds.push_back("vcmi.optionsTab.cheatAllowed.hover");
optionIds.emplace_back("vcmi.optionsTab.cheatAllowed.hover");
if(si->extraOptionsInfo.unlimitedReplay)
optionIds.push_back("vcmi.optionsTab.unlimitedReplay.hover");
optionIds.emplace_back("vcmi.optionsTab.unlimitedReplay.hover");
if(!optionIds.size()) // No settings to publish
return;