I see this request come up a lot
and lots of bashing back and forth between the novices wanting
the tools to catch their mistakes and the purists saying
just learn how to better use the tools.
Because "arduino" is not a language, and the IDE simply passes
the sketch to gcc for C++ parsing, it is the compiler that must check
for this condition.
Good news is that there is already a compiler option to do this.
(Has been for like 20+ years in gcc.....)
Currently, it is -Wparentheses
If you simply use this option to the compiler, then if you do something like:
if( a = 50)
if (a == 50)
or even with dual variables like
if( a = b)
You will get a warning like:bs.cpp:25: warning: suggest parentheses around assignment used as truth value
If you really need to do the assignment inside the if, you can avoid the warning by
if( (a = 50) )
To me this actually emphasizes the assignment,
which seems like a good thing for clarity.
Now if the Arduino guys would just let you set the compiler flags.......
(but thats another issue)
Now for the really bad news. Nobody and and I mean nobody has seen a warning
from the compiler using the IDE. The IDE explicitly disables ALL warnings from the compiler!!
(again you'll have to ask the arduino team why they decided to do this)
For now, you can go in and hack the IDE java code and rebuild
the IDE yourself: (the beauty of open source)
It is in app/src/processing/app/debug/Compiler.java around line #443
You will need to remove the -w option which suppresses all warnings and
replace it with -Wparentheses and rebuild the IDE.
It does work. I've tested it.
But be prepared to get many warnings because none of the code has been built
with warnings enabled for quite some time now,
So you will see that lots of the code out there (including some of the core arduino code) has slight
issues that generate warnings.
But you will actually get a warning for the dreaded "accidental" assignment
inside an if statement.