# compiler: add error for unnecessary use of 'var' · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/baabc6013ea4f44082e69375214e76b5d803c5cb)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: baabc6013ea4f44082e69375214e76b5d803c5cb

Requested commit: baabc6013ea4f44082e69375214e76b5d803c5cb

Commit: baabc6013ea4f44082e69375214e76b5d803c5cb

Tree: 00f89e2c48f72b581bedd7b2c8a068d28839368f

Author: mlugg

Committer: mlugg

## Message

```
compiler: add error for unnecessary use of 'var'

When a local variable is never used as an lvalue, we can determine that
`const` would be sufficient for this variable, so emit an error in this
case. More sophisticated checking is unfortunately not possible with
Zig's current analysis model, since whether an lvalue is actually
mutated depends on semantic analysis, in which some code paths may not
be analyzed, so attempting to determine this would result in false
positive compile errors.

It's worth noting that an unfortunate consequence of this is that any
field call `a.b()` will allow `a` to be `var`, even if `b` does not take
a pointer as its first parameter - this is again a necessary compromise
because the parameter type is not known until semantic analysis.

Also update `translate-c` to not trigger these errors. This is done by
replacing the `_ = @TypeOf(x)` emitted with `_ = &x` - the reference
there means that the local is permitted to be `var`. A similar strategy
will be used to prevent compile errors in the behavior tests, where we
sometimes want to force a value to be runtime-known.

Resolves: #224

```

## Parents

- [325e0f5f0e8a9ce2540ec3ec5b7cbbecac15257a](https://git.cafe/gitcafe/zig/commit/325e0f5f0e8a9ce2540ec3ec5b7cbbecac15257a?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/baabc6013ea4f44082e69375214e76b5d803c5cb?format=markdown)
