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 MihailRis
parent 3fa7fac4df
commit 26ff0f133a
4 changed files with 5 additions and 5 deletions

View File

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

View File

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

View File

@ -324,8 +324,6 @@ static int l_gui_getattr(lua::State* L) {
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
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 {
{"color", p_get_color},
@ -367,6 +365,8 @@ static int l_gui_getattr(lua::State* L) {
};
auto func = getters.find(attr);
if (func != getters.end()) {
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
return func->second(node.get(), L);
}
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) {
std::string name = block->name + ".broken";
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::pushivec3_stack(L, x, y, z);
lua::pushinteger(L, player ? player->getId() : -1);