[Bug c/25733] missed diagnostic about assignment used as truth value.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733 --- Comment #13 from Vincent Lefèvre --- Another example: int f1 (int a, int b, int c, int d) { if (a = c && b == d) return 0; else return 1; } int f2 (int a, int b, int c, int d) { if (a == b || (a = c && b == d)) return 0; else return 1; } int f3 (int a, int b, int c, int d) { if (a == b && (a = c && b == d)) return 0; else return 1; } GCC emits a warning for f1 (this is the usual case), but neither for f2, nor for f3. Note that for f2, the parentheses around what should be a == c && b == d are optional, but strongly recommended (GCC has a warning for that), so one would really expect if (a == b || (a == c && b == d)) or if (a == b || ((a = c && b == d))) For f3, the parentheses around what should be a == c && b == d are not specially recommended (but may be useful to emphasize on some symmetry in some context), but I think that there should be the same rule as f2. FYI, MPFR had an undetected bug corresponding to f3 and a warning would have really been useful: https://gforge.inria.fr/scm/viewvc.php/mpfr/trunk/src/sqrt.c?r1=11021=11022
[Bug c/25733] missed diagnostic about assignment used as truth value.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733 Manuel López-Ibáñez manu at gcc dot gnu.org changed: What|Removed |Added CC||aravindvijayan224185 at ||gmail dot com --- Comment #12 from Manuel López-Ibáñez manu at gcc dot gnu.org 2012-01-19 13:48:48 UTC --- *** Bug 51894 has been marked as a duplicate of this bug. ***
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #7 from manu at gcc dot gnu dot org 2010-02-21 00:30 --- (In reply to comment #3) As another data-point, if ( (a=10) ) ; also doesn't warn. I'm not sure what the standard says on that, but other contemporary compilers do give the an assignment used as truth value warning for the example above. How do other compilers deal with false positives? That is, how can a programmer specify that they really want to do an assignment? We could use a cast to bool. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #8 from manu at gcc dot gnu dot org 2010-02-21 00:33 --- *** Bug 32587 has been marked as a duplicate of this bug. *** -- manu at gcc dot gnu dot org changed: What|Removed |Added CC||fritz at intrinsity dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #9 from manu at gcc dot gnu dot org 2010-02-21 00:34 --- Another testcase: int bar(int a, int b, int c) { if ((b = c) (a == 0)) { return 0; } return 1; } -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #10 from bangerth at gmail dot com 2010-02-21 01:25 --- (In reply to comment #7) (In reply to comment #3) As another data-point, if ( (a=10) ) ; also doesn't warn. I'm not sure what the standard says on that, but other contemporary compilers do give the an assignment used as truth value warning for the example above. How do other compilers deal with false positives? That is, how can a programmer specify that they really want to do an assignment? We could use a cast to bool. I think at least in C++ the warning is useful. Conditions in if(...) statements have type bool, and things like if (a=10) use the implicit conversion from int to bool. If a programmer wants to avoid the warning, one can always be explicit and write if ( (a=10) != 0) W. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #11 from manu at gcc dot gnu dot org 2010-02-21 01:43 --- I am not saying that the warning is not useful. I am saying that the current behaviour of not warning if there are parenthesis is not ideal. So either: if ((bool) a = 0) or if ( (a=0) != 0) seem much better choices for avoiding warning. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #3 from ianw at vmware dot com 2008-04-28 22:14 --- As another data-point, if ( (a=10) ) ; also doesn't warn. I'm not sure what the standard says on that, but other contemporary compilers do give the an assignment used as truth value warning for the example above. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #4 from ianw at vmware dot com 2008-04-28 22:16 --- Oh, just to be clear, my point was if (a=10) warns, but the extra parenthesis hide the warning. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #5 from pinskia at gcc dot gnu dot org 2008-04-28 22:17 --- (In reply to comment #4) Oh, just to be clear, my point was if (a=10) warns, but the extra parenthesis hide the warning. That is by design. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #6 from ianw at vmware dot com 2008-04-28 22:28 --- (In reply to comment #5) (In reply to comment #4) Oh, just to be clear, my point was if (a=10) warns, but the extra parenthesis hide the warning. That is by design. Ok, I did try looking but is that documented anywhere? It would seem to have ramifications for people that do things like make an ASSERT statement something like #define ASSERT(v) {if (!(v))} A quick search on google codesearch showed this was a very common idiom... -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #2 from manu at gcc dot gnu dot org 2008-04-27 22:13 --- *** Bug 36050 has been marked as a duplicate of this bug. *** -- manu at gcc dot gnu dot org changed: What|Removed |Added CC||ianw at vmware dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733
[Bug c/25733] missed diagnostic about assignment used as truth value.
--- Comment #1 from rguenth at gcc dot gnu dot org 2006-01-10 10:17 --- Confirmed. Same in the C++ frontend. -- rguenth at gcc dot gnu dot org changed: What|Removed |Added Severity|normal |enhancement Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 GCC build triplet|i686-pld-linux | GCC host triplet|i686-pld-linux | GCC target triplet|i686-pld-linux | Keywords||diagnostic Known to fail|3.4.5 4.1.0 |2.95.3 3.3.6 3.4.5 4.0.2 ||4.1.0 Last reconfirmed|-00-00 00:00:00 |2006-01-10 10:17:07 date|| http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25733