From patchwork Wed Oct 19 16:38:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 5742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp423169wrs; Wed, 19 Oct 2022 09:39:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM58XmRRPlVw+yhF0oO6HQtZvdp2X2Cw3bNgFW5SlhdHeTLGq5u+SQ07Dye60kNr7GzXaWZ0 X-Received: by 2002:a05:6402:1205:b0:458:c1b2:e428 with SMTP id c5-20020a056402120500b00458c1b2e428mr8267819edw.94.1666197547769; Wed, 19 Oct 2022 09:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666197547; cv=none; d=google.com; s=arc-20160816; b=Q0xVYu0w7ftJ9oN8HsnmcHmRN/+ughs9j89oXMNVS3GS1pybAR36QD8144H7F6xOjT B33NSH5pYagwnPfUffg/JLI4S722Q4yJW7wQNAlU/I74DtQxWNVLqcD48RHnxZu/K5Ai mPKUuzvTIZrKyqTn/Z9rrGCyOAz8zFMhbOGwnc0oItnr5n9cPyHyFTnjfQGzBzwRYJTh t69fUnMc166rURhzszWef5ZkoF3U1XMt+Ev1HBeU2S5JRcTcPwJSuUvL0ee6gy81uOfS snda3Xp0LLjKucO+eGNTnhS24QVWrWZJMe3+2rJqXd1hza9U/Re87H9BhX9840Xv8ih4 7Tdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=ofYcpwo655UG5ODBrUdxZo/XUnGPAEpQ9am0Tf/fZ6U=; b=JU61/FZal8qrcNPR03dah+LSxqciFKTDyNC9GWTwrj6jBhl3taTJeZNNpKGwE/2G9X 6WF0zLVprlSCA6aeF+/9NE96bAA6rCXgzDp1JNk0CUqyzMl9VOYDBejWp3M39MPeYf8B RXYyffBLDwtXwXDbftNiMB78jMfe4J8d5v+0f2i7jPHUjj6imXmDVYv4QBW7rx4GvCdq SqSmNenlaYTHJSdFhF3/3FUqR9MkJuuiJ7BbNbU4qobVp7kOIUfvRzblMdNjeKBB9m7B oKmGFtyigrrImi3udfmZ5QSsIJGe+tubKwWMWbUm9GINceT2ZyPxOVb1CqRRGjYcj0Pa Qo6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OJD1PPxe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z11-20020a05640240cb00b0045807f73ad5si16335426edb.570.2022.10.19.09.39.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 09:39:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OJD1PPxe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BFA243858415 for ; Wed, 19 Oct 2022 16:39:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFA243858415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666197546; bh=ofYcpwo655UG5ODBrUdxZo/XUnGPAEpQ9am0Tf/fZ6U=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=OJD1PPxeNCrxh4wd2seGVrBNtkR3X7BvFXxKL29Xstx1CPQKhD4eZfBVI0zzq1kBV LU2LgIa7PI2yZCowUC/gWO6EzNqN97gEKm6KDExeL2gtMh4BRspwVbtWVgWHiSnGDI xAREZsEguYVNVz5e+8/IaZopuX6Xs3Fiw1vWa0AQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 132823858D3C for ; Wed, 19 Oct 2022 16:38:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 132823858D3C Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-232-YOBbX9ZQMCu0IhV82Q9sBw-1; Wed, 19 Oct 2022 12:38:12 -0400 X-MC-Unique: YOBbX9ZQMCu0IhV82Q9sBw-1 Received: by mail-qk1-f199.google.com with SMTP id bm16-20020a05620a199000b006ee95743ad2so15324045qkb.14 for ; Wed, 19 Oct 2022 09:38:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ofYcpwo655UG5ODBrUdxZo/XUnGPAEpQ9am0Tf/fZ6U=; b=iGtFgcsTILZbTmcUobXqgClKCSLLX4Lcr8r/Xh0ux3kRqECBNiBPKyC/NiVH8fm4ho qX1yfTrMCczhY1bxafhvQxi5OqYmdGYr0lU/WcZE4HjSY37iiPMe+hcRIwdXQNmIUpbs hA18mjzDSsqR5TRn1yzh05H7HydqJX+LNbycnnSIceQLEoRHGk+eq8x3Yn37i/rqMGUN dfE8R4e+i8SCj8ONazdODIQhNmlzyaXOOt9FnMlAYhRwqpfA+FTmLFrtoEpCW9aWVFis yHWECRwRLBz5CoMQgHDA8XEj+atW8HFXWgob5HbgGAv+4gmJ2LygGkKTaOobgRA2WOLQ o3dg== X-Gm-Message-State: ACrzQf1wUGiA2cgfnEPk4XHr6M9dKFYjePPKePTH5L26tmNP7MRQipti F4HrHk6lXyvScVMjqeTtPfcwUnUjqMet2cBO+5DazVRp0FA4S1DdhmUUgaSt2xXCEWJ/k4CLtTD JQo3o9Rilp3y2zzd2LQ== X-Received: by 2002:a05:6214:c6f:b0:4b2:b0e2:2122 with SMTP id t15-20020a0562140c6f00b004b2b0e22122mr7355593qvj.55.1666197491801; Wed, 19 Oct 2022 09:38:11 -0700 (PDT) X-Received: by 2002:a05:6214:c6f:b0:4b2:b0e2:2122 with SMTP id t15-20020a0562140c6f00b004b2b0e22122mr7355567qvj.55.1666197491472; Wed, 19 Oct 2022 09:38:11 -0700 (PDT) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id w29-20020a05620a0e9d00b006ee7e223bb8sm5173341qkm.39.2022.10.19.09.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 09:38:10 -0700 (PDT) Date: Wed, 19 Oct 2022 12:38:08 -0400 To: Jason Merrill Subject: [PATCH v2] c++: Mitigate -Wuseless-cast with classes [PR85043] Message-ID: References: <20221018173829.392773-1-polacek@redhat.com> <84d8cddc-79f5-3920-fd34-4b2b28010363@redhat.com> MIME-Version: 1.0 In-Reply-To: <84d8cddc-79f5-3920-fd34-4b2b28010363@redhat.com> User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747047969259548962?= X-GMAIL-MSGID: =?utf-8?q?1747134759904568728?= On Wed, Oct 19, 2022 at 09:27:27AM -0400, Jason Merrill wrote: > On 10/18/22 13:38, Marek Polacek wrote: > > -Wuseless-cast (not part of -Wall/-Wextra) warns here: > > > > struct S { }; > > void g (S&&); > > void f (S&& arg) > > { > > g (S(arg)); // warning: useless cast to type 'struct S' > > } > > > > which is wrong: the code will not compile without the cast because > > "arg" is an lvalue which cannot bind to S&&. > > > > I'd like to disable the warning when a class object is cast to > > a non-reference type, which seems like a minimal patch that fixes the > > problems reported in our Bugzilla. Of course, the cast in "(int)i" > > may not be useless, either, but I'm not changing that here. > > As I commented on PR 14710, "I wouldn't warn about a cast that changes the > type or value category of an expression at all...." Ah, I didn't see that comment. > The code currently checks the value category when casting to a reference, > but not when casting to a non-reference; that seems like the thing to fix. OK, how about this? The nice thing is that it handles non-classes as well, while we still warn about truly useless casts as in "X(X{})". Thanks, Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- -Wuseless-cast (not part of -Wall/-Wextra) warns here: struct S { }; void g (S&&); void f (S&& arg) { g (S(arg)); // warning: useless cast to type 'struct S' } which is wrong: the code will not compile without the cast because "arg" is an lvalue which cannot bind to S&&. This patch disables the warning when an object that isn't a prvalue is cast to a non-reference type. Therefore we still warn about the useless cast in "X(X{})". PR c++/85043 gcc/cp/ChangeLog: * typeck.cc (maybe_warn_about_useless_cast): Don't warn when a glvalue is cast to a non-reference type. gcc/ChangeLog: * doc/invoke.texi: Update documentation of -Wuseless-cast. gcc/testsuite/ChangeLog: * g++.dg/warn/Wuseless-cast.C: Remove dg-warning. * g++.dg/warn/Wuseless-cast3.C: New test. --- gcc/cp/typeck.cc | 4 ++- gcc/doc/invoke.texi | 13 +++++++++- gcc/testsuite/g++.dg/warn/Wuseless-cast.C | 12 ++++----- gcc/testsuite/g++.dg/warn/Wuseless-cast3.C | 29 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wuseless-cast3.C base-commit: 69a233610f6b27cd4283561569d8ce0f35044dc4 diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index da0e1427b97..8e1d14ee6b0 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8108,7 +8108,9 @@ maybe_warn_about_useless_cast (location_t loc, tree type, tree expr, && (TYPE_REF_IS_RVALUE (type) ? xvalue_p (expr) : lvalue_p (expr)) && same_type_p (TREE_TYPE (expr), TREE_TYPE (type))) - || same_type_p (TREE_TYPE (expr), type)) + /* Don't warn when converting a class object to a non-reference type, + because that's a common way to create a temporary. */ + || (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type))) warning_at (loc, OPT_Wuseless_cast, "useless cast to type %q#T", type); } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index c176e2dc646..cd4d3c1d72c 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -4551,7 +4551,18 @@ pointers after reallocation. @item -Wuseless-cast @r{(C++ and Objective-C++ only)} @opindex Wuseless-cast @opindex Wno-useless-cast -Warn when an expression is casted to its own type. +Warn when an expression is cast to its own type. This warning does not +occur when a class object is converted to a non-reference type as that +is a way to create a temporary: + +@smallexample +struct S @{ @}; +void g (S&&); +void f (S&& arg) +@{ + g (S(arg)); // make arg prvalue so that it can bind to S&& +@} +@end smallexample @item -Wno-conversion-null @r{(C++ and Objective-C++ only)} @opindex Wconversion-null diff --git a/gcc/testsuite/g++.dg/warn/Wuseless-cast.C b/gcc/testsuite/g++.dg/warn/Wuseless-cast.C index 2fd6bc45102..d7cb89930a6 100644 --- a/gcc/testsuite/g++.dg/warn/Wuseless-cast.C +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast.C @@ -62,11 +62,11 @@ A prvalue(); void f() { - int n; + int n; - (int)(n); // { dg-warning "3:useless cast" } - static_cast(n); // { dg-warning "3:useless cast" } - reinterpret_cast(n); // { dg-warning "3:useless cast" } + (int)(n); + static_cast(n); + reinterpret_cast(n); (int*)(&n); // { dg-warning "3:useless cast" } const_cast(&n); // { dg-warning "3:useless cast" } @@ -100,8 +100,8 @@ void f() A a; - (A)(a); // { dg-warning "3:useless cast" } - static_cast(a); // { dg-warning "3:useless cast" } + (A)(a); + static_cast(a); (A*)(&a); // { dg-warning "3:useless cast" } const_cast(&a); // { dg-warning "3:useless cast" } diff --git a/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C b/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C new file mode 100644 index 00000000000..43dd2ebfaea --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C @@ -0,0 +1,29 @@ +// PR c++/85043 +// { dg-do compile { target c++11 } } +// { dg-options "-Wuseless-cast" } + +struct S { int s; void bump () { s++; } }; + +void +foo () +{ + S s = { 1 }; + s.bump (); + S (s).bump (); // { dg-bogus "useless" } + ((S) s).bump (); // { dg-bogus "useless" } + static_cast (s).bump (); // { dg-bogus "useless" } +} + +struct X { }; +void g(X&&); + +void +f (X&& arg) +{ + g(X(arg)); // { dg-bogus "useless" } + g(X(X{})); // { dg-warning "useless" } + g(static_cast(arg)); + + int i = (int) 1; // { dg-warning "useless" } + const int &r = (int) i; // { dg-bogus "useless" } +}