|P is for Practical|
Re^2: No warning when assiging to a variableby afoken (Parson)
|on Aug 16, 2013 at 19:38 UTC||Need Help??|
Let's have a look at what gcc does:
So, what do we see here?
(Ignore i and i++, it's just dummy code.)
Possibly unintentional assignments inside if conditions cause warnings, no matter what is on the RHS of the assignment operator. The RHS may be a constant, or any other expression. It does not matter, and it should not matter.
If you want to write "especially clever" code, you still can assign inside an if condition without warnings, but you must wrap the assignment in semi-redundant parentheses to make your intention -- you want to test the truth of the result of the assignment -- clear to gcc. And the code is still compatible with other C compilers that do not know the extra parentheses trick, they just ignore the redundant parentheses.
I think Perl with warnings enabled should behave the same: Warn if a variable is assigned anything, constant or not, inside the condition of if and friends, because it is likely a bug. And don't warn only if the assignment has extra parentheses indicating someone wanted to write "especially clever" code.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)