Further reduce warnings with -Wshadow=compatible-local
authorDavid Rowley <[email protected]>
Wed, 24 Aug 2022 00:27:12 +0000 (12:27 +1200)
committerDavid Rowley <[email protected]>
Wed, 24 Aug 2022 00:27:12 +0000 (12:27 +1200)
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
src/backend/access/brin/brin_minmax_multi.c
src/backend/access/gist/gist.c
src/backend/commands/copyfrom.c
src/backend/commands/indexcmds.c
src/backend/executor/nodeAgg.c
src/backend/optimizer/path/costsize.c
src/backend/statistics/mcv.c
src/backend/storage/buffer/bufmgr.c
src/bin/pg_dump/pg_dump.c
src/interfaces/ecpg/pgtypeslib/numeric.c

index e88f7efa7e488429875ae13089b440ad33013ac4..69f21abfb590c1a43fb38a97960397f572103f6b 100644 (file)
@@ -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;
index 10d4f17bc6ff37e7c796b9366045804f3ab7ea34..a581659fe2b4dc2c4cb8fc656a71a3c66b3ddf56 100644 (file)
@@ -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 */
        {
index 5866c6aaaf7cd33ad43a6b0b789ba206a6ab5de9..30069f139c7555b81282713fc9528141ab37af8c 100644 (file)
@@ -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;
 
index a976008b3d455b2dab70947d3c29b8bfd0847619..e8bb168aea89b81cd6626f751484e39d93a6f354 100644 (file)
@@ -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);
 
index 667f2a4cd169d5bc7b52f98161869d413675ac79..3c6e09815e0a9a118264474f9cbd49e07fd19b66 100644 (file)
@@ -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;
index 96d200e4461b380e11287e30bc95cfc0c2f55609..933c30490162d1b0d14d17e74bfd4aca0826b6a7 100644 (file)
@@ -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;
index 1e94c5aa7c49438989d544e52ea5d2e6f1df0604..75acea149c74677015d789d9aca656e246cbe2c6 100644 (file)
@@ -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;
index 5410a68bc91fd30165b8e4e6b344c8b8ae11ae88..6eeacb0d4766a29786c89662a79a293b84ffa89c 100644 (file)
@@ -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];
index 7a1202c6096e392901c9aa25e3d1094ae1b6634b..49d3b8c9dd0d615b8e048d21b539afeb3c91388a 100644 (file)
@@ -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]))
index 2c6891573296b395c5bd6b627d061de657355e9c..ca4ad070042deceb1c87566dd9a405fca854d2dc 100644 (file)
@@ -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];
index a97b3300cb815b81dea751755e7310c3b678db06..35e7b92da405ed12b51badb45056f43a665e906f 100644 (file)
@@ -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(&dividend);
-   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);