Maybe, but probably not. A language with pointers makes it fairly difficult to detect this kind of lack-of-liveness:
var obj_int = object.as_int();
const obj_int_ptr = &obj_int;
obj_int += 1;
return obj_int_ptr.*;
This is also one of the reasons why taking a pointer to a var
qualifies as mutation.
The specific example here looks easy enough to catch, but that doesn’t generalize, and I suspect that an algorithm which does generalize is superlinear, and compilers can’t afford many of those.
The language needs to strike a balance here, and I think “variable is mutated but the result is never used” is impractical to provide. I’m sure that time will bring more advanced tools for validating Zig code properties, possibly with a bit of language-level support, and that will be the proper tool for catching bugs of this nature.
As long as we retain some way to ‘park’ a var
while working on code, the way _ = &thing;
functions now, I wouldn’t object to “variable is mutated but the result is never used” being a compile error. I suspect that it’s surprisingly difficult and expensive to catch all cases of it, but it does fit the bill for “Compile errors for unused things”, so if it can be accomplished on a practical level I’d be happy for the compiler to catch a bug like that.