Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Let's have a look at what gcc does:

/tmp>cat gcc-warn-assign.c int main(void) { int i,x,y; i=x=y=0; /* unintentional assignment inside if condition */ if (x=42) i++; if (x=y) i++; /* intentional assignment inside if condition, marked with ext +ra parentheses */ if ((x=42)) i++; if ((x=y)) i++; return i; } /tmp>gcc -Wall -pedantic gcc-warn-assign.c gcc-warn-assign.c: In function ‘main’: gcc-warn-assign.c:6:2: warning: suggest parentheses around assignment +used as truth value gcc-warn-assign.c:7:2: warning: suggest parentheses around assignment +used as truth value /tmp>gcc --version gcc (GCC) 4.5.2 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There i +s NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR +POSE. /tmp>

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.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^2: No warning when assiging to a variable by afoken
in thread No warning when assiging to a variable by mikeraz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found