Suppress variable-set-but-not-used warnings from clang 15.
authorTom Lane
Tue, 20 Sep 2022 16:04:37 +0000 (12:04 -0400)
committerTom Lane
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/backend/utils/adt/jsonpath_gram.y
src/bin/pgbench/exprparse.y

index b60dba052fa865ffb4d2983ec69a01af9d660d44..07803d6f17c39a1b0d7e9336b3cfd8cd39da38a7 100644 (file)
@@ -81,7 +81,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 a580efea61c3040da51d5fccb518a4e454b835c9..a05a82119e558484c6d7ad3eb2263f43dd3b9df9 100644 (file)
@@ -2140,7 +2140,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 74339aa4db871146e79e1043227ae464d21849d7..62829186919314c70273870950b5f60de4acc294 100644 (file)
@@ -804,6 +804,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 stmtblock: stmtmulti
            {
                pg_yyget_extra(yyscanner)->parsetree = $1;
+               (void) yynerrs;     /* suppress compiler warning */
            }
        ;
 
index 4912cabc6176a8b00ded9150bb5c82f661aef80c..4cfafca782a5e149ec625b03e25fe4997abff0de 100644 (file)
@@ -218,7 +218,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;
 
@@ -322,8 +321,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 6cbf441b9368ac7800f8d2dfa4ec9215c14795af..287d497477ce36c3415eb1ace4dd28c08b97eb0a 100644 (file)
@@ -130,6 +130,7 @@ result:
                                        *result = palloc(sizeof(JsonPathParseResult));
                                        (*result)->expr = $2;
                                        (*result)->lax = $1;
+                                       (void) yynerrs;
                                    }
    | /* EMPTY */                   { *result = NULL; }
    ;
index 85d61caa9f10d19e38cf6402d1165c09d2917b1e..b4ef7bfc260316a7deae711e12a1857bd1954ce9 100644 (file)
@@ -79,7 +79,10 @@ static PgBenchExpr *make_case(yyscan_t yyscanner, PgBenchExprList *when_then_lis
 
 %%
 
-result: expr               { expr_parse_result = $1; }
+result: expr               {
+                               expr_parse_result = $1;
+                               (void) yynerrs; /* suppress compiler warning */
+                           }
 
 elist:                     { $$ = NULL; }
    | expr                  { $$ = make_elist($1, NULL); }