From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E78EA3840C2D; Thu, 11 Feb 2021 15:25:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E78EA3840C2D From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/96311] [8/9/10/11 Regression] false positive for -Wunused-but-set-variable (const/constexpr identifier used in generic lambda) Date: Thu, 11 Feb 2021 15:25:04 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 8.5 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Feb 2021 15:25:05 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96311 --- Comment #4 from Jakub Jelinek --- If I compare void foo () { constexpr int used =3D 0; [](auto unused) { return used; }; } void bar () { constexpr int used =3D 0; [](int unused) { return used; }; } then in bar mark_exp_read is called on used from check_return_expr -> convert_for_initialization -> convert_from_assignment -> perform_implicit_conversion_flags -> mark_rvalue_use -> mark_use -> mark_exp_read, but for the generic lambda check_return_expr takes the goto dependent; and doesn't mark anything. It is only marked during instantiation of the lambda, such as when (0) as a= dded in between } and ; and in that case the warning is gone. More complete testcase: void foo () { constexpr int used =3D 0; [](auto unused) { return used; }; } void bar () { constexpr int used =3D 0; [](int unused) { return used; }; } void baz () { constexpr int used =3D 0; [](auto unused) { return used; } (0); } void qux () { constexpr int used =3D 0; [](auto unused) { return used + 1; }; } void corge () { constexpr int used =3D 0; [](auto unused) { return used + unused; }; } This shows that if the variable is used in some non-dependent expression as= in qux (and not directly in return statement), it is marked already without instantiation, but when it is only used directly in return stmt or used in a dependent expression, it is marked only during instantiation of the lambda. Though, when the lambda isn't really instantiated, the variable is not real= ly used, so not 100% sure the warning is unwanted.=