https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827
Bug ID: 85827
Summary: false positive for -Wunused-but-set-variable because
of constexpr-if
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: kretz at kde dot org
Target Milestone: ---
Testcase `-std=c++17 -Wall` (cf. https://godbolt.org/g/kfgN2V):
template <int N> int f()
{
constexpr bool _1 = N == 1;
constexpr bool _2 = N == 2;
constexpr bool _3 = N == 3;
if constexpr (_1) {
return 5;
} else if constexpr (_2) {
return 1;
} else if constexpr (_3) {
return 7;
}
}
int a() {
return f<1>();
}
int b() {
return f<2>();
}
int c() {
return f<3>();
}
Yes, I see how one can argue that _2 and _3 are unused in f<1>. However, this
makes use of constexpr-if cumbersome. E.g. when a variable is required in all
but one branch, then it'll warn for specializations that take this one branch.
So you'll have to reorder the code such that the variable is only defined
inside the else branch where all the other branches are handled. But what if
you have two variables and their uses are disjunct? This leads to code
duplication, just for silencing the warning.
I believe, -Wunused-but-set-variable needs to consider all constexpr branches
to be useful here.