A colleague prefers Yoda Conditionals:
if (0 == x) // as a safer alternative to (x == 0), to prevent the typo (x = 0)
This is a controversial style in the team, and one argument brought up against it is that compilers can consistently emit warnings to detect the buggy pattern if (x = 0).
However, MSVC doesn't appear to detect this pattern for classes (https://godbolt.org/z/qqo9ee5n5):
struct wrapper {
int x;
operator int();
};
void foo(wrapper w, int x) {
if (w = wrapper{}) {} // OK ?!
if (x = 0) {} // warning C4706: assignment within conditional expression
}
Am I doing something wrong? Is this a known bug? I have enabled /Wall, but maybe there is some other flag I am missing. IntelliSense also doesn't detect this.
If there is no way to detect this with MSVC and IntelliSense, how can I flag this automatically?
Note: I am aware that if this was compiled in a CI with multiple compilers, one of them ought to catch it. However, the goal is to detect this issue locally (ideally with a linter).
Update: it's becoming clear that I haven't simply missed some flag, so I have submitted a bug report in the Microsoft Developer Community.