From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 64EBE3858D39 for ; Wed, 19 Oct 2022 13:27:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 64EBE3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666186052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=twXUwVgFoYkN2sx1D/WNBLtfDMhKEXrwaOpLJl3+O8Q=; b=c6OQAJ4rPPWXreu/9/86e+uV+hITZhE7aDqLJKAwuyulloF0V9XszqtU+IvhjvqWMPwohw QvQHJNWyIMWNZAoF327/Ee1Kc4reCUXR2kvPnwas8zmDvAP9fRbfLhKLAP0jhAOYJtnGlG 1/OmB8Hrj9XGgX4lR0dh+xb5U5exY98= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-456-2HvTo0QwPT28NjSMcvkHLQ-1; Wed, 19 Oct 2022 09:27:30 -0400 X-MC-Unique: 2HvTo0QwPT28NjSMcvkHLQ-1 Received: by mail-qv1-f71.google.com with SMTP id dn14-20020a056214094e00b004b1a231394eso10684317qvb.13 for ; Wed, 19 Oct 2022 06:27:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=twXUwVgFoYkN2sx1D/WNBLtfDMhKEXrwaOpLJl3+O8Q=; b=rTSARhsD9a1N11bugbseYp73wMTcL07nucgK4hAH3KvrkR4p+6vj1hyX6itQCEhRr6 idXN2M0FtAjEHdPzlbWUo9ZOJF5mvk0h74iaZVRQUV75hlTdCcMP3KtHgzD+nV0tSax6 SHjHDU6nMIDSwlNQUILjs3jMThAzSCKkoEkE7WZq0NTJlBXAvHYUCepMMe0R8t6GhI0J cqYsLd8ro1u5+NsZJOqoUaXoWM+RVO/RCqaUamku32OBCj66ZZh7QB67sritI9gtenl0 13z5rgVVQOszh2T2RJRKB0NSqM8tVEnZNyuWowr9QZl0QLybzfcrCii20y+5lC6vqUiV DDsw== X-Gm-Message-State: ACrzQf1vtL6M6u3VbeJhk6eW6OJIjIvjLbWIVdbsJQoUQjlIv5/lO8ta BFHwiHzYNxm+kxTc1MohQSBWW4Xjw7+il7ATqNbqMi1DpBkLDHvJKYe04My7HyzgGfBCiUb7/yV uN+F2qDhec4AEF0ElUQ== X-Received: by 2002:ac8:5f53:0:b0:39c:e315:114d with SMTP id y19-20020ac85f53000000b0039ce315114dmr6543548qta.346.1666186050244; Wed, 19 Oct 2022 06:27:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7OC6le9BiGecAqbxB05mj0xLuj+KxL9LGaeP0Ck3eoLX9xVs282thavr2rSw+IW8bF+bvGeA== X-Received: by 2002:ac8:5f53:0:b0:39c:e315:114d with SMTP id y19-20020ac85f53000000b0039ce315114dmr6543530qta.346.1666186049927; Wed, 19 Oct 2022 06:27:29 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id i8-20020a05620a248800b006bb0f9b89cfsm5111362qkn.87.2022.10.19.06.27.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Oct 2022 06:27:29 -0700 (PDT) Message-ID: <84d8cddc-79f5-3920-fd34-4b2b28010363@redhat.com> Date: Wed, 19 Oct 2022 09:27:27 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.2 Subject: Re: [PATCH] c++: Mitigate -Wuseless-cast with classes [PR85043] To: Marek Polacek , GCC Patches References: <20221018173829.392773-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20221018173829.392773-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: 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...." 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. > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/85043 > > gcc/cp/ChangeLog: > > * typeck.cc (maybe_warn_about_useless_cast): Don't warn when a class > object 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 | 5 +++++ > gcc/doc/invoke.texi | 13 ++++++++++- > gcc/testsuite/g++.dg/warn/Wuseless-cast.C | 4 ++-- > gcc/testsuite/g++.dg/warn/Wuseless-cast3.C | 25 ++++++++++++++++++++++ > 4 files changed, 44 insertions(+), 3 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wuseless-cast3.C > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index da0e1427b97..a7587f56720 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -8104,6 +8104,11 @@ maybe_warn_about_useless_cast (location_t loc, tree type, tree expr, > if (warn_useless_cast > && complain & tf_warning) > { > + /* Don't warn when converting a class object to a non-reference type, > + because that's a common way to create a temporary. */ > + if (!TYPE_REF_P (type) && CLASS_TYPE_P (TREE_TYPE (expr))) > + return; > + > if ((TYPE_REF_P (type) > && (TYPE_REF_IS_RVALUE (type) > ? xvalue_p (expr) : lvalue_p (expr)) > 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..6084b4cef49 100644 > --- a/gcc/testsuite/g++.dg/warn/Wuseless-cast.C > +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast.C > @@ -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..2db07de731f > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast3.C > @@ -0,0 +1,25 @@ > +// 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(static_cast(arg)); > +} > > base-commit: 885b6660c17fb91980b5682514ef54668e544b02