From patchwork Tue Oct 18 17:38:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 4269 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2080108wrs; Tue, 18 Oct 2022 10:39:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7VXraXwdqcT5OjDL83LSZa/bjs6v9BNHXB/skjZUbP3Ga7yRJ1ZCVmGkfmjpB4MBRFNU/y X-Received: by 2002:a17:907:2c44:b0:78d:4e67:ca5d with SMTP id hf4-20020a1709072c4400b0078d4e67ca5dmr3336344ejc.397.1666114777851; Tue, 18 Oct 2022 10:39:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666114777; cv=none; d=google.com; s=arc-20160816; b=0Xf4FZPNkQas/4r6MgOIF9ko3C/JQeqnUc0VPk2j/zoum8Ezq9adSFf475s8sceb9O XZB54+0xxa/MGXRbKYAzXTUR4ayKW8m+81a2vsfZKcYrILIQ0XKzpFUxiDapGVoa//+f UtZMEj24Rr24te0bTYa6BMzStRHChX+bOptsM+mYittLBSxImnbEemaT0Bmq3ApCCfZF fXHXWpLc6DP42hqFoFUYbA15pMfZuRdPZGZndwH97ndNJ40SNU8qJNyRtrw4LQ0HFyiv Zn7u9s+zNqS168AsNtdMNjPNEaNQgxvbuDkPEEXgkttIa5P5CDA5250zGhcUClea4CJ+ bvYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=EmDWNgx0eGVDoGDQWfHsk+JsEYef5GmnZIAzQ2J8sMU=; b=VO/FZ/v0smLD8xUTPHOKGxeux+55Xl+ob9mrQYAhHbtMuzT+/lydvMtKeXBo8tRn9q fc12LSg5r32KaU2QT9vllWaYE31RXYfvXUWvUU1XtIca4E6j57HeNPFN1LS5snJUaDCF LMkYtXAb7/0V/Bcy9J6p7S+LbiGeYJ0HAv9aYUCaT3Vxdj8GYkAJEKf75i8yl2NHGxVE Oqqqxlazl7y0uAckfpok3ng4JIGNOslT4udnJkBvzxkMxwIxqhBFrl0UmpuQQ8OY9QyR ZncHeqVYEOqkbfFPE6k3ONZ2s1H4b+laFxBoit3j263um2HagLxr7OJPsqz4XxlcHztN Eezw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=x0XKkhEE; 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 he38-20020a1709073da600b0073d6b849d4fsi12306262ejc.731.2022.10.18.10.39.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 10:39:37 -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=x0XKkhEE; 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 A493F385841F for ; Tue, 18 Oct 2022 17:39:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A493F385841F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666114776; bh=EmDWNgx0eGVDoGDQWfHsk+JsEYef5GmnZIAzQ2J8sMU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=x0XKkhEE4/jVhxBC6F8ppUCEfxKn+LL910cny5n/caFwaVU2SFM/3Snh0eQfmLTcw TjPfIZkknvZUsgRB48miH64ybYtEz7CZQtnQz9oQMwZNC3udXcDZFsNsD0MiZiF0Bb WYgx5JTGVojAbOU9alAOUhcEptE6zj2trsSL4EbM= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 8943C3858D32 for ; Tue, 18 Oct 2022 17:38:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8943C3858D32 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-185-1M_OMvSqNr2lMi9Qb1XHUQ-1; Tue, 18 Oct 2022 13:38:34 -0400 X-MC-Unique: 1M_OMvSqNr2lMi9Qb1XHUQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A1E2A381A728 for ; Tue, 18 Oct 2022 17:38:34 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 846EA2022EBA; Tue, 18 Oct 2022 17:38:34 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Mitigate -Wuseless-cast with classes [PR85043] Date: Tue, 18 Oct 2022 13:38:29 -0400 Message-Id: <20221018173829.392773-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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 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?1747047969259548962?= -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. 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 base-commit: 885b6660c17fb91980b5682514ef54668e544b02 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)); +}