[{"id":3684764,"web_url":"http://patchwork.ozlabs.org/comment/3684764/","msgid":"<8d188ded-0e3a-426d-b3ef-6357cc98440f@redhat.com>","list_archive_url":null,"date":"2026-04-30T14:49:56","subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","submitter":{"id":4337,"url":"http://patchwork.ozlabs.org/api/people/4337/","name":"Jason Merrill","email":"jason@redhat.com"},"content":"On 4/30/26 4:16 AM, Jakub Jelinek wrote:\n> Hi!\n> \n> Given the recent (data->flags && ff_genericize) vs.\n> (data->flags & ff_genericize) typo, I've looked at warning in similar\n> cases.\n> We don't warn for cases like that at all, clang/clang++ has\n> -Wconstant-logical-operand warning enabled by default.\n> Their behavior is:\n> 1) only warns for rhs of &&/|| (why?)\n> 2) don't warn if rhs is bool\n> 3) for C++ warn if rhs is constant or folds into constant,\n>     for C warn if rhs is constant or folds into constant and\n>     that constant is not 0 or 1\n> 4) I think it doesn't warn if rhs comes from a macro\n> The following patch implements similar warning with similar wording,\n> just provides the value of the constant, but\n> 1) warns for lhs and rhs\n> 2) doesn't warn if either lhs or rhs is bool\n> 3) doesn't warn if lhs or rhs is or folds to constant 0 or 1\n> 4) doesn't care if it comes from a macro or not\n> I think 64 && x is similarly suspicious to x && 64 and both\n> are likely to be meant 64 & x or x & 64.\n\nAgreed.\n\n> I think having\n> && 1 or && 0 is common even in C++, people don't always write\n> && true or && false etc. and don't see why C++ would be different\n> in that from C, I think people sometimes write\n>   if (1\n>   #ifdef ABC\n>       && ABC\n>   #endif\n>   #ifdef DEF\n>       && DEF\n>   #endif\n>       && 1)\n> and similar (or similarly with 0/true/false or ||).\n\nHmm, I often write things like this, but only locally/temporarily to \ndisable a change for testing; I wouldn't mind a warning about it.  But \nwe wouldn't want to warn if it comes from a macro or enumerator, and I \nsuppose it's unlikely to be an accident, so not warning also seems fine.\n\n> And the warning\n> is only enabled in -Wall, not by default.  This won't warn about\n> the ff_genericize case, because that is actually data->flags && 1\n> and I think that isn't that suspicious.\n\nI think it would still be good to warn if the original operand has enum \ntype with flag_enum attribute (and use that attribute on more enums).\n\nJason","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=VSxyio3J;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=VSxyio3J","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.133.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5xw454y5z1xqf\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 01 May 2026 00:52:04 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 824B34BAE7F2\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 14:50:31 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.124])\n by sourceware.org (Postfix) with ESMTP id 72A654B9DB56\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 14:50:03 +0000 (GMT)","from mail-lj1-f199.google.com (mail-lj1-f199.google.com\n [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-554-R_CC3V0mOCeC-P0Q_LWC3Q-1; Thu, 30 Apr 2026 10:50:01 -0400","by mail-lj1-f199.google.com with SMTP id\n 38308e7fff4ca-38cd8390a28so5638151fa.2\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 07:50:00 -0700 (PDT)","from [192.168.50.130]\n (130-44-146-247.s12789.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com.\n [130.44.146.247]) by smtp.gmail.com with ESMTPSA id\n 2adb3069b0e04-5a85865ee07sm14565e87.21.2026.04.30.07.49.57\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 30 Apr 2026 07:49:58 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 824B34BAE7F2","OpenDKIM Filter v2.11.0 sourceware.org 72A654B9DB56"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 72A654B9DB56","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 72A654B9DB56","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777560603; cv=none;\n b=QuW9hfFtfPb/2N5vmR9f5aEJXd/KtOw7LN5zbaHqCJ/dhopQ/UJ/ZAvDkPe3ZpgSvFMrSS/km/3v1zq1Fq2PC3VyDqubhSzb/rLh9S865U3iy8ADwbaDssidQBc0wDLRNtAN+5gMBZk2LJdAppDvq1yue+hi/XypuoMnC/583XQ=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777560603; c=relaxed/simple;\n bh=Ez2o2Da/8BoGdNpA/Bv6Jkwu0sPwTdJp5AtqD+WJQtY=;\n h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From;\n b=WoxdnJO8IodaZGbl19zCNaUlNdTgp3BBGY7dvfEgnlqxNIzvxXPw4Cy4j6af3/KmkxiNY6yuVXxCs7EtkDfQ1O/fprOuHaCjQX+doS9vFCjvMwwLF4543RGaiEBNVds5MhEbswtr7BvocB2QqfTYvdmCqOGTM1nZoISDGK5BrfA=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777560603;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=7TRDRMpGj58rjmpfODilA0Pd7UeVVdcWTzSOuJEpzh0=;\n b=VSxyio3J8pON2xxyUPyjF2h2MCtWXx8E7V0EplsoGK7bwzed84OIQbTcxYUS9seWm11J/Z\n QmkEx3mSldAsdQ7DaEc1P8g9Du2FnfKAGZRq+TYCtwFc9D4T5jJ3iLuUd61GAEs0Y8+gYu\n pLDT6Vk2WWs6H5PWPPaRZJrmweHqRDQ=","X-MC-Unique":"R_CC3V0mOCeC-P0Q_LWC3Q-1","X-Mimecast-MFC-AGG-ID":"R_CC3V0mOCeC-P0Q_LWC3Q_1777560600","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777560599; x=1778165399;\n h=content-transfer-encoding:in-reply-to:from:content-language\n :references:cc:to:subject:user-agent:mime-version:date:message-id\n :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id\n :reply-to;\n bh=7TRDRMpGj58rjmpfODilA0Pd7UeVVdcWTzSOuJEpzh0=;\n b=UKFBPeZadHpqP3DtwO5h+JJmsihXcmrTwMeSd/xn0z9j20YuJPqhSPU4/e5qGvFtjt\n FveqYBnBAS+4sR9OPCMDc3QdKkNg4Dz2e6CsP3vmyYJmWX1T5OqwFiawJ2GKJ/CC4En9\n USP7IeARQ20ylJB8SOGQhriGkt7kcC5GLR169FRffzcbO10w0Tu1EpYHr3jZlSPt7icn\n OMH8Z+AIuIUoKz70glHRdxyATgCOKlzg3ymxh8KJKCt2xvj3aTk0X1wdvwbpj67DczpR\n dQkOcGNRScXz5pELI3CpzFw+7h+CKaQeqkPSe2Juj2NAL8AFDJjaICbVpTjDYdS9sVA7\n 4Brw==","X-Gm-Message-State":"AOJu0YzFH0gMRyvMBv4HK7hQQ3DipWM7VDtOwh0AqfbY8+5Kpz/ejAnu\n rUXZ4We4dxstsXibpt3mzVJP0EuNPVJAQoGqbyfS0EJTQWUNLyf6Z1Y5g6NWWtiMof7ZRKLVRDP\n w6qRCQRlu8TM9EZgN1jYExaTkugRvWPXLaSuMsD19cToK/QM9t6oVXOl7H9M=","X-Gm-Gg":"AeBDiesjJFz3yGK9v07NyO3rJRk+MXbt/cuzGk3jNSn7vqkDTP+1G6pBnrjj59us47Z\n RduyHb84FZVlwK6fsqETQlaXzZM9lNcbhVIF7pCgudJ4hiRVbZjAKnEtIc6vZKU5q5ERwXEXRS0\n vpbOUJhM507WXe9DcvOVft6VCRuZ2UL6ER+asxhcsnyrP/Eov/a2OsVwO90PVL0WntaOEE3FqHu\n jvgQFOGuIJNYwe7jPIPOngi2kJttVzjUCmxcyQVmvc4nJIzJKMUQU2MUAtg9VACalNaYD9TskFz\n V5D21dZEMZg4mOZKmBwDC9KrSIiQyYPzDp3wokAZc9DHNIC30PcY7VbbZOiWRexK/OBHjgLqOKM\n DJ6yFT+faiWYOHLPgQ/ToBJKmU1PorWXe5zVBnNW/pQnoD3bcr4oe67WuLXbvQrz9sLn+jg9F99\n tcAYobZKTt2SYkfdrNjWOsmjdcUynH87zfZ63lSVgxuA==","X-Received":["by 2002:a05:6512:3b14:b0:5a2:7b74:3c60 with SMTP id\n 2adb3069b0e04-5a8522dbd08mr1161211e87.33.1777560599408;\n Thu, 30 Apr 2026 07:49:59 -0700 (PDT)","by 2002:a05:6512:3b14:b0:5a2:7b74:3c60 with SMTP id\n 2adb3069b0e04-5a8522dbd08mr1161186e87.33.1777560598792;\n Thu, 30 Apr 2026 07:49:58 -0700 (PDT)"],"Message-ID":"<8d188ded-0e3a-426d-b3ef-6357cc98440f@redhat.com>","Date":"Thu, 30 Apr 2026 10:49:56 -0400","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","To":"Jakub Jelinek <jakub@redhat.com>, \"Joseph S. Myers\"\n <josmyers@redhat.com>, Marek Polacek <polacek@redhat.com>,\n Patrick Palka <ppalka@redhat.com>","Cc":"gcc-patches@gcc.gnu.org","References":"<afMPwPZBZlyCU7TG@tucnak>","From":"Jason Merrill <jason@redhat.com>","In-Reply-To":"<afMPwPZBZlyCU7TG@tucnak>","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"qPAz5G_Nhow9pcI8EY5sf8YDCcanKx2ra6R8JuQ40cs_1777560600","X-Mimecast-Originator":"redhat.com","Content-Language":"en-US","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}},{"id":3684775,"web_url":"http://patchwork.ozlabs.org/comment/3684775/","msgid":"<afNwI0F3WFI9i1bL@tucnak>","list_archive_url":null,"date":"2026-04-30T15:07:15","subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","submitter":{"id":671,"url":"http://patchwork.ozlabs.org/api/people/671/","name":"Jakub Jelinek","email":"jakub@redhat.com"},"content":"On Thu, Apr 30, 2026 at 10:49:56AM -0400, Jason Merrill wrote:\n> On 4/30/26 4:16 AM, Jakub Jelinek wrote:\n> > Given the recent (data->flags && ff_genericize) vs.\n> > (data->flags & ff_genericize) typo, I've looked at warning in similar\n> > cases.\n> > We don't warn for cases like that at all, clang/clang++ has\n> > -Wconstant-logical-operand warning enabled by default.\n> > Their behavior is:\n> > 1) only warns for rhs of &&/|| (why?)\n> > 2) don't warn if rhs is bool\n> > 3) for C++ warn if rhs is constant or folds into constant,\n> >     for C warn if rhs is constant or folds into constant and\n> >     that constant is not 0 or 1\n> > 4) I think it doesn't warn if rhs comes from a macro\n> > I think having\n> > && 1 or && 0 is common even in C++, people don't always write\n> > && true or && false etc. and don't see why C++ would be different\n> > in that from C, I think people sometimes write\n> >   if (1\n> >   #ifdef ABC\n> >       && ABC\n> >   #endif\n> >   #ifdef DEF\n> >       && DEF\n> >   #endif\n> >       && 1)\n> > and similar (or similarly with 0/true/false or ||).\n> \n> Hmm, I often write things like this, but only locally/temporarily to disable\n> a change for testing; I wouldn't mind a warning about it.  But we wouldn't\n> want to warn if it comes from a macro or enumerator, and I suppose it's\n> unlikely to be an accident, so not warning also seems fine.\n> \n> > And the warning\n> > is only enabled in -Wall, not by default.  This won't warn about\n> > the ff_genericize case, because that is actually data->flags && 1\n> > and I think that isn't that suspicious.\n> \n> I think it would still be good to warn if the original operand has enum type\n> with flag_enum attribute (and use that attribute on more enums).\n\nI can certainly implement that.  Or just warn also whenever the constant is\n1 with some enum type originally, I don't see why people actually would write\nenum { A, B }; if (x && B) unless it is a poor man's pre-C11 replacement of bool (so\nsay typedef enum BOOL { FALSE, TRUE } BOOL;\nand using if (x && TRUE)).  Or warn for constant 1 if it has enum type and\nthe enum type contains more enumerators than just 0/1.  One can always get\nrid of the warning by using && ENUM != 0 instead of just && ENUM and make\nthe code even more readable that way.\n\n\tJakub","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=IMp9TUOY;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=IMp9TUOY","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.133.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5yGP4fSkz1xqf\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 01 May 2026 01:07:56 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id BDA774371D66\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 15:07:53 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.124])\n by sourceware.org (Postfix) with ESMTP id 7005D436F7CF\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 15:07:25 +0000 (GMT)","from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-360-V8A9NMLvPXSDlbhH_ZFfLg-1; Thu,\n 30 Apr 2026 11:07:20 -0400","from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id B25EC18005A9\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 15:07:19 +0000 (UTC)","from tucnak.zalov.cz (unknown [10.44.34.21])\n by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with\n ESMTPS\n id E4339180045E; Thu, 30 Apr 2026 15:07:18 +0000 (UTC)","from tucnak.zalov.cz (localhost [127.0.0.1])\n by tucnak.zalov.cz (8.18.1/8.18.1) with ESMTPS id 63UF7G1i3802023\n (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT);\n Thu, 30 Apr 2026 17:07:16 +0200","(from jakub@localhost)\n by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 63UF7FVY3802022;\n Thu, 30 Apr 2026 17:07:15 +0200"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org BDA774371D66","OpenDKIM Filter v2.11.0 sourceware.org 7005D436F7CF"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 7005D436F7CF","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 7005D436F7CF","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777561645; cv=none;\n b=tACPbxn5mPAup7U/IenmMx/PZhmb+92rLn/zceWqan1mxQglTFL6+BN6y57gPpkpY2SSPwM7nleReBJJiLPcIJ2n3nZC11m6VdULUc90KKJUumD7wAaT6OR28KkwOJ2UG0tz/m6OSeFrrQ/ctENu50zWGnYbZx4qi8JzU/qdtas=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777561645; c=relaxed/simple;\n bh=TZ3Wn0qzXmtrya8FqzFt2LnpGmiGGZJ4HY/bB2PwloY=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=Izy1KR80oYrnTaMerCh5zwzPaVXu3APbAd4Xj2PgdCxZUlJ83a1U3BkP19MoPQcZ0l8t+tT0dV3h3Aj7bStNNelCsaJckLDeNMUw/9f9+F8Pt7GZ8Dx6vc5CFmU/YRJrDDuAQ/0ZbJiywKUwXAlsJxGcJ+aqSvFyy66iNbaIXgY=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777561645;\n h=from:from:reply-to:reply-to:subject:subject:date:date:\n message-id:message-id:to:to:cc:cc:mime-version:mime-version:\n content-type:content-type:in-reply-to:in-reply-to:  references:references;\n bh=wEwTb1+i0u3fyV8uFJk+bwhtxVAHDKbub8SKuVtq5wg=;\n b=IMp9TUOYrkL3jXDqTW+PIyyjZfz5SKW0QWRty+1hzrJ4ecEp5YfBOkYwnFoPTBAcF271Lz\n cI6isduNB9L7roKuhWmol6vt70sl1f6gg7JB8xVOwuwbLeBjKHZPpy8K2R+63zXCb0ZKxi\n k3M59Axdu0IzX0CqI5v7WzflsU6nFyU=","X-MC-Unique":"V8A9NMLvPXSDlbhH_ZFfLg-1","X-Mimecast-MFC-AGG-ID":"V8A9NMLvPXSDlbhH_ZFfLg_1777561640","Date":"Thu, 30 Apr 2026 17:07:15 +0200","From":"Jakub Jelinek <jakub@redhat.com>","To":"Jason Merrill <jason@redhat.com>","Cc":"\"Joseph S. Myers\" <josmyers@redhat.com>,\n Marek Polacek <polacek@redhat.com>, Patrick Palka <ppalka@redhat.com>,\n gcc-patches@gcc.gnu.org","Subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","Message-ID":"<afNwI0F3WFI9i1bL@tucnak>","References":"<afMPwPZBZlyCU7TG@tucnak>\n <8d188ded-0e3a-426d-b3ef-6357cc98440f@redhat.com>","MIME-Version":"1.0","In-Reply-To":"<8d188ded-0e3a-426d-b3ef-6357cc98440f@redhat.com>","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.111","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"wY9yDb5zwJA2dqSOLPrMiyRkd2EhgFy8uylmbce-J2w_1777561640","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Reply-To":"Jakub Jelinek <jakub@redhat.com>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}},{"id":3684885,"web_url":"http://patchwork.ozlabs.org/comment/3684885/","msgid":"<9acfb25a-8cb0-45f1-8059-d959d8e88b2a@redhat.com>","list_archive_url":null,"date":"2026-04-30T18:08:46","subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","submitter":{"id":4337,"url":"http://patchwork.ozlabs.org/api/people/4337/","name":"Jason Merrill","email":"jason@redhat.com"},"content":"On 4/30/26 11:07 AM, Jakub Jelinek wrote:\n> On Thu, Apr 30, 2026 at 10:49:56AM -0400, Jason Merrill wrote:\n>> On 4/30/26 4:16 AM, Jakub Jelinek wrote:\n>>> Given the recent (data->flags && ff_genericize) vs.\n>>> (data->flags & ff_genericize) typo, I've looked at warning in similar\n>>> cases.\n>>> We don't warn for cases like that at all, clang/clang++ has\n>>> -Wconstant-logical-operand warning enabled by default.\n>>> Their behavior is:\n>>> 1) only warns for rhs of &&/|| (why?)\n>>> 2) don't warn if rhs is bool\n>>> 3) for C++ warn if rhs is constant or folds into constant,\n>>>      for C warn if rhs is constant or folds into constant and\n>>>      that constant is not 0 or 1\n>>> 4) I think it doesn't warn if rhs comes from a macro\n>>> I think having\n>>> && 1 or && 0 is common even in C++, people don't always write\n>>> && true or && false etc. and don't see why C++ would be different\n>>> in that from C, I think people sometimes write\n>>>    if (1\n>>>    #ifdef ABC\n>>>        && ABC\n>>>    #endif\n>>>    #ifdef DEF\n>>>        && DEF\n>>>    #endif\n>>>        && 1)\n>>> and similar (or similarly with 0/true/false or ||).\n>>\n>> Hmm, I often write things like this, but only locally/temporarily to disable\n>> a change for testing; I wouldn't mind a warning about it.  But we wouldn't\n>> want to warn if it comes from a macro or enumerator, and I suppose it's\n>> unlikely to be an accident, so not warning also seems fine.\n>>\n>>> And the warning\n>>> is only enabled in -Wall, not by default.  This won't warn about\n>>> the ff_genericize case, because that is actually data->flags && 1\n>>> and I think that isn't that suspicious.\n>>\n>> I think it would still be good to warn if the original operand has enum type\n>> with flag_enum attribute (and use that attribute on more enums).\n> \n> I can certainly implement that.  Or just warn also whenever the constant is\n> 1 with some enum type originally, I don't see why people actually would write\n> enum { A, B }; if (x && B) unless it is a poor man's pre-C11 replacement of bool (so\n> say typedef enum BOOL { FALSE, TRUE } BOOL;\n> and using if (x && TRUE)).  Or warn for constant 1 if it has enum type and\n> the enum type contains more enumerators than just 0/1.  One can always get\n> rid of the warning by using && ENUM != 0 instead of just && ENUM and make\n> the code even more readable that way.\n\nSounds good.\n\nJason","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=eiCsW0aD;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=eiCsW0aD","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g62Hl2TDXz1xqf\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 01 May 2026 04:09:22 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 004F5436F3ED\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 18:09:19 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 60B6B436F7FC\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 18:08:52 +0000 (GMT)","from mail-qk1-f200.google.com (mail-qk1-f200.google.com\n [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS\n (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n us-mta-471-xQDO_Ur5NAuYExGrwTBqOA-1; Thu, 30 Apr 2026 14:08:49 -0400","by mail-qk1-f200.google.com with SMTP id\n af79cd13be357-8f9e55c40a5so153452185a.3\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 11:08:49 -0700 (PDT)","from [192.168.50.130]\n (130-44-146-247.s12789.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com.\n [130.44.146.247]) by smtp.gmail.com with ESMTPSA id\n af79cd13be357-8fbb0af2090sm46019585a.5.2026.04.30.11.08.47\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 30 Apr 2026 11:08:47 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 004F5436F3ED","OpenDKIM Filter v2.11.0 sourceware.org 60B6B436F7FC"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 60B6B436F7FC","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 60B6B436F7FC","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777572532; cv=none;\n b=GrbfFT9w5rZwUFOucRKDzaEJ8V8DLbjnQYBH/BLHkyql1U7PUprLtVi1dx9W/rVONPqm2VL8tDugpkKOti5t4n/3fXZAFoqWu4zsRL9S1txJ5YDuYsFQgA1+OqaP4QsOwQBveF4MDGMkfuQZ+kVHP19Y5BuxiQv8o9IUf0Rwkcw=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777572532; c=relaxed/simple;\n bh=+6MG5MMkPqXbXqKhUJ5WAJT+/PC01K0YFdNwBSnDhcc=;\n h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From;\n b=XDzco2lNzoWEVonz0yc36CufHvsSR7Cco2z04yckzQ2rw/AfYACHUV+14rFQB3fEfSG7kA6MhxCyzlX8vltOSoVOqFDU8yivNnTN3Sx7bgY34NfBsuovWaNiQ4B8Ja5LNNhpFcH1MpGlfBk7ln1dJBtOqv8C6bjMWrvfo8GMmHg=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777572532;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=VtZJg9NM1sNIR3/7KBBtJv7X8f5SCvs+EBaJDuURVv8=;\n b=eiCsW0aDkATFsZj8exSwd7mhLsFHPv6Gs4cLVByI+578g3MVptT9E+v7NO6GpiMu/bdqXp\n wiqwiZYg20qWwKfhkpngQi85+Z2zTIpZhSttYM8NWCIbd92yGjtzDn0jj92RkLUX8mgdfV\n Z5kDJoasdqQcq004lBfqVJZKSlhgyqk=","X-MC-Unique":"xQDO_Ur5NAuYExGrwTBqOA-1","X-Mimecast-MFC-AGG-ID":"xQDO_Ur5NAuYExGrwTBqOA_1777572529","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777572529; x=1778177329;\n h=content-transfer-encoding:in-reply-to:from:content-language\n :references:cc:to:subject:user-agent:mime-version:date:message-id\n :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id\n :reply-to;\n bh=VtZJg9NM1sNIR3/7KBBtJv7X8f5SCvs+EBaJDuURVv8=;\n b=K34fi6q8tEmv+lHz896CgKrTdEt61aIb8PSVkL6ezm81GbzneiNAEapKh31V8ra7x/\n 62R93CNau8gkRMEuVaatgSsnYfGH9GNgQEL70aLT9uWSXnEytH7wLosTvj+3qre/7dXE\n aniK2ZH47EI+mW/vqNOmWkWd2rma+bVsooEI5uzgRhRnUCtMA3EtBzRlmfRQDZrgL+C9\n J14nBqk5tsM64vuHoy36xnuq9oRrtQksYGB6AKlHY2+adO3IhQj3Z56UU+8O2QZetZ+g\n ugQe5axgvKvvscs/f+pju0iZOaqT1VJ7m2+sOkBAR3LNp9kPJAZXZsT6z/rG3pFsexFt\n g5JQ==","X-Forwarded-Encrypted":"i=1;\n AFNElJ/OOul61zPYyzZ8MiL7NXRxP7TuZlYtR7mrunq7h6vgkiQG10wX9HrTz2AfWXyZVNZd6Lp1jOEW+G/aOA==@gcc.gnu.org","X-Gm-Message-State":"AOJu0YxpWN5ccH9ATt5B60GyKz00kii2ovzymyyQjk/DmQfadcVPZa3e\n mmUCJVtsrD2biDVFbsuXlr//o+aj2kr4t7iML442fpiuLtG58jBii9IUpel37Edg6V38ONFjRPF\n FnyzQdxzBhO29lTiFWM/n8BS8AKeHrs/e9ydi19CddKdJJUGrizqUmvshU4U=","X-Gm-Gg":"AeBDiesHmPps16vGOvzI8wLRdl+cE37iyr/LY86ERA5wUrb8ZV6u27hqwIPZNm3v34e\n KxArtDqtG+UV02sv6/e3tCaNVR08uj2G84JtcFLVk4cpyIQN0AsRES6DsG3D3Sz6iXPmKStUrZf\n ILLoaO9GS5/5duclDfthtCe0eCB9WHGszmYBkCa8NmYuvUpifvyezUCpnfX6y6pI7+aCZyVodr4\n WY8sjMr7sBJI+efkOIiIRmkubeuKBTr2MJFiLu+Qo9w+9bsE3cPNz+u7f1lwfGO0c01/9NXWjor\n PXu6vHLNRGAF0v23PFuZ3g7g8sMe1swBSWOfIO23LueqnYqdnRRUKN062d57e95H63sxJX3Nwdn\n 7re75aOdLW6E+KyAfATJWfwoH2m2+QAF7kPuwfH6TvqCqCRjbyU9jpREajgiVhhbmRly7E4ubP+\n gAwaZMtonEMhN4+D9dHCKcofe6zvAZImfWk1weQIKKAw==","X-Received":["by 2002:a05:620a:bc5:b0:8d1:d0db:392e with SMTP id\n af79cd13be357-8fa88b3b526mr639278785a.32.1777572528917;\n Thu, 30 Apr 2026 11:08:48 -0700 (PDT)","by 2002:a05:620a:bc5:b0:8d1:d0db:392e with SMTP id\n af79cd13be357-8fa88b3b526mr639272485a.32.1777572528374;\n Thu, 30 Apr 2026 11:08:48 -0700 (PDT)"],"Message-ID":"<9acfb25a-8cb0-45f1-8059-d959d8e88b2a@redhat.com>","Date":"Thu, 30 Apr 2026 14:08:46 -0400","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH] c, c++: Introduce -Wconstant-logical-operand warning\n [PR125081]","To":"Jakub Jelinek <jakub@redhat.com>","Cc":"\"Joseph S. Myers\" <josmyers@redhat.com>,\n Marek Polacek <polacek@redhat.com>, Patrick Palka <ppalka@redhat.com>,\n gcc-patches@gcc.gnu.org","References":"<afMPwPZBZlyCU7TG@tucnak>\n <8d188ded-0e3a-426d-b3ef-6357cc98440f@redhat.com> <afNwI0F3WFI9i1bL@tucnak>","From":"Jason Merrill <jason@redhat.com>","In-Reply-To":"<afNwI0F3WFI9i1bL@tucnak>","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"qvGVGDQjnxw3A16zwxarnwnb5xDlJskVdLnH6uOEnEk_1777572529","X-Mimecast-Originator":"redhat.com","Content-Language":"en-US","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]