diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 8535ecc2d93..11dee7d8753 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -10215,6 +10215,9 @@ check_return_expr (tree retval, bool *no_warning) dependent: /* We should not have changed the return value. */ gcc_assert (retval == saved_retval); + /* We don't know if this is an lvalue or rvalue use, but + either way we can mark it as read. */ + mark_exp_read (retval); return retval; } diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-Wunused.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-Wunused.C new file mode 100644 index 00000000000..b43cbe6b675 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-Wunused.C @@ -0,0 +1,18 @@ +// PR c++/96311 +// { dg-do compile { target c++14 } } +// { dg-additional-options -Wunused } + +auto foo() +{ + constexpr int used = 0; + return + [](auto unused) + { + return used; + }; +} + +int main() +{ + foo()(42); +}