Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Zig seems to behave similarly. This compiles:

  export fn x() i32 {
      var a: i32 = 1; // okay
      a = 2;
      return a;
  }
This does not:

  export fn x() i32 {
      var a: i32 = 1; // unused variable
      var a2: i32 = 2;
      return a2;
  }
> Not that this lint actually achieves that, or even prevents real errors.

I have to agree. If there's one thing I've learned over the years, it's that you can't prevent bad code by enforcing lint errors.



> If there's one thing I've learned over the years, it's that you can't prevent bad code by enforcing lint errors.

I completely disagree, I regularly catch mistakes/bugs in my own code due to enforced linting errors (both locally and in CI), and just in the past few months I can recall numerous instances where CI linting caught bugs in teammates' code too.

There are both false positives and false negatives with linting, but that doesn't make it useless at all.


And there are good and bad lints. If you want a somewhat view on what lints are good and bad go find some issues for Rust that discuss how to categorize a particular lint.


I don't think that's quite the same issue. That `a` is reassigned doesn't make it unused. It is, after all, a variable.

And that's why this issue is kind of hard. It's why I end up block scoping a lot of error code in Go, which is ugly but generally safer.


> I don't think that's quite the same issue.

It is.

> That `a` is reassigned doesn't make it unused. It is, after all, a variable.

But that's exactly why it's bad, "unused variable" is not a super useful signal in the first place, and if you assert that everything must be used then you need to protect against "unused stores" instead.

SSA tells us there is essentially no difference between assigning and reassigning.


> I have to agree. If there's one thing I've learned over the years, it's that you can't prevent bad code by enforcing lint errors.

A lot of simple cases can be handled with linting rules, BUT that, IMO requires a few additional things. Namely, being expression based, rather than statement based, and enforcing something like a hindley milner type system.


Zig will also let you return references to stack allocated objects.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: