Suppress variable-set-but-not-used warnings from clang 15.
authorTom Lane <[email protected]>
Tue, 20 Sep 2022 16:04:37 +0000 (12:04 -0400)
committerTom Lane <[email protected]>
Tue, 20 Sep 2022 16:04:37 +0000 (12:04 -0400)
clang 15+ will issue a set-but-not-used warning when the only
use of a variable is in autoincrements (e.g., "foo++;").
That's perfectly sensible, but it detects a few more cases that
we'd not noticed before.  Silence the warnings with our usual
methods, such as PG_USED_FOR_ASSERTS_ONLY, or in one case by
actually removing a useless variable.

One thing that we can't nicely get rid of is that with %pure-parser,
Bison emits "yynerrs" as a local variable that falls foul of this
warning.  To silence those, I inserted "(void) yynerrs;" in the
top-level productions of affected grammars.

Per recently-established project policy, this is a candidate
for back-patching into out-of-support branches: it suppresses
annoying compiler warnings but changes no behavior.  Hence,
back-patch to 9.5, which is as far as these patches go without
issues.  (A preliminary check shows that the prior branches
need some other set-but-not-used cleanups too, so I'll leave
them for another day.)

Discussion: https://postgr.es/m/514615.1663615243@sss.pgh.pa.us

src/backend/access/gist/gistxlog.c
src/backend/access/transam/xlog.c
src/backend/parser/gram.y
src/backend/utils/adt/array_typanalyze.c
src/bin/pgbench/exprparse.y

index 5425b45a14376db3cbd471d944f2ed0fe0eecea5..002a3e0221bab179025e2d3c047bb2628cd9928b 100644 (file)
@@ -255,7 +255,7 @@ gistRedoPageUpdateRecord(XLogReaderState *record)
        char       *begin;
        char       *data;
        Size        datalen;
-       int         ninserted = 0;
+       int         ninserted PG_USED_FOR_ASSERTS_ONLY = 0;
 
        data = begin = XLogRecGetBlockData(record, 0, &datalen);
 
index b76451f168b03eaebd545ff1d24784ec078e925a..fc79bcbdba529b9c6ddd46224142df2a7784aa8f 100644 (file)
@@ -1923,7 +1923,7 @@ AdvanceXLInsertBuffer(XLogRecPtr upto, bool opportunistic)
    XLogRecPtr  NewPageEndPtr = InvalidXLogRecPtr;
    XLogRecPtr  NewPageBeginPtr;
    XLogPageHeader NewPage;
-   int         npages = 0;
+   int         npages pg_attribute_unused() = 0;
 
    LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
 
index 31ec7d196f2152bc4847134613da0008098cce5a..ab49a3d0aa1ac9227a9552fad31f71b0bd4c9e7e 100644 (file)
@@ -734,6 +734,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 stmtblock: stmtmulti
            {
                pg_yyget_extra(yyscanner)->parsetree = $1;
+               (void) yynerrs;     /* suppress compiler warning */
            }
        ;
 
index 6f89cf9f9fd10ca1e05ca5df57de9ad105ba81d8..52c6d244d47f072eba4cebe793d65e4378967b7d 100644 (file)
@@ -216,7 +216,6 @@ compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
 {
    ArrayAnalyzeExtraData *extra_data;
    int         num_mcelem;
-   int         null_cnt = 0;
    int         null_elem_cnt = 0;
    int         analyzed_rows = 0;
 
@@ -320,8 +319,7 @@ compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
        value = fetchfunc(stats, array_no, &isnull);
        if (isnull)
        {
-           /* array is null, just count that */
-           null_cnt++;
+           /* ignore arrays that are null overall */
            continue;
        }
 
index 0cc665b75b4bed6929faef62a8d70394d57b18da..633b37aefbe2100d094a3a54a7cf4968da49a326 100644 (file)
@@ -60,7 +60,10 @@ static PgBenchExpr *make_func(yyscan_t yyscanner, int fnumber, PgBenchExprList *
 
 %%
 
-result: expr               { expr_parse_result = $1; }
+result: expr               {
+                               expr_parse_result = $1;
+                               (void) yynerrs; /* suppress compiler warning */
+                           }
 
 elist:                     { $$ = NULL; }
    | expr                  { $$ = make_elist($1, NULL); }