This warning is logically incorrect: logical '&&' with non-zero constant will always evaluate as true should say '... will have no effect'? Reproduce with ============== /* run: gcc -v -save-temps -Wlogical-op -c xx.c */ #define FORCE 1 #define FLAG 1 static int func (int resp, int flags) { return (resp && (FORCE || (FLAG & flags))); } output ====== Using built-in specs. Target: i686-pc-linux-gnu Configured with: /usr/local/src/gcc/src/gcc-current/configure --srcdir=/usr/local/src/gcc/src/gcc-current --prefix=/usr/local/gcc-current --enable-languages=c,c++ --with-mpfr=/usr/local/mpfr --with-gmp=/usr/local/gmp Thread model: posix gcc version 4.3.0 20070518 (experimental) /data2/usr/local/gcc-current-20070519-083055/bin/../libexec/gcc/i686-pc-linux-gnu/4.3.0/cc1 -E -quiet -v -iprefix /data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/i686-pc-linux-gnu/4.3.0/ xx.c -mtune=generic -Wlogical-op -fpch-preprocess -o xx.i ignoring nonexistent directory "/data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/i686-pc-linux-gnu/4.3.0/../../../../i686-pc-linux-gnu/include" ignoring duplicate directory "/data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/../../lib/gcc/i686-pc-linux-gnu/4.3.0/include" ignoring duplicate directory "/data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/../../lib/gcc/i686-pc-linux-gnu/4.3.0/include-fixed" ignoring nonexistent directory "/data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/../../lib/gcc/i686-pc-linux-gnu/4.3.0/../../../../i686-pc-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/i686-pc-linux-gnu/4.3.0/include /data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/i686-pc-linux-gnu/4.3.0/include-fixed /usr/local/include /data2/usr/local/gcc-current-20070519-083055/bin/../lib/gcc/../../include /usr/include End of search list. /data2/usr/local/gcc-current-20070519-083055/bin/../libexec/gcc/i686-pc-linux-gnu/4.3.0/cc1 -fpreprocessed xx.i -quiet -dumpbase xx.c -mtune=generic -auxbase xx -Wlogical-op -version -o xx.s GNU C version 4.3.0 20070518 (experimental) (i686-pc-linux-gnu) compiled by GNU C version 4.3.0 20070518 (experimental), GMP version 4.2.1, MPFR version 2.2.1. warning: GMP header version 4.2.1 differs from library version 4.1.4. GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096 Compiler executable checksum: 128b6cbfc01a7daaa897672777a1e4cb xx.c: In function 'func': xx.c:7: warning: logical '&&' with non-zero constant will always evaluate as true as -V -Qy -o xx.o xx.s GNU assembler version 2.15 (i386-linux) using BFD version 2.15
I don't see the problem because "xx.c:7: warning: logical '&&' with non-zero constant will always evaluate as true" means the non-zero constant will evaluate as true and not the logical will evaluate as true.
(In reply to comment #1) > I don't see the problem because > "xx.c:7: warning: logical '&&' with non-zero constant will always evaluate as > true" > means the non-zero constant will evaluate as true and not the logical will > evaluate as true. > (In reply to comment #1) > I don't see the problem because > "xx.c:7: warning: logical '&&' with non-zero constant will always evaluate as > true" > means the non-zero constant will evaluate as true and not the logical will > evaluate as true. > But it says 'logical... will always evaluate as true' which clearly refers to the result of the logical operator, not to one of its arguments. BTW, why no warning for this? resp == 0 && 0 Naturally, all the above constants will hide behind some macros which could indicate a real error, hence the value of the warning.
Confirmed. I have a patch for this for 4.5. The new wording will say: warning: logical 'or' applied to non-boolean constant. Are you satisfied with this? > > BTW, why no warning for this? > resp == 0 && 0 > Naturally, all the above constants will hide behind some macros which could > indicate a real error, hence the value of the warning. Warning for (x && 0) or (x && 1) would produce hundreds of false positives. We shouldn't warn about your testcase either.
(In reply to comment #3) > Confirmed. I have a patch for this for 4.5. > > The new wording will say: > > warning: logical 'or' applied to non-boolean constant. > > Are you satisfied with this? Great, thanks.
Patch submitted: http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00824.html
Subject: Bug 32061 Author: manu Date: Sun Apr 19 11:04:13 2009 New Revision: 146344 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=146344 Log: 2009-04-19 Manuel López-Ibáñez <manu@gcc.gnu.org> PR c/32061 PR c++/36954 * doc/invoke.texi: Add -Wlogical-op to -Wextra. * common.opt (Wlogical-op): Move from here... * c.opt (Wlogical-op): ... to here. * c-typeck.c (parser_build_binary_op): Update call to warn_logical_operator. * c-opts.c (c_common_post_options): Enable warn_logical_op with extra_warnings. * c-common.c (warn_logical_op): Update. * c-common.h (warn_logical_op): Update declaration. cp/ * call.c (build_new_op): Save the original codes of operands before folding. testsuite/ * gcc.dg/pr32061.c: New. * gcc.dg/Wlogical-op-1.c: Update. * g++.dg/warn/Wlogical-op-1.C: Update. * g++.dg/warn/pr36954.C: New. Added: trunk/gcc/testsuite/g++.dg/warn/pr36954.C trunk/gcc/testsuite/gcc.dg/pr32061.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-common.c trunk/gcc/c-common.h trunk/gcc/c-opts.c trunk/gcc/c-typeck.c trunk/gcc/c.opt trunk/gcc/common.opt trunk/gcc/cp/ChangeLog trunk/gcc/cp/call.c trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/g++.dg/warn/Wlogical-op-1.C trunk/gcc/testsuite/gcc.dg/Wlogical-op-1.c
FIXED in GCC 4.5