Hacker News new | ask | show | jobs
by pikzel 378 days ago
They still haven't solved shadowing.

  a, err := foo()
  b, err := bar()
  if err != nil { // oops, forgot to handle foo()'s err }
This is the illusion of safe error handling.
3 comments

I would be astonished if there isn't an automated tool to check for that at the push of a button. I would be mildly surprised if there isn't a compiler flag to check for it.
Not a compiler check, but staticcheck is widely used:

    % staticcheck test.go
    test.go:7:2: this value of err is never used (SA4006)
Yeah, as one data point, https://staticcheck.dev/docs/checks/#SA4006 has existed since 2017.
There very much is not. There is a compiler error you can’t disable if a variable is completely unused and that is it.
I’m surprised I don’t see this mentioned more. This is spooky action at a distance at its worst. And it’s not even limited to error handling. Any multi value assignment works like this.
It's fairly obvious when writing Go that `err` is being shadowed and needs to be checked after each expression. You should be wrapping them anyways!