In this testcase we were pulling out the constant value of the variable without ever calling mark_rvalue_use, which is needed for proper handling of lambda captures. This lack also lead to this false positive warning.
Tested x86_64-pc-linux-gnu, applying to trunk and 8.
commit e3359de1378a74fd049bcb3e737feefaa359a8f6 Author: Jason Merrill <ja...@redhat.com> Date: Mon Jun 11 16:54:24 2018 -0400 PR c++/85963 - -Wunused-but-set with ?: in template. * pt.c (tsubst_copy_and_build) [COND_EXPR]: Call mark_rvalue_use. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index d8880eb138d..ba78d2e34a5 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -18511,6 +18511,7 @@ tsubst_copy_and_build (tree t, case COND_EXPR: { tree cond = RECUR (TREE_OPERAND (t, 0)); + cond = mark_rvalue_use (cond); tree folded_cond = fold_non_dependent_expr (cond); tree exp1, exp2; diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-34.C b/gcc/testsuite/g++.dg/warn/Wunused-var-34.C new file mode 100644 index 00000000000..52c715121f5 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-34.C @@ -0,0 +1,27 @@ +// PR c++/85963 +// { dg-additional-options -Wall } + +template<typename T> +struct foo { + T val, alpha; + foo() : val(0), alpha(0) {} +}; + +template<typename T> +inline void bar(const foo<T>& A, const foo<T>& B, foo<T>& C) { + const bool use_alpha = true; + const T alpha = use_alpha ? (A.alpha * B.alpha) : T(0); + + C.val = A.val * B.val; + C.alpha = alpha; +} + + +int main() { + foo<double> A,B,C; + + bar(A,B,C); + + return 0; +} +