sema: fix cppcheck lint warnings

- autoVariables: heap-allocate cross-module source_dir copy instead of
  assigning pointer to local stack array
- identicalInnerCondition: simplify log2_int_ceil by removing redundant
  inner check (v > 0 always true when type_bits > 0)
- knownConditionTrueFalse: remove redundant !is_cross_module guard in
  else-if branch (always true since if-branch didn't execute)
- variableScope: narrow scope of di, field_name, ei variables
- unreadVariable: remove dead ei++ increment

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-23 07:47:08 +00:00
parent 21e0cd1500
commit 5757043dee

View File

@@ -2240,12 +2240,9 @@ static uint32_t findDeclInstByNameInZir(
uint32_t payload = zir->inst_datas[di_inst].declaration.payload_index;
uint32_t flags_1 = zir->extra[payload + 5];
uint32_t id = (flags_1 >> 27) & 0x1F;
uint32_t di = payload + 6;
uint32_t name_idx = 0;
if (declIdHasName(id)) {
name_idx = zir->extra[di];
di++;
}
if (declIdHasName(id))
name_idx = zir->extra[payload + 6];
if (name_idx == 0)
continue;
const char* name = (const char*)&zir->string_bytes[name_idx];
@@ -2256,6 +2253,7 @@ static uint32_t findDeclInstByNameInZir(
}
// findFuncInstInZir: find a func/func_fancy instruction by name in a ZIR.
// Scans the ZIR's struct_decl (inst 0) for a declaration matching the
// given name, then searches its value body for a func instruction.
// Uses the same parsing as zirStructDecl.
@@ -3103,12 +3101,12 @@ static AirInstRef zirCall(
} else if (base_import && nfields == 1) {
// Single-level: e.g. const panic = common.panic;
// fields[0] = "panic"
const char* field_name = fields[0];
Ast base_ast;
memset(&base_ast, 0, sizeof(base_ast));
Zir base_zir = loadStdImportZir(sema->module_root,
sema->source_dir, base_import, &base_ast);
if (base_zir.inst_len > 0) {
const char* field_name = fields[0];
func_inst = findFuncInstInZir(&base_zir, field_name);
if (func_inst != UINT32_MAX) {
saved_code = sema->code;
@@ -3364,9 +3362,8 @@ static AirInstRef zirCall(
// field_val(decl_val("std"), "math"), "F80")
// callee_name = "fromFloat"
// Handles: std.math.F80.fromFloat(...)
} else if (!is_cross_module
&& (lt == ZIR_INST_FIELD_VAL
|| lt == ZIR_INST_FIELD_PTR)) {
} else if (lt == ZIR_INST_FIELD_VAL
|| lt == ZIR_INST_FIELD_PTR) {
uint32_t lp
= sema->code.inst_datas[li].pl_node.payload_index;
uint32_t lhs2_ref = sema->code.extra[lp];
@@ -4027,13 +4024,10 @@ static AirInstRef zirCall(
if (type_bits > 0) {
// log2_int_ceil: smallest n such that 2^n >= bits.
uint32_t n = 0;
uint32_t v = type_bits;
if (v > 0) {
v--;
while (v > 0) {
n++;
v >>= 1;
}
uint32_t v = type_bits - 1;
while (v > 0) {
n++;
v >>= 1;
}
if (n == 0)
n = 1; // Log2Int minimum is u1
@@ -4619,7 +4613,11 @@ static AirInstRef zirCall(
populateDeclTableFromZir(sema, &import_zir);
if (import_source_dir[0]) {
saved_source_dir = sema->source_dir;
sema->source_dir = import_source_dir;
size_t len = strlen(import_source_dir) + 1;
char* dir_copy = (char*)malloc(len);
assert(dir_copy);
memcpy(dir_copy, import_source_dir, len);
sema->source_dir = dir_copy;
}
}
@@ -4635,8 +4633,10 @@ static AirInstRef zirCall(
memcpy(sema->decl_insts, saved_decl_insts,
saved_num_decls * sizeof(uint32_t));
sema->num_decls = saved_num_decls;
if (saved_source_dir)
if (saved_source_dir) {
free((char*)sema->source_dir);
sema->source_dir = saved_source_dir;
}
}
sema->fn_ret_ty = saved_fn_ret_ty;
@@ -6620,10 +6620,9 @@ static bool analyzeBodyInner(
uint16_t small = sema->code.inst_datas[inst].extended.small;
uint32_t payload_index
= sema->code.inst_datas[inst].extended.operand;
uint32_t ei = payload_index + 1; // skip src_node
if (small & 0x1) { // has_type
uint32_t ei = payload_index + 1; // skip src_node
ZirInstRef type_ref = sema->code.extra[ei];
ei++;
AirInstRef resolved = resolveInst(sema, type_ref);
TypeIndex elem_ty = IP_INDEX_VOID_TYPE;
if (AIR_REF_IS_IP(resolved))