4 ms·
The genie is out of the bottle already, gcc quite sensibly will warn you if it sees an assignment operator in an if statement. I'm sure it's saved countless man
by chris_wot 11y ago
The genie is out of the bottle already, gcc quite sensibly will warn you if it sees an assignment operator in an if statement. I'm sure it's saved countless man hours and pretty awful bugs since it was introduced :-)
- ifdefdebug 11y agoI hope it doesn't warn for things like if(0 != (i = getValue()))
- chris_wot 11y agoI hope it does!
- PeCaN 11y agoWhy would that be a warning? It's quite clear what that code is doing and is a reasonably common C idiom.
- tspiteri 11y agoIt doesn't since the assignment is in brackets. You can also write if ((i = getValue()))
- deleted 11y ago[deleted]
- sigjuice 11y agoWhy not be absolutely sure? if (0 != (0 != (i = getValue())))
- ifdefdebug 11y agoI am aware of the redundancy, this was about the new warnings. If you can't do if(i = getValue()) without warning, I was wondering if assignments as a part of a larger expression would pass.
- PeCaN 11y agoSpecifically, it doesn't warn you if the assignment statement is in parenthesis, so while ((xs = xs->tail)) doesn't warn. I really like how GCC handles that.
- chris_wot 11y agoI just noticed this comment. OK, you've definitely got a good point. That is elegant.