cppcheck: variable scopes, etc.

This commit is contained in:
Enno Rehling 2018-02-11 15:57:31 +01:00
parent f67a4943e4
commit 398a258658
11 changed files with 39 additions and 41 deletions

View file

@ -333,7 +333,7 @@ static int do_recruiting(recruitment * recruits, int available)
for (req = rec->requests; req; req = req->next) { for (req = rec->requests; req; req = req->next) {
unit *u = req->unit; unit *u = req->unit;
const race *rc = u_race(u); /* race is set in recruit() */ const race *rc = u_race(u); /* race is set in recruit() */
int number, dec; int number;
double multi = 2.0 * rc->recruit_multi; double multi = 2.0 * rc->recruit_multi;
number = (int)(get / multi); number = (int)(get / multi);
@ -359,7 +359,7 @@ static int do_recruiting(recruitment * recruits, int available)
} }
add_recruits(u, number, req->qty); add_recruits(u, number, req->qty);
if (number > 0) { if (number > 0) {
dec = (int)(number * multi); int dec = (int)(number * multi);
if ((rc->ec_flags & ECF_REC_ETHEREAL) == 0) { if ((rc->ec_flags & ECF_REC_ETHEREAL) == 0) {
recruited += dec; recruited += dec;
} }
@ -1006,7 +1006,7 @@ static void allocate_resource(unit * u, const resource_type * rtype, int want)
} }
} }
assert(sk != NOSKILL || "limited resource needs a required skill for making it"); assert(sk != NOSKILL || !"limited resource needs a required skill for making it");
skill = effskill(u, sk, 0); skill = effskill(u, sk, 0);
if (skill == 0) { if (skill == 0) {
add_message(&u->faction->msgs, add_message(&u->faction->msgs,
@ -1075,13 +1075,12 @@ leveled_allocation(const resource_type * rtype, region * r, allocation * alist)
{ {
const item_type *itype = resource2item(rtype); const item_type *itype = resource2item(rtype);
rawmaterial *rm = rm_get(r, rtype); rawmaterial *rm = rm_get(r, rtype);
int need;
bool first = true; bool first = true;
if (rm != NULL) { if (rm != NULL) {
int need;
do { do {
int avail = rm->amount; int avail = rm->amount, nreq = 0;
int nreq = 0;
allocation *al; allocation *al;
if (avail <= 0) { if (avail <= 0) {
@ -1093,7 +1092,7 @@ leveled_allocation(const resource_type * rtype, region * r, allocation * alist)
assert(avail > 0); assert(avail > 0);
for (al = alist; al; al = al->next) for (al = alist; al; al = al->next) {
if (!fval(al, AFL_DONE)) { if (!fval(al, AFL_DONE)) {
int req = required(al->want - al->get, al->save); int req = required(al->want - al->get, al->save);
assert(al->get <= al->want && al->get >= 0); assert(al->get <= al->want && al->get >= 0);
@ -1112,6 +1111,7 @@ leveled_allocation(const resource_type * rtype, region * r, allocation * alist)
fset(al, AFL_LOWSKILL); fset(al, AFL_LOWSKILL);
} }
} }
}
need = nreq; need = nreq;
if (avail > nreq) avail = nreq; if (avail > nreq) avail = nreq;

View file

@ -158,18 +158,15 @@ static bool limited_give(const item_type * type)
int give_quota(const unit * src, const unit * dst, const item_type * type, int give_quota(const unit * src, const unit * dst, const item_type * type,
int n) int n)
{ {
double divisor;
if (!limited_give(type)) { if (!limited_give(type)) {
return n; return n;
} }
if (dst && src && src->faction != dst->faction) { if (dst && src && src->faction != dst->faction) {
divisor = config_get_flt("rules.items.give_divisor", 1); int divisor = config_get_int("rules.items.give_divisor", 1);
assert(divisor <= 0 || divisor >= 1); assert(divisor <= 0 || divisor >= 1);
if (divisor >= 1) { if (divisor >= 1) {
/* predictable > correct: */ /* predictable > correct: */
int x = (int)(n / divisor); return n / divisor;
return x;
} }
} }
return n; return n;
@ -306,7 +303,6 @@ static bool rule_transfermen(void)
message * give_men(int n, unit * u, unit * u2, struct order *ord) message * give_men(int n, unit * u, unit * u2, struct order *ord)
{ {
ship *sh; ship *sh;
int k = 0;
int error = 0; int error = 0;
message * msg; message * msg;
int maxt = max_transfers(); int maxt = max_transfers();
@ -391,7 +387,7 @@ message * give_men(int n, unit * u, unit * u2, struct order *ord)
} }
if (has_skill(u, SK_ALCHEMY) || has_skill(u2, SK_ALCHEMY)) { if (has_skill(u, SK_ALCHEMY) || has_skill(u2, SK_ALCHEMY)) {
k = count_skill(u2->faction, SK_ALCHEMY); int k = count_skill(u2->faction, SK_ALCHEMY);
/* Falls die Zieleinheit keine Alchemisten sind, werden sie nun /* Falls die Zieleinheit keine Alchemisten sind, werden sie nun
* welche. */ * welche. */

View file

@ -137,10 +137,10 @@ int umvwaddnstr(WINDOW *w, int y, int x, const char * str, int len) {
static void init_curses(void) static void init_curses(void)
{ {
int fg, bg;
initscr(); initscr();
if (has_colors() || force_color) { if (has_colors() || force_color) {
int fg, bg;
short bcol = COLOR_BLACK; short bcol = COLOR_BLACK;
short hcol = COLOR_MAGENTA; short hcol = COLOR_MAGENTA;
start_color(); start_color();
@ -316,11 +316,11 @@ static void paint_map(window * wnd, const state * st)
int yp = (lines - vy - 1) * THEIGHT; int yp = (lines - vy - 1) * THEIGHT;
for (vx = 0; vx != cols; ++vx) { for (vx = 0; vx != cols; ++vx) {
map_region *mr = mr_get(&st->display, vx, vy); map_region *mr = mr_get(&st->display, vx, vy);
int attr = 0;
int hl = 0;
int xp = vx * TWIDTH + (vy & 1) * TWIDTH / 2; int xp = vx * TWIDTH + (vy & 1) * TWIDTH / 2;
int nx, ny; int nx, ny;
if (mr) { if (mr) {
int attr = 0;
int hl = 0;
cnormalize(&mr->coord, &nx, &ny); cnormalize(&mr->coord, &nx, &ny);
if (tagged_region(st->selected, nx, ny)) { if (tagged_region(st->selected, nx, ny)) {
attr |= A_REVERSE; attr |= A_REVERSE;
@ -336,9 +336,9 @@ static void paint_map(window * wnd, const state * st)
map_region *cursor_region(const view * v, const coordinate * c) map_region *cursor_region(const view * v, const coordinate * c)
{ {
coordinate relpos; coordinate relpos;
int cx, cy;
if (c) { if (c) {
int cx, cy;
relpos.x = c->x - v->topleft.x; relpos.x = c->x - v->topleft.x;
relpos.y = c->y - v->topleft.y; relpos.y = c->y - v->topleft.y;
cy = relpos.y; cy = relpos.y;
@ -426,13 +426,14 @@ static void paint_info_region(window * wnd, const state * st)
{ {
WINDOW *win = wnd->handle; WINDOW *win = wnd->handle;
int size = getmaxx(win) - 2; int size = getmaxx(win) - 2;
int line = 0, maxline = getmaxy(win) - 2; int maxline = getmaxy(win) - 2;
map_region *mr = cursor_region(&st->display, &st->cursor); map_region *mr = cursor_region(&st->display, &st->cursor);
UNUSED_ARG(st); UNUSED_ARG(st);
werase(win); werase(win);
wxborder(win); wxborder(win);
if (mr && mr->r) { if (mr && mr->r) {
int line = 0;
const region *r = mr->r; const region *r = mr->r;
if (r->land) { if (r->land) {
umvwaddnstr(win, line++, 1, (char *)r->land->name, size); umvwaddnstr(win, line++, 1, (char *)r->land->name, size);
@ -708,10 +709,10 @@ static void select_regions(state * st, int selectmode)
doupdate(); doupdate();
findmode = getch(); findmode = getch();
if (findmode == 'n') { /* none */ if (findmode == 'n') { /* none */
int i;
sprintf(sbuffer, "%snone", status); sprintf(sbuffer, "%snone", status);
statusline(st->wnd_status->handle, sbuffer); statusline(st->wnd_status->handle, sbuffer);
if (selectmode & MODE_SELECT) { if (selectmode & MODE_SELECT) {
int i;
for (i = 0; i != MAXTHASH; ++i) { for (i = 0; i != MAXTHASH; ++i) {
tag **tp = &st->selected->tags[i]; tag **tp = &st->selected->tags[i];
while (*tp) { while (*tp) {

View file

@ -124,7 +124,7 @@ static void json_maintenance_i(cJSON *json, maintenance *mt) {
static void json_maintenance(cJSON *json, maintenance **mtp) { static void json_maintenance(cJSON *json, maintenance **mtp) {
cJSON *child; cJSON *child;
maintenance *mt; maintenance *mt;
int i, size = 1; int size = 1;
if (json->type == cJSON_Array) { if (json->type == cJSON_Array) {
size = cJSON_GetArraySize(json); size = cJSON_GetArraySize(json);
@ -135,6 +135,7 @@ static void json_maintenance(cJSON *json, maintenance **mtp) {
} }
*mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1); *mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1);
if (json->type == cJSON_Array) { if (json->type == cJSON_Array) {
int i;
for (i = 0, child = json->child; child; child = child->next, ++i) { for (i = 0, child = json->child; child; child = child->next, ++i) {
if (child->type == cJSON_Object) { if (child->type == cJSON_Object) {
json_maintenance_i(child, mt + i); json_maintenance_i(child, mt + i);

View file

@ -2720,10 +2720,10 @@ static void age_stonecircle(building *b) {
curse *c = get_curse(rt->attribs, &ct_astralblock); curse *c = get_curse(rt->attribs, &ct_astralblock);
if (!c) { if (!c) {
int sk = effskill(mage, SK_MAGIC, 0); int sk = effskill(mage, SK_MAGIC, 0);
float effect = 100;
if (sk > 0) { if (sk > 0) {
int vig = sk; int vig = sk;
int dur = (sk + 1) / 2; int dur = (sk + 1) / 2;
float effect = 100;
/* the mage reactivates the circle */ /* the mage reactivates the circle */
c = create_curse(mage, &rt->attribs, &ct_astralblock, c = create_curse(mage, &rt->attribs, &ct_astralblock,
vig, dur, effect, 0); vig, dur, effect, 0);

View file

@ -499,13 +499,13 @@ void unset_combatspell(unit * u, spell * sp)
{ {
sc_mage *m; sc_mage *m;
int nr = 0; int nr = 0;
int i;
m = get_mage_depr(u); m = get_mage_depr(u);
if (!m) if (!m)
return; return;
if (!sp) { if (!sp) {
int i;
for (i = 0; i < MAXCOMBATSPELLS; i++) { for (i = 0; i < MAXCOMBATSPELLS; i++) {
m->combatspells[i].sp = NULL; m->combatspells[i].sp = NULL;
} }
@ -1589,13 +1589,13 @@ verify_targets(castorder * co, int *invalid, int *resist, int *success)
const spell *sp = co->sp; const spell *sp = co->sp;
region *target_r = co_get_region(co); region *target_r = co_get_region(co);
spellparameter *sa = co->par; spellparameter *sa = co->par;
int i;
*invalid = 0; *invalid = 0;
*resist = 0; *resist = 0;
*success = 0; *success = 0;
if (sa && sa->length) { if (sa && sa->length) {
int i;
/* zuerst versuchen wir vorher nicht gefundene Objekte zu finden. /* zuerst versuchen wir vorher nicht gefundene Objekte zu finden.
* Wurde ein Objekt durch globalsuche gefunden, obwohl der Zauber * Wurde ein Objekt durch globalsuche gefunden, obwohl der Zauber
* gar nicht global hätte suchen dürften, setzen wir das Objekt * gar nicht global hätte suchen dürften, setzen wir das Objekt

View file

@ -284,7 +284,7 @@ static int ridingcapacity(const unit * u)
int walkingcapacity(const struct unit *u) int walkingcapacity(const struct unit *u)
{ {
int n, people, pferde_fuer_wagen, horses; int n, people, pferde_fuer_wagen, horses;
int wagen_ohne_pferde, wagen_mit_pferden, wagen_mit_trollen; int wagen_mit_pferden;
int vehicles = 0, vcap = 0; int vehicles = 0, vcap = 0;
int animals = 0, acap = 0; int animals = 0, acap = 0;
const struct resource_type *rhorse = rt_find("horse"); const struct resource_type *rhorse = rt_find("horse");
@ -312,6 +312,7 @@ int walkingcapacity(const struct unit *u)
n = wagen_mit_pferden * vcap; n = wagen_mit_pferden * vcap;
if (u_race(u) == get_race(RC_TROLL)) { if (u_race(u) == get_race(RC_TROLL)) {
int wagen_ohne_pferde, wagen_mit_trollen;
/* 4 Trolle ziehen einen Wagen. */ /* 4 Trolle ziehen einen Wagen. */
/* Unbesetzte Wagen feststellen */ /* Unbesetzte Wagen feststellen */
wagen_ohne_pferde = vehicles - wagen_mit_pferden; wagen_ohne_pferde = vehicles - wagen_mit_pferden;
@ -682,7 +683,6 @@ static bool is_freezing(const unit * u)
int check_ship_allowed(struct ship *sh, const region * r) int check_ship_allowed(struct ship *sh, const region * r)
{ {
int c = 0;
const building_type *bt_harbour = bt_find("harbour"); const building_type *bt_harbour = bt_find("harbour");
if (sh->region && r_insectstalled(r)) { if (sh->region && r_insectstalled(r)) {
@ -708,6 +708,7 @@ int check_ship_allowed(struct ship *sh, const region * r)
return SA_COAST; return SA_COAST;
} }
if (sh->type->coasts) { if (sh->type->coasts) {
int c;
for (c = 0; sh->type->coasts[c] != NULL; ++c) { for (c = 0; sh->type->coasts[c] != NULL; ++c) {
if (sh->type->coasts[c] == r->terrain) { if (sh->type->coasts[c] == r->terrain) {
return SA_COAST; return SA_COAST;

View file

@ -350,7 +350,6 @@ static void dracoid_name(unit * u)
{ {
static char name[NAMESIZE + 1]; static char name[NAMESIZE + 1];
int mid_syllabels; int mid_syllabels;
size_t sz;
/* ignore u */ /* ignore u */
UNUSED_ARG(u); UNUSED_ARG(u);
@ -358,14 +357,14 @@ static void dracoid_name(unit * u)
mid_syllabels = rng_int() % 4; mid_syllabels = rng_int() % 4;
sz = str_strlcpy(name, drac_pre[rng_int() % DRAC_PRE], sizeof(name)); str_strlcpy(name, drac_pre[rng_int() % DRAC_PRE], sizeof(name));
while (mid_syllabels > 0) { while (mid_syllabels > 0) {
mid_syllabels--; mid_syllabels--;
if (rng_int() % 10 < 4) if (rng_int() % 10 < 4)
str_strlcat(name, "'", sizeof(name)); str_strlcat(name, "'", sizeof(name));
sz += str_strlcat(name, drac_mid[rng_int() % DRAC_MID], sizeof(name)); str_strlcat(name, drac_mid[rng_int() % DRAC_MID], sizeof(name));
} }
sz += str_strlcat(name, drac_suf[rng_int() % DRAC_SUF], sizeof(name)); str_strlcat(name, drac_suf[rng_int() % DRAC_SUF], sizeof(name));
unit_setname(u, name); unit_setname(u, name);
} }
@ -392,13 +391,13 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
while (*p != 0) { while (*p != 0) {
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
/* Leerzeichen <20>berspringen */ /* Leerzeichen <20>berspringen */
while (*p != 0 && !iswalnum((wint_t)ucs)) { while (*p != 0 && !iswalnum((wint_t)ucs)) {
p += size; p += size;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
} }
/* Counter erh<72>hen */ /* Counter erh<72>hen */
@ -409,7 +408,7 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
while (*p != 0 && iswalnum((wint_t)ucs)) { while (*p != 0 && iswalnum((wint_t)ucs)) {
p += size; p += size;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
} }
} }
@ -423,7 +422,7 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
bufp = buf; bufp = buf;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
while (*p != 0 && c < maxchars) { while (*p != 0 && c < maxchars) {
/* Leerzeichen <20>berspringen */ /* Leerzeichen <20>berspringen */
@ -431,7 +430,7 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
while (*p != 0 && !iswalnum((wint_t)ucs)) { while (*p != 0 && !iswalnum((wint_t)ucs)) {
p += size; p += size;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
} }
/* alnums <20>bertragen */ /* alnums <20>bertragen */
@ -443,7 +442,7 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
++c; ++c;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
} }
/* Bis zum n<>chsten Leerzeichen */ /* Bis zum n<>chsten Leerzeichen */
@ -451,7 +450,7 @@ const char *abkz(const char *s, char *buf, size_t buflen, size_t maxchars)
while (c < maxchars && *p != 0 && iswalnum((wint_t)ucs)) { while (c < maxchars && *p != 0 && iswalnum((wint_t)ucs)) {
p += size; p += size;
result = unicode_utf8_to_ucs4(&ucs, p, &size); result = unicode_utf8_to_ucs4(&ucs, p, &size);
assert(result == 0 || "damnit, we're not handling invalid input here!"); assert(result == 0 || !"damnit, we're not handling invalid input here!");
} }
} }

View file

@ -122,7 +122,6 @@ void piracy_cmd(unit * u)
const faction *target; const faction *target;
int value; int value;
} aff[MAXDIRECTIONS]; } aff[MAXDIRECTIONS];
int saff = 0;
int *il; int *il;
assert(u->thisorder); assert(u->thisorder);
@ -139,6 +138,7 @@ void piracy_cmd(unit * u)
/* Wenn nicht, sehen wir, ob wir ein Ziel finden. */ /* Wenn nicht, sehen wir, ob wir ein Ziel finden. */
if (target_dir == NODIRECTION) { if (target_dir == NODIRECTION) {
int saff = 0;
direction_t dir; direction_t dir;
/* Einheit ist also Kapitän. Jetzt gucken, in wievielen /* Einheit ist also Kapitän. Jetzt gucken, in wievielen
* Nachbarregionen potentielle Opfer sind. */ * Nachbarregionen potentielle Opfer sind. */

View file

@ -37,8 +37,8 @@ int add_raceprefix(const char *prefix)
} }
void free_prefixes(void) { void free_prefixes(void) {
int i;
if (race_prefixes) { if (race_prefixes) {
int i;
for (i = 0; race_prefixes[i]; ++i) { for (i = 0; race_prefixes[i]; ++i) {
free(race_prefixes[i]); free(race_prefixes[i]);
} }

View file

@ -537,7 +537,6 @@ static int sp_summon_familiar(castorder * co)
unit *mage = co->magician.u; unit *mage = co->magician.u;
int cast_level = co->level; int cast_level = co->level;
const race *rc; const race *rc;
int dh;
message *msg; message *msg;
char zText[2048]; char zText[2048];
@ -553,7 +552,8 @@ static int sp_summon_familiar(castorder * co)
if (fval(rc, RCF_SWIM) && !fval(rc, RCF_WALK)) { if (fval(rc, RCF_SWIM) && !fval(rc, RCF_WALK)) {
int coasts = is_coastregion(r); int coasts = is_coastregion(r);
int dir; int dir, dh;
if (coasts == 0) { if (coasts == 0) {
cmistake(mage, co->order, 229, MSG_MAGIC); cmistake(mage, co->order, 229, MSG_MAGIC);
return 0; return 0;