Given the following C++ source code void g(); void h(); void f( int a, int b) { if (b = 1) // case 1 - fixed constant on rhs. g() always executed. g(); if (b = a) // case 2 - variable on rhs. h() might be executed. h(); } then GNU C++ 4.2 snapshot says dcb@linux:~/C++/Alphasrc> ~/gnu/42-20060114/results/bin/g++ -c -Wall jan15b.cc jan15b.cc: In function 'void f(int, int)': jan15b.cc:6: warning: suggest parentheses around assignment used as truth value jan15b.cc:8: warning: suggest parentheses around assignment used as truth value dcb@linux:~/C++/Alphasrc> We can see the compiler fails to distinguish case 1 and case 2. Suggest enhance the compiler to say something different for case 1. Fixed constant on rhs is much more likely to be a programmer error, IMHO. Here is Intel C++ 9.0 doing what I want jan15b.cc(6): warning #187: use of "=" where "==" may have been intended if (b = 1) ^
Both of these are questionable, I don't see why there should be a different diagnostic.
(In reply to comment #1) > Both of these are questionable, I don't see why there should be a different > diagnostic. Because the two different diagnostics make it easier to grep for. For example, Intel C++ compiler finds case 1, but says nothing about case 2. Easy to grep for. GNU C++ produces the same message for case 1 and case 2, leading to a lot of time wasting manual checking of diagnostics. Surely case 1 is much more likely to be programmer error than case 2 ?
I agree with Pinskia here - also the detection of a constant RHS is difficult and will cause followup PRs that we do not catch all cases.
I agree with Richard and Andrew. I don't see why we should issue different diagnostics for essentially the same case. W.