Skip to content

Warn for assignment in if() upon compilation #8048

@robzach

Description

@robzach

I often see students write if (x = y) when they mean if (x == y). An understandable error; even a prof of C++ taking one of my intro classes made this same mistake in his code and wasn't able to see it.

Apparently gcc can be made to warn of assignments in if tests, by using the -Wparentheses flag at compilation. The warning can be avoided by using an extra pair of parentheses to demonstrate programmer's intention, i.e. if ((x = y)).

GNU's gcc -Wparentheses documentation.

As many users of the Arduino IDE are newer programmers and unlikely to intend assignments this way, I recommend that the warning for assignments inside of if() tests be made visible in the console.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: InvalidOff topic for this repository, or a bug report determined to not actually represent a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions