template < typename T > int f(T v){ if constexpr(sizeof(T) == sizeof(int)){ return v; }else{ return 0; } } int main(){ f(0); f('a'); } This is a valid program which should not produce a warning. (Normal 'if' doesn't produce a warning either.) $ g++ -std=c++1z -Wunused-but-set-parameter gcc-warning.cpp gcc-warning.cpp: In instantiation of 'int f(T) [with T = char]': gcc-warning.cpp:12:7: required from here gcc-warning.cpp:2:9: warning: parameter 'v' set but not used [-Wunused-but-set-parameter] int f(T v){ ^ $ g++ --version g++ (GCC) 7.1.1 20170724 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Confirmed.
Does still exist in: $ g++ --version g++ (GCC) 8.0.0 20171201 (experimental) Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Found this today, still present in gcc trunk (9.2+). Example: int main() { auto f = [](auto a, auto b) { if constexpr (sizeof(b) == 1) { return a; } else { return b; } }; return f(1, 1) + f(1, 'a'); } https://gcc.godbolt.org/z/SOvLEV
The problem seems to be that we're losing DECL_READ_P on the parm_decl v.
Well, patch posted: https://gcc.gnu.org/ml/gcc-patches/2019-08/msg01196.html I'm not very optimistic about it being accepted, but so far no one has submitted anything better.
Author: mpolacek Date: Wed Aug 28 02:22:29 2019 New Revision: 274982 URL: https://gcc.gnu.org/viewcvs?rev=274982&root=gcc&view=rev Log: PR c++/81676 - bogus -Wunused warnings in constexpr if. * semantics.c (maybe_mark_exp_read_r): New function. (finish_if_stmt): Call it on THEN_CLAUSE and ELSE_CLAUSE. * g++.dg/cpp1z/constexpr-if31.C: New test. * g++.dg/cpp1z/constexpr-if32.C: New test. Added: trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/semantics.c trunk/gcc/testsuite/ChangeLog
Fixed.
*** Bug 100956 has been marked as a duplicate of this bug. ***