refactor: PVS-Studio warnings fixes

This commit is contained in:
MihailRis 2024-08-04 01:30:52 +03:00
parent 245b39be62
commit 47db626145
14 changed files with 81 additions and 69 deletions

View File

@ -82,6 +82,10 @@ public:
return defs.size();
}
inline const auto& getIterable() const {
return defs;
}
inline const T* const* getDefs() const {
return defs.data();
}

View File

@ -37,8 +37,8 @@ public:
for (size_t i = 0; i < count; i++) {
indices.push_back(i);
}
for (size_t i = 0; i < unitIndices.count(); i++) {
names.push_back(unitIndices.get(i)->name); //FIXME: .get(i) potentional null pointer //-V522
for (auto unit : unitIndices.getIterable()) {
names.push_back(unit->name);
}
for (size_t i = unitIndices.count(); i < count; i++) {
names.emplace_back("");

View File

@ -103,9 +103,8 @@ template <class T>
static void write_indices(
const ContentUnitIndices<T>& indices, dynamic::List& list
) {
size_t count = indices.count();
for (size_t i = 0; i < count; i++) {
list.put(indices.get(i)->name); //FIXME: .get(i) potential null pointer //-V522
for (auto unit : indices.getIterable()) {
list.put(unit->name);
}
}

View File

@ -17,10 +17,11 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
auto atlas = assets->get<Atlas>("blocks");
for (uint i = 0; i < indices->blocks.count(); i++) {
auto def = indices->blocks.getWriteable(i); //FIXME: Potential null pointer
const auto& blocks = indices->blocks.getIterable();
for (uint i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side]; //-V522
const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) {

View File

@ -22,30 +22,30 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
Shader* shader,
Framebuffer* fbo,
Batch3D* batch,
const Block* def,
const Block& def,
int size
){
Window::clear();
blockid_t id = def->rt.id;
blockid_t id = def.rt.id;
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3),
cache->getRegion(id, 4), cache->getRegion(id, 5)};
glm::vec3 offset(0.1f, 0.5f, 0.1f);
switch (def->model) {
switch (def.model) {
case BlockModel::none:
// something went wrong...
break;
case BlockModel::block:
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
batch->blockCube(glm::vec3(size * 0.63f), texfaces,
glm::vec4(1.0f), !def->rt.emissive);
glm::vec4(1.0f), !def.rt.emissive);
batch->flush();
break;
case BlockModel::aabb:
{
glm::vec3 hitbox {};
for (const auto& box : def->hitboxes) {
for (const auto& box : def.hitboxes) {
hitbox = glm::max(hitbox, box.size());
}
offset = glm::vec3(1, 1, 0.0f);
@ -55,7 +55,7 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
-hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1),
hitbox * scaledSize,
texfaces, glm::vec4(1.0f),
!def->rt.emissive
!def.rt.emissive
);
}
batch->flush();
@ -64,32 +64,32 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
{
glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3();
for (const auto& box : def->modelBoxes) {
for (const auto& box : def.modelBoxes) {
hitbox = glm::max(hitbox, box.size());
}
offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
for (size_t i = 0; i < def->modelBoxes.size(); i++) {
for (size_t i = 0; i < def.modelBoxes.size(); i++) {
const UVRegion (&boxtexfaces)[6] = {
def->modelUVs[i * 6],
def->modelUVs[i * 6 + 1],
def->modelUVs[i * 6 + 2],
def->modelUVs[i * 6 + 3],
def->modelUVs[i * 6 + 4],
def->modelUVs[i * 6 + 5]
def.modelUVs[i * 6],
def.modelUVs[i * 6 + 1],
def.modelUVs[i * 6 + 2],
def.modelUVs[i * 6 + 3],
def.modelUVs[i * 6 + 4],
def.modelUVs[i * 6 + 5]
};
batch->cube(
def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def->modelBoxes[i].size() * pmul,
boxtexfaces, glm::vec4(1.0f), !def->rt.emissive
def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def.modelBoxes[i].size() * pmul,
boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
);
}
auto& points = def->modelExtraPoints;
auto& points = def.modelExtraPoints;
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
for (size_t i = 0; i < def->modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def->modelUVs[def->modelBoxes.size() * 6 + i];
for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
@ -154,9 +154,9 @@ std::unique_ptr<Atlas> BlocksPreview::build(
fbo.bind();
for (size_t i = 0; i < count; i++) {
auto def = indices->blocks.get(i); //FIXME: Potentional null pointer
auto& def = indices->blocks.require(i);
atlas->getTexture()->bind();
builder.add(def->name, draw(cache, shader, &fbo, &batch, def, iconSize)); //-V522
builder.add(def.name, draw(cache, shader, &fbo, &batch, def, iconSize));
}
fbo.unbind();

View File

@ -22,7 +22,7 @@ class BlocksPreview {
Shader* shader,
Framebuffer* framebuffer,
Batch3D* batch,
const Block* block,
const Block& block,
int size
);
public:

View File

@ -142,11 +142,12 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture()); //FIXME: Potentional null pointer
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture());
assert(cubemap != nullptr);
ready = true;
glActiveTexture(GL_TEXTURE1);
cubemap->bind(); //-V522
cubemap->bind();
shader->use();
const glm::vec3 xaxs[] = {

View File

@ -121,9 +121,9 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
itemid_t itemid = bound->getItemId();
if (itemid != prevItem) {
if (itemid) {
auto def = content->getIndices()->items.get(itemid); //FIXME: Potentional null pointer
auto& def = content->getIndices()->items.require(itemid);
tooltip = util::pascal_case(
langs::get(util::str2wstr_utf8(def->caption)) //-V522
langs::get(util::str2wstr_utf8(def.caption))
);
} else {
tooltip.clear();
@ -159,7 +159,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
auto previews = assets->get<Atlas>("block-previews");
auto indices = content->getIndices();
auto& item = indices->items.require(stack.getItemId()); //FIXME: Potentional null pointer
auto& item = indices->items.require(stack.getItemId());
switch (item.iconType) {
case item_icon_type::none:
break;
@ -268,14 +268,15 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
stack.setCount(halfremain);
}
} else {
auto stackDef = content->getIndices()->items.get(stack.getItemId()); //FIXME: Potentional null pointer
auto& stackDef =
content->getIndices()->items.require(stack.getItemId());
if (stack.isEmpty()) {
stack.set(grabbed);
stack.setCount(1);
grabbed.setCount(grabbed.getCount()-1);
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef->stackSize){ //-V522
stack.setCount(stack.getCount()+1);
grabbed.setCount(grabbed.getCount()-1);
grabbed.setCount(grabbed.getCount() - 1);
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) {
stack.setCount(stack.getCount() + 1);
grabbed.setCount(grabbed.getCount() - 1);
}
}
}

View File

@ -26,8 +26,8 @@ bool ItemStack::accepts(const ItemStack& other) const {
}
void ItemStack::move(ItemStack& item, const ContentIndices* indices) {
auto def = indices->items.get(item.getItemId()); //FIXME: Potentional null pointer
int count = std::min(item.count, def->stackSize - this->count); //-V522
auto& def = indices->items.require(item.getItemId());
int count = std::min(item.count, def.stackSize - this->count);
if (isEmpty()) {
set(ItemStack(item.getItemId(), count));
} else {

View File

@ -153,8 +153,8 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) {
auto indices = level->content->getIndices();
auto def = indices->blocks.get(chunk->voxels[vox_index(lx, y, lz)].id); //FIXME: Potentional null pointer
int invsize = def->inventorySize; //-V522
auto& def = indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def.inventorySize;
if (invsize == 0) {
return 0;
}

View File

@ -335,8 +335,8 @@ static int determine_rotation(
static void pick_block(
ContentIndices* indices, Chunks* chunks, Player* player, int x, int y, int z
) {
auto block = indices->blocks.get(chunks->get(x, y, z)->id); //FIXME: Potentional null pointer
itemid_t id = block->rt.pickingItem; //-V522
auto& block = indices->blocks.require(chunks->get(x, y, z)->id);
itemid_t id = block.rt.pickingItem;
auto inventory = player->getInventory();
size_t slotid = inventory->findSlotByItem(id, 0, 10);
if (slotid == Inventory::npos) {

View File

@ -119,11 +119,11 @@ static int l_get_x(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 1, 0, 0);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 1, 0, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
}
}
@ -136,11 +136,11 @@ static int l_get_y(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 1, 0);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 1, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
}
}
@ -153,11 +153,11 @@ static int l_get_z(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 0, 1);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 0, 1);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
}
}

View File

@ -53,34 +53,40 @@ static DocumentNode getDocumentNode(lua::State* L, int idx = 1) {
static int l_menu_back(lua::State* L) {
auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer
menu->back(); //-V522
if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->back();
}
return 0;
}
static int l_menu_reset(lua::State* L) {
auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer
menu->reset(); //-V522
if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->reset();
}
return 0;
}
static int l_textbox_paste(lua::State* L) {
auto node = getDocumentNode(L);
auto box = dynamic_cast<TextBox*>(node.node.get()); //FIXME: Potentional null pointer
if (auto box = dynamic_cast<TextBox*>(node.node.get())) {
auto text = lua::require_string(L, 2);
box->paste(util::str2wstr_utf8(text)); //-V522
box->paste(util::str2wstr_utf8(text));
}
return 0;
}
static int l_container_add(lua::State* L) {
auto docnode = getDocumentNode(L);
auto node = dynamic_cast<Container*>(docnode.node.get()); //FIXME: Potentional null pointer
auto node = dynamic_cast<Container*>(docnode.node.get());
if (node == nullptr) {
return 0;
}
auto xmlsrc = lua::require_string(L, 2);
try {
auto subnode =
guiutil::create(xmlsrc, docnode.document->getEnvironment());
node->add(subnode); //-V522
node->add(subnode);
UINode::getIndices(subnode, docnode.document->getMapWriteable());
} catch (const std::exception& err) {
throw std::runtime_error(err.what());

View File

@ -49,11 +49,11 @@ static int l_hud_open_block(lua::State* L) {
std::to_string(y) + " " + std::to_string(z)
);
}
auto def = content->getIndices()->blocks.get(vox->id);
auto& def = content->getIndices()->blocks.require(vox->id);
auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(def->uiLayout);//FIXME: Potentional null pointer //-V522
auto layout = assets->get<UiDocument>(def.uiLayout);
if (layout == nullptr) {
throw std::runtime_error("block '" + def->name + "' has no ui layout");
throw std::runtime_error("block '" + def.name + "' has no ui layout");
}
auto id = blocks->createBlockInventory(x, y, z);
@ -65,7 +65,7 @@ static int l_hud_open_block(lua::State* L) {
);
lua::pushinteger(L, id);
lua::pushstring(L, def->uiLayout);
lua::pushstring(L, def.uiLayout);
return 2;
}