From 421892a192b8f95ab96c5edb61d424f80a4221d0 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Wed, 24 Aug 2022 12:27:12 +1200 Subject: [PATCH] Further reduce warnings with -Wshadow=compatible-local In a similar effort to f01592f91, here we're targetting fixing the warnings that -Wshadow=compatible-local produces that we can fix by moving a variable to an inner scope to stop that variable from being shadowed by another variable declared somewhere later in the function. All of the warnings being fixed here are changing the scope of variables which are being used as an iterator for a "for" loop. In each instance, the fix happens to be changing the for loop to use the C99 type initialization. Much of this code likely pre-dates our use of C99. Reducing the scope of the outer scoped variable seems like the safest way to fix these. Renaming seems more likely to risk patches using the wrong variable. Reducing the scope is more likely to result in a compilation failure after applying some future patch rather than introducing bugs with it. By my count, this takes the warning count from 129 down to 114. Author: Justin Pryzby Discussion: https://postgr.es/m/CAApHDvrwLGBP%2BYw9vriayyf%3DXR4uPWP5jr6cQhP9au_kaDUhbA%40mail.gmail.com --- src/backend/access/brin/brin.c | 3 +-- src/backend/access/brin/brin_minmax_multi.c | 3 +-- src/backend/access/gist/gist.c | 3 +-- src/backend/commands/copyfrom.c | 3 +-- src/backend/commands/indexcmds.c | 7 +++---- src/backend/executor/nodeAgg.c | 3 +-- src/backend/optimizer/path/costsize.c | 8 ++++---- src/backend/statistics/mcv.c | 11 +++++------ src/backend/storage/buffer/bufmgr.c | 5 ++--- src/bin/pg_dump/pg_dump.c | 3 +-- src/interfaces/ecpg/pgtypeslib/numeric.c | 5 ++--- 11 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index e88f7efa7e..69f21abfb5 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -372,7 +372,6 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm) **nullkeys; int *nkeys, *nnullkeys; - int keyno; char *ptr; Size len; char *tmp PG_USED_FOR_ASSERTS_ONLY; @@ -454,7 +453,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm) memset(nnullkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts); /* Preprocess the scan keys - split them into per-attribute arrays. */ - for (keyno = 0; keyno < scan->numberOfKeys; keyno++) + for (int keyno = 0; keyno < scan->numberOfKeys; keyno++) { ScanKey key = &scan->keyData[keyno]; AttrNumber keyattno = key->sk_attno; diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c index 10d4f17bc6..a581659fe2 100644 --- a/src/backend/access/brin/brin_minmax_multi.c +++ b/src/backend/access/brin/brin_minmax_multi.c @@ -582,7 +582,6 @@ brin_range_serialize(Ranges *range) int typlen; bool typbyval; - int i; char *ptr; /* simple sanity checks */ @@ -662,7 +661,7 @@ brin_range_serialize(Ranges *range) */ ptr = serialized->data; /* start of the serialized data */ - for (i = 0; i < nvalues; i++) + for (int i = 0; i < nvalues; i++) { if (typbyval) /* simple by-value data types */ { diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 5866c6aaaf..30069f139c 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -234,7 +234,6 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, Page page = BufferGetPage(buffer); bool is_leaf = (GistPageIsLeaf(page)) ? true : false; XLogRecPtr recptr; - int i; bool is_split; /* @@ -420,7 +419,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, { char *data = (char *) (ptr->list); - for (i = 0; i < ptr->block.num; i++) + for (int i = 0; i < ptr->block.num; i++) { IndexTuple thistup = (IndexTuple) data; diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index a976008b3d..e8bb168aea 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -1202,7 +1202,6 @@ BeginCopyFrom(ParseState *pstate, num_defaults; FmgrInfo *in_functions; Oid *typioparams; - int attnum; Oid in_func_oid; int *defmap; ExprState **defexprs; @@ -1401,7 +1400,7 @@ BeginCopyFrom(ParseState *pstate, defmap = (int *) palloc(num_phys_attrs * sizeof(int)); defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *)); - for (attnum = 1; attnum <= num_phys_attrs; attnum++) + for (int attnum = 1; attnum <= num_phys_attrs; attnum++) { Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1); diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 667f2a4cd1..3c6e09815e 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -565,7 +565,6 @@ DefineIndex(Oid relationId, Oid root_save_userid; int root_save_sec_context; int root_save_nestlevel; - int i; root_save_nestlevel = NewGUCNestLevel(); @@ -1047,7 +1046,7 @@ DefineIndex(Oid relationId, * We disallow indexes on system columns. They would not necessarily get * updated correctly, and they don't seem useful anyway. */ - for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) + for (int i = 0; i < indexInfo->ii_NumIndexAttrs; i++) { AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i]; @@ -1067,7 +1066,7 @@ DefineIndex(Oid relationId, pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs); pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs); - for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) + for (int i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) { if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, indexattrs)) @@ -1243,7 +1242,7 @@ DefineIndex(Oid relationId, * If none matches, build a new index by calling ourselves * recursively with the same options (except for the index name). */ - for (i = 0; i < nparts; i++) + for (int i = 0; i < nparts; i++) { Oid childRelid = part_oids[i]; Relation childrel; diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 96d200e446..933c304901 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -1296,13 +1296,12 @@ finalize_aggregates(AggState *aggstate, Datum *aggvalues = econtext->ecxt_aggvalues; bool *aggnulls = econtext->ecxt_aggnulls; int aggno; - int transno; /* * If there were any DISTINCT and/or ORDER BY aggregates, sort their * inputs and run the transition functions. */ - for (transno = 0; transno < aggstate->numtrans; transno++) + for (int transno = 0; transno < aggstate->numtrans; transno++) { AggStatePerTrans pertrans = &aggstate->pertrans[transno]; AggStatePerGroup pergroupstate; diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 1e94c5aa7c..75acea149c 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -2447,7 +2447,6 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) int arrlen; ListCell *l; ListCell *cell; - int i; int path_index; int min_index; int max_index; @@ -2486,7 +2485,6 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) for_each_cell(l, subpaths, cell) { Path *subpath = (Path *) lfirst(l); - int i; /* Consider only the non-partial paths */ if (path_index++ == numpaths) @@ -2495,7 +2493,8 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) costarr[min_index] += subpath->total_cost; /* Update the new min cost array index */ - for (min_index = i = 0; i < arrlen; i++) + min_index = 0; + for (int i = 0; i < arrlen; i++) { if (costarr[i] < costarr[min_index]) min_index = i; @@ -2503,7 +2502,8 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) } /* Return the highest cost from the array */ - for (max_index = i = 0; i < arrlen; i++) + max_index = 0; + for (int i = 0; i < arrlen; i++) { if (costarr[i] > costarr[max_index]) max_index = i; diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c index 5410a68bc9..6eeacb0d47 100644 --- a/src/backend/statistics/mcv.c +++ b/src/backend/statistics/mcv.c @@ -1604,7 +1604,6 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, Bitmapset *keys, List *exprs, MCVList *mcvlist, bool is_or) { - int i; ListCell *l; bool *matches; @@ -1659,7 +1658,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match = true; MCVItem *item = &mcvlist->items[i]; @@ -1766,7 +1765,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { int j; bool match = !expr->useOr; @@ -1837,7 +1836,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match = false; /* assume mismatch */ MCVItem *item = &mcvlist->items[i]; @@ -1930,7 +1929,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { MCVItem *item = &mcvlist->items[i]; bool match = false; @@ -1956,7 +1955,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match; MCVItem *item = &mcvlist->items[i]; diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 7a1202c609..49d3b8c9dd 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -3183,7 +3183,6 @@ void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) { int i; - int j; int n = 0; SMgrRelation *rels; BlockNumber (*block)[MAX_FORKNUM + 1]; @@ -3232,7 +3231,7 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) */ for (i = 0; i < n && cached; i++) { - for (j = 0; j <= MAX_FORKNUM; j++) + for (int j = 0; j <= MAX_FORKNUM; j++) { /* Get the number of blocks for a relation's fork. */ block[i][j] = smgrnblocks_cached(rels[i], j); @@ -3259,7 +3258,7 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) { for (i = 0; i < n; i++) { - for (j = 0; j <= MAX_FORKNUM; j++) + for (int j = 0; j <= MAX_FORKNUM; j++) { /* ignore relation forks that doesn't exist */ if (!BlockNumberIsValid(block[i][j])) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 2c68915732..ca4ad07004 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -11576,7 +11576,6 @@ dumpFunc(Archive *fout, const FuncInfo *finfo) char **configitems = NULL; int nconfigitems = 0; const char *keyword; - int i; /* Do nothing in data-only dump */ if (dopt->dataOnly) @@ -11853,7 +11852,7 @@ dumpFunc(Archive *fout, const FuncInfo *finfo) finfo->dobj.name); } - for (i = 0; i < nconfigitems; i++) + for (int i = 0; i < nconfigitems; i++) { /* we feel free to scribble on configitems[] here */ char *configitem = configitems[i]; diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index a97b3300cb..35e7b92da4 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -1062,7 +1062,6 @@ PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) int weight_tmp; int rscale_tmp; int ri; - int i; long guess; long first_have; long first_div; @@ -1109,7 +1108,7 @@ PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) * Initialize local variables */ init_var(÷nd); - for (i = 1; i < 10; i++) + for (int i = 1; i < 10; i++) init_var(&divisor[i]); /* @@ -1268,7 +1267,7 @@ done: if (dividend.buf != NULL) digitbuf_free(dividend.buf); - for (i = 1; i < 10; i++) + for (int i = 1; i < 10; i++) { if (divisor[i].buf != NULL) digitbuf_free(divisor[i].buf);