Bug 25808 - constant on rhs of conditional assignment
Summary: constant on rhs of conditional assignment
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-16 14:23 UTC by David Binderman
Modified: 2006-03-08 05:09 UTC (History)
2 users (show)

See Also:
Host: x86_64-suse-linux
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2006-01-16 14:23:28 UTC
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)
            ^
Comment 1 Andrew Pinski 2006-01-16 15:27:50 UTC
Both of these are questionable, I don't see why there should be a different diagnostic.
Comment 2 David Binderman 2006-01-16 22:28:49 UTC
(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 ?
Comment 3 Richard Biener 2006-01-17 11:51:51 UTC
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.
Comment 4 Wolfgang Bangerth 2006-03-08 05:09:22 UTC
I agree with Richard and Andrew. I don't see why we should issue different
diagnostics for essentially the same case. 

W.