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.129.124]) by sourceware.org (Postfix) with ESMTPS id EB6413858C56 for ; Wed, 19 Oct 2022 19:31:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB6413858C56 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=1666207913; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=mMJvKslml4ddr3S+42CIS2+lFQ+hx5PjTSk0sBRFlPI=; b=CjkRWNgbiF7h6sJwGBhRLj1SsVS/awvQqmrXJ1wAMKXKVAGWRjc+f670NZZhhHzS/KRQEZ SJ4oy0IMDN1Le8c49ze4+2k1kUM7AM6kwRfyKprpqtS0kaNnXG521ymUPTJTwcFs8/jp/I u1oNB17yngSm3ZLxq0J60p++2ZNweDk= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-303-P3D03QImMWazfaxMkZSueQ-1; Wed, 19 Oct 2022 15:31:52 -0400 X-MC-Unique: P3D03QImMWazfaxMkZSueQ-1 Received: by mail-qk1-f198.google.com with SMTP id t1-20020a05620a450100b006ee79ceeb6fso15507123qkp.11 for ; Wed, 19 Oct 2022 12:31:52 -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=mMJvKslml4ddr3S+42CIS2+lFQ+hx5PjTSk0sBRFlPI=; b=uWTWySKmYsNlgKDBz9Xiw6Y5SAxe6h6GKzmKAQJQQNAWqxvp3UPIXVl2fx6X+e7XQ8 lD7dgK0m9yFxguj4OxPWrbT+xEYYyWTLk88I6MYjJNe7MQS7jHANQu0e9fc8DgsEy6zU nuLqC274mHbMjhfakTfXm1QoMzxfAICeuDpf30JDqjYavB20gWtjLOpvPflc9eFoKa3B 3axOr37iyHgSgsH3n4+o1OvUGJP5tEPOYG/BfjWLfBmyUDIW1I+52eCod1JATUXEDBp+ Asih2MOybml2jLo7djdNfRE7rWOGDsjfBsHlauixkTzXFf1Rh77S2MseI6s8zDz9zBEU i7Fw== X-Gm-Message-State: ACrzQf2nTXMJ2KVZOYzh6391o1cONZw3GWR/qYviDsezHFndj+wPKhSL 9LB6eHCiQa3OHykwfPXm+XtGoPHVYVZ2YBYaID5z8jb1bu02vA/2zmiE8MZ7pyAAX3X5x+LGEH5 EtbG2Iqsa0S3nUP8gcA== X-Received: by 2002:a05:620a:3192:b0:6ee:d298:65c2 with SMTP id bi18-20020a05620a319200b006eed29865c2mr6679280qkb.248.1666207911690; Wed, 19 Oct 2022 12:31:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Ys5ohY2EMELCbdpllJg7Hl9CaXcqEo9NQ3MVPSfdxwN1MHx9x5d9BOHioAslpB+iP7MNl+A== X-Received: by 2002:a05:620a:3192:b0:6ee:d298:65c2 with SMTP id bi18-20020a05620a319200b006eed29865c2mr6679263qkb.248.1666207911409; Wed, 19 Oct 2022 12:31:51 -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 t12-20020ac8530c000000b0039a55f78792sm4484164qtn.89.2022.10.19.12.31.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 12:31:50 -0700 (PDT) Date: Wed, 19 Oct 2022 15:31:49 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [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: User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii 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 List-Id: On Wed, Oct 19, 2022 at 02:37:23PM -0400, Jason Merrill wrote: > On 10/19/22 12:38, Marek Polacek wrote: > > 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 > > > > 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))) > > Might use ?: instead of || so we check this only when !TYPE_REF_P. OK with > that change. Ah nice, pushed with this instead of the ||: if (TYPE_REF_P (type) ? ((TYPE_REF_IS_RVALUE (type) ? xvalue_p (expr) : lvalue_p (expr)) && same_type_p (TREE_TYPE (expr), TREE_TYPE (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))) Thanks, Marek