fix: optimization: PVS-Studio warning V821

Creating variables in a more localized scope can enhance performance and make the code easier to understand.

Reported by: PVS-Studio
Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov 2024-08-02 02:43:52 +03:00 committed by Pugemon
parent e9a5f18bc3
commit 6b3146f98a
No known key found for this signature in database
GPG Key ID: 472FA343B3CC3287
4 changed files with 5 additions and 5 deletions

View File

@ -406,9 +406,9 @@ void Hud::add(const HudElement& element) {
using namespace dynamic; using namespace dynamic;
gui->add(element.getNode()); gui->add(element.getNode());
auto invview = std::dynamic_pointer_cast<InventoryView>(element.getNode());
auto document = element.getDocument(); auto document = element.getDocument();
if (document) { if (document) {
auto invview = std::dynamic_pointer_cast<InventoryView>(element.getNode());
auto inventory = invview ? invview->getInventory() : nullptr; auto inventory = invview ? invview->getInventory() : nullptr;
std::vector<Value> args; std::vector<Value> args;
args.emplace_back(inventory ? inventory.get()->getId() : 0); args.emplace_back(inventory ? inventory.get()->getId() : 0);

View File

@ -243,13 +243,13 @@ void WorldRenderer::renderBlockSelection() {
void WorldRenderer::renderLines( void WorldRenderer::renderLines(
Camera* camera, Shader* linesShader, const DrawContext& pctx Camera* camera, Shader* linesShader, const DrawContext& pctx
) { ) {
auto ctx = pctx.sub(lineBatch.get());
linesShader->use(); linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
if (player->selection.vox.id != BLOCK_VOID) { if (player->selection.vox.id != BLOCK_VOID) {
renderBlockSelection(); renderBlockSelection();
} }
if (player->debug && showEntitiesDebug) { if (player->debug && showEntitiesDebug) {
auto ctx = pctx.sub(lineBatch.get());
level->entities->renderDebug(*lineBatch, *frustumCulling, ctx); level->entities->renderDebug(*lineBatch, *frustumCulling, ctx);
} }
} }

View File

@ -324,8 +324,6 @@ static int l_gui_getattr(lua::State* L) {
auto docname = lua::require_string(L, 1); auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2); auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3); auto attr = lua::require_string(L, 3);
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
static const std::unordered_map<std::string_view, std::function<int(UINode*,lua::State*)>> getters { static const std::unordered_map<std::string_view, std::function<int(UINode*,lua::State*)>> getters {
{"color", p_get_color}, {"color", p_get_color},
@ -367,6 +365,8 @@ static int l_gui_getattr(lua::State* L) {
}; };
auto func = getters.find(attr); auto func = getters.find(attr);
if (func != getters.end()) { if (func != getters.end()) {
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
return func->second(node.get(), L); return func->second(node.get(), L);
} }
return 0; return 0;

View File

@ -240,8 +240,8 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y
} }
void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) { void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) {
std::string name = block->name + ".broken";
if (block->rt.funcsset.onbroken) { if (block->rt.funcsset.onbroken) {
std::string name = block->name + ".broken";
lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) { lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) {
lua::pushivec3_stack(L, x, y, z); lua::pushivec3_stack(L, x, y, z);
lua::pushinteger(L, player ? player->getId() : -1); lua::pushinteger(L, player ? player->getId() : -1);