pub const E0002_INVALID_ASSIGNMENT: ErrorCode;
Expand description
§E0002: Assignments are not allowed in expression contexts
Assignments like x = y
are statements, not expressions. They cannot be used in places where an expression is expected.
- ✅ If you meant to compare, use
x == y
. - ✅ If you meant to assign, wrap it in a block:
{ x = y }
.
§Example of erroneous code
let f = (x, a) => (x = a);
// autogenerated
§Explanation
Assignment (x = a
) evaluates to the unit type ()
and cannot be used where a value is expected. This includes:
- return values in closures or functions,
if
andwhile
conditions,- or anywhere an actual value is needed.
This helps avoid common mistakes, such as confusing assignment with comparison:
if a = b { } // probably meant `a == b`
§Fixes
§✅ If you meant to compare:
let f = (x, a) => (x == a);
§✅ If you meant to assign a value:
let f = (x, a) => { x = a; };
§✅ Or use let
for rebinding the variable:
let f = (x, a) => { let x = a; x; };