From 264822a354d73c2de77f44f544b6e114c6d0d075 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 11 Dec 2018 19:57:27 +0100 Subject: [PATCH] reduce variables scope (cppcheck warnings). --- src/kernel/config.c | 2 +- src/kernel/unit.c | 3 +-- src/magic.c | 2 +- src/move.c | 6 +++--- src/report.c | 22 +++++++++------------- src/reports.c | 6 +++--- src/reports.test.c | 4 ++-- src/spells.c | 12 +++++------- 8 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 73b4aa22e..5c0bfc777 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -107,10 +107,10 @@ int findoption(const char *s, const struct locale *lang) /* -- Erschaffung neuer Einheiten ------------------------------ */ static int *forbidden_ids; +static const char *forbidden[] = { "t", "te", "tem", "temp", NULL }; bool forbiddenid(int id) { - static const char *forbidden[] = { "t", "te", "tem", "temp", NULL }; static size_t len; size_t i; if (id <= 0) { diff --git a/src/kernel/unit.c b/src/kernel/unit.c index bf2ad7a37..0778fc48e 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -982,9 +982,8 @@ void set_number(unit * u, int count) void remove_skill(unit * u, skill_t sk) { int i; - skill *sv; for (i = 0; i != u->skill_size; ++i) { - sv = u->skills + i; + skill *sv = u->skills + i; if (sv->id == sk) { if (u->skill_size - i - 1 > 0) { memmove(sv, sv + 1, (u->skill_size - i - 1) * sizeof(skill)); diff --git a/src/magic.c b/src/magic.c index 98d5dc05d..7ea51581e 100644 --- a/src/magic.c +++ b/src/magic.c @@ -2090,7 +2090,7 @@ castorder *create_castorder(castorder * co, unit *caster, unit * familiar, const co->sp = sp; co->level = lev; co->force = MagicPower(force); - co->_rtarget = r ? r : (familiar ? familiar->region : (caster ? caster->region : 0)); + co->_rtarget = r ? r : (familiar ? familiar->region : (caster ? caster->region : NULL)); co->distance = range; co->order = copy_order(ord); co->par = p; diff --git a/src/move.c b/src/move.c index fe39a5295..683908065 100644 --- a/src/move.c +++ b/src/move.c @@ -637,6 +637,7 @@ void move_ship(ship * sh, region * from, region * to, region_list * route) unit **iunit = &from->units; unit **ulist = &to->units; + assert(sh); if (from != to) { translist(&from->ships, &to->ships, sh); sh->region = to; @@ -1676,10 +1677,9 @@ static bool ship_ready(const region * r, unit * u, order * ord) unit *owner_buildingtyp(const region * r, const building_type * bt) { building *b; - unit *owner; for (b = rbuildings(r); b; b = b->next) { - owner = building_owner(b); + unit *owner = building_owner(b); if (b->type == bt && owner != NULL) { if (building_finished(b)) { return owner; @@ -1971,7 +1971,7 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) /* Hafengebühren ? */ harbourmaster = owner_buildingtyp(current_point, bt_find("harbour")); - if (sh && harbourmaster != NULL) { + if (harbourmaster != NULL) { item *itm; unit *u2; item *trans = NULL; diff --git a/src/report.c b/src/report.c index b96267b82..fd73f6369 100644 --- a/src/report.c +++ b/src/report.c @@ -432,7 +432,7 @@ void nr_spell_syntax(char *buf, size_t size, spellbook_entry * sbe, const struct void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *lang) { - int k, itemanz, costtyp; + int k; bool cont; char buf[4096]; const spell *sp = spellref_get(&sbe->spref); @@ -471,8 +471,8 @@ void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *la paragraph(out, LOC(lang, "nr_spell_components"), 0, 0, 0); for (k = 0; sp->components[k].type; ++k) { const resource_type *rtype = sp->components[k].type; - itemanz = sp->components[k].amount; - costtyp = sp->components[k].cost; + int itemanz = sp->components[k].amount; + int costtyp = sp->components[k].cost; if (itemanz > 0) { sbs_init(&sbs, buf, sizeof(buf)); if (sp->sptyp & SPELLLEVEL) { @@ -519,7 +519,6 @@ static void nr_curses_i(struct stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self) { for (; a; a = a->next) { - char buf[4096]; message *msg = 0; if (a->type == &at_curse) { @@ -536,6 +535,7 @@ nr_curses_i(struct stream *out, int indent, const faction *viewer, objtype_t typ } } if (msg) { + char buf[4096]; newline(out); nr_render(msg, viewer->locale, buf, sizeof(buf), viewer); paragraph(out, buf, indent, 2, 0); @@ -847,7 +847,6 @@ static void report_region_resource(sbstring *sbp, const struct locale *lang, con static void report_region_description(struct stream *out, const region * r, faction * f, const bool see[]) { int n; - bool dh; int trees; int saplings; attrib *a; @@ -986,6 +985,7 @@ static void report_region_description(struct stream *out, const region * r, fact } else { int d, nrd = 0; + bool dh = false; /* Nachbarregionen, die gesehen werden, ermitteln */ for (d = 0; d != MAXDIRECTIONS; d++) { @@ -994,7 +994,6 @@ static void report_region_description(struct stream *out, const region * r, fact } /* list directions */ - dh = false; for (d = 0; d != MAXDIRECTIONS; d++) { if (see[d]) { region *r2 = rconnect(r, d); @@ -1037,7 +1036,6 @@ static void report_region_description(struct stream *out, const region * r, fact sbs_strcat(&sbs, " (\""); sbs_strcat(&sbs, LOC(f->locale, spd->keyword)); sbs_strcat(&sbs, "\")."); - dh = 1; } } pump_paragraph(&sbs, out, REPORTWIDTH, true); @@ -1889,7 +1887,6 @@ static void cb_write_travelthru(region *r, unit *u, void *cbdata) { void report_travelthru(struct stream *out, region *r, const faction *f) { int maxtravel; - char buf[8192]; assert(r); assert(f); @@ -1901,6 +1898,7 @@ void report_travelthru(struct stream *out, region *r, const faction *f) maxtravel = count_travelthru(r, f); if (maxtravel > 0) { cb_data cbdata; + char buf[8192]; init_cb(&cbdata, out, buf, sizeof(buf), f); cbdata.maxtravel = maxtravel; @@ -1960,12 +1958,11 @@ report_plaintext(const char *filename, report_context * ctx, if (f->age <= 2) { const char *email; - const char *subject; email = config_get("game.email"); if (!email) log_error("game.email not set"); else { - subject = get_mailcmd(f->locale); + const char *subject = get_mailcmd(f->locale); m = msg_message("newbie_info_game", "email subject", email, subject); if (m) { @@ -2068,9 +2065,9 @@ report_plaintext(const char *filename, report_context * ctx, for (a = a_find(f->attribs, &at_showitem); a && a->type == &at_showitem; a = a->next) { const item_type *itype = (const item_type *)a->data.v; - const char *description = NULL; if (itype) { const char *pname = resourcename(itype->rtype, 0); + const char *description; if (ch == 0) { newline(out); @@ -2102,8 +2099,7 @@ report_plaintext(const char *filename, report_context * ctx, } centre(out, buf, true); newline(out); - description = mkname("describe", pname); - description = LOC(f->locale, description); + description = LOC(f->locale, mkname("describe", pname)); centre(out, description, true); } } diff --git a/src/reports.c b/src/reports.c index 72399b136..addc78dd7 100644 --- a/src/reports.c +++ b/src/reports.c @@ -319,11 +319,11 @@ report_items(const unit *u, item * result, int size, const unit * owner, } } for (itm = items; itm; itm = itm->next) { - item *ishow; const char *ic; report_item(owner, itm, viewer, NULL, &ic, NULL, false); if (ic && *ic) { + item *ishow; for (ishow = result; ishow != result + n; ++ishow) { const char *sc; @@ -1783,9 +1783,9 @@ static void var_free_regions(variant x) /*-V524 */ const char *trailinto(const region * r, const struct locale *lang) { - static char ref[32]; - const char *s; if (r) { + static char ref[32]; + const char *s; const char *tname = terrain_name(r); size_t sz; diff --git a/src/reports.test.c b/src/reports.test.c index 0ea5c0944..0f5f850e9 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -601,14 +601,14 @@ static void test_prepare_lighthouse_owners(CuTest *tc) f = test_create_faction(NULL); r1 = test_create_region(0, 0, t_plain); r2 = test_create_region(1, 0, t_ocean); - r3 = test_create_region(2, 0, t_ocean); + test_create_region(2, 0, t_ocean); r3 = test_create_region(3, 0, t_ocean); btype = test_create_buildingtype("lighthouse"); b = test_create_building(r1, btype); b->flags |= BLD_MAINTAINED; b->size = 10; update_lighthouse(b); - u = test_create_unit(f, r1); + test_create_unit(f, r1); u = test_create_unit(test_create_faction(NULL), r1); u->building = b; region_set_owner(b->region, f, 0); diff --git a/src/spells.c b/src/spells.c index b29e86ea9..a5c8383a1 100644 --- a/src/spells.c +++ b/src/spells.c @@ -2346,7 +2346,6 @@ static int sp_earthquake(castorder * co) static void patzer_peasantmob(const castorder * co) { unit *u; - attrib *a; region *r; unit *mage = co_get_magician(co); r = mage->region->land ? mage->region : co_get_region(co); @@ -2355,6 +2354,7 @@ static void patzer_peasantmob(const castorder * co) faction *f = get_monsters(); const struct locale *lang = f->locale; message *msg; + attrib *a; int anteil, n; anteil = 6 + rng_int() % 4; @@ -2410,7 +2410,6 @@ static void patzer_peasantmob(const castorder * co) static int sp_forest_fire(castorder * co) { unit *u; - region *nr; direction_t i; region *r = co_get_region(co); unit *caster = co_get_caster(co); @@ -2442,7 +2441,7 @@ static int sp_forest_fire(castorder * co) msg_release(msg); for (i = 0; i < MAXDIRECTIONS; i++) { - nr = rconnect(r, i); + region *nr = rconnect(r, i); assert(nr); destroyed = 0; vernichtet_schoesslinge = 0; @@ -5629,7 +5628,6 @@ int sp_disruptastral(castorder * co) attrib *a; double effect; region *r2 = rl2->data; - spec_direction *sd; int inhab_regions = 0; region_list *trl = NULL; @@ -5649,7 +5647,7 @@ int sp_disruptastral(castorder * co) while (a != NULL && a->type == &at_direction) { attrib *a2 = a->next; - sd = (spec_direction *)(a->data.v); + spec_direction *sd = (spec_direction *)(a->data.v); if (sd->duration != -1) a_remove(&r->attribs, a); a = a2; @@ -6161,7 +6159,6 @@ int sp_speed2(castorder * co) */ int sp_break_curse(castorder * co) { - attrib **ap; int obj; curse *c; region *r = co_get_region(co); @@ -6169,7 +6166,6 @@ int sp_break_curse(castorder * co) int cast_level = co->level; double force = co->force; spellparameter *pa = co->par; - const char *ts = NULL; if (pa->length < 2) { /* Das Zielobjekt wurde vergessen */ @@ -6186,6 +6182,8 @@ int sp_break_curse(castorder * co) "unit region command", mage, mage->region, co->order)); } else { + const char *ts = NULL; + attrib **ap; switch (obj) { case SPP_REGION: ap = &r->attribs;