[Bug c/108395] New: [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable

pexu@gcc-bugzilla.mail.kapsi.fi gcc-bugzilla@gcc.gnu.org
Fri Jan 13 14:58:38 GMT 2023


            Bug ID: 108395
           Summary: [C2x] Bogus -Wunused-but-set-variable when returning
                    constexpr variable
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pexu@gcc-bugzilla.mail.kapsi.fi
  Target Milestone: ---


It appears if constexpr variable, declared at block scope, is used verbatim as
a return value GCC emits a bogus -Wunused-but-set-variable.

Using version `13.0.0 20230113 (experimental)'.

# echo 'int f() { constexpr int v = 0; return v; }' | gcc -c -xc -std=c2x -Wall
-Wextra -
<stdin>: In function 'f':
<stdin>:1:25: warning: variable 'v' set but not used

As a workaround any operation on v (cast, effectively-no-op arithmetics etc.)
or maybe_unused attribute (or file scope) can be used.

More information about the Gcc-bugs mailing list