The problem is that "if (a=b)" is a perfectly legal statement. It just that it doesn't do what the programmer expected.
There actually is a compiler switch that will cause this to issue a "warning" statement, but: the warning is likely to appear pretty cryptic to beginning programmers and other warnings that may or may not be relevant are also likely to appear.
Actually it doesn't always return true. Whatever is "assigned" by the = sign is evaluated by the IF. So
if (myVar = false)
… assigns false to myVar and does not trigger the if statement.
I understand that it is legal C. And, I understand that you wouldn't do it on purpose.
However, this is one of those gotchas I think deserves a complier "Warning." Personally, I can't think of a single time I have ever used assignment inside of an if statement. And if I had, I wouldn't be offended if the complier simply asked "Hey, did you mean to do this?!"
The opposite deserves some attention as well. Equality outside of if (or other conditional) statements should throw a Warning as well.
At the very least, when a sketch doesn't work if a couple of warnings like these appeared in the IDE it would beg the "uh huh?" question.
I think I should add that although my above example has fewer lines it's actually less clear to a reader, and therefore I think it's fair to say that it's not good coding practice.
Richard, I completely agree with you on #1, but more importantly on #2.
The "value-add" of Arduino is making things easier for "everyone-else." As you eloquently state, this would be a user-friendly suggestion. That's all it needs to be.
I think it'd be nice to have a warning (even just something in the text box down below, a "Did you really mean this?" line.
Let it compile anyway, don't pop anything up, that way it can be used either way but those of us who are new to C don't have to spend half an hour (or more ) trying to figure out what is happening.
Well your wish for new or different compiler options are being voiced to the wrong group here. The compiler used by the Arduino is a open sourced project managed by a totaly different group http://gcc.gnu.org/
You could always contact them and see what they think of your request.
I don't think they'll change the GCC compiler just for a group of newbs, if this should be done, it should be handled during verification in the IDE.
The compiler isn't just used by Arduino users, it is a widely used tool by millions of programmers across the world. They couldn't care less about people like us probably
There already is a compiler option for this in GCC (it's part of -Wparentheses). However the IDE disables all warnings (-w). Even in GCC, -Wparentheses is off by default, probably it generates more unhelpful warnings than helpful ones.
Syntax-checking in the IDE is extremely hard to program. You might think it's easy to write a function that finds an = in an if but parsing a string is actually very hard! For example:
if(a=b){....} needs a warning, but
if(a<=b){...} has one =, but is correct, because the = is part of <=
if(c == '='){...} is correct because the == is correct and the single = is in quotes
Perhaps it would be a better suggestion to make the warning options (or even better, all compiler options) settable in preferences. For absolute beginners, warnings off is a good idea, as they tend to be more confused by the warnings than helped.
Even so, it will never be possible to check for all simple mistakes, there are too many other ones (logical-bitwise confusion & / &&, forgetting braces after the if when you need it to control multiple statements, or remembering the braces but accidentally putting a ; before them). You've got to learn to take care over every character when coding in C++.
Well, we could start making a list of the warning options (like -Wparenthesis) that it might make sense to turn on for at least the user sketch part of the compile. Programmer types tend to live in a world of 0s and 1s: -Wall to turn on all warnings (causes lots of output even on correct sketches, some due compiler bugs) or -w (all warnings off.)
Another idea is to add a static analysis step (just to the user sketch. We wouldn't want the core and library authors to have their creativity stifled by having to make their code "technically perfect." (and there are probably things about microcontrollers that SA tools don't understand very well.))