The various cases are:
1. f := os.Open("file")
2. f, _ := os.Open("file")
3. os.Open("file")
Case 1) is a compiler error (only one lval, 2 expected). Case 2) is
explicitly ignored, so neither the compiler nor go vet should complain.
Case 3) causes no compiler error, and is the one case that would get caught
by your proposal in go vet, right? So the warning would always be in the
3rd case, regardless of how many values are returned by the function, as
long as one of them is an error.
I can see this being annoying in some cases (File.Close() comes to mind, as
it is often in a defer call, without the lval). But then, isn't it go vet's
role to be the verbose, annoying little brother? If I make a parallel with
javascript, jshint (not Douglas Crockford's stricter jslint) is very
customizable and can be configured to complain about the things you want.
I'd like go vet to be that flexible, though honestly I haven't used it
enough to comment on it.
Le vendredi 25 janvier 2013 14:05:18 UTC-5, Greg Ward a écrit :
Hi all --
I blew an hour or so yesterday tracking down a bug that was a simple
case of forgetting to handle an error. I wrote
doSomething(...)
when I should have written
err = doSomething(...)
if err != nil {
return err
}
Oops, my bad. Easy mistake to make, though. It just occurred to me
that "go vet" ought to be able to catch this: if calling a function
that returns just "error", and the result isn't assigned to anything,
complain. The logic would presumably be a little more complex for
multi-return functions, but it should be doable. (Or should it? I have
not looked at the source for "go vet" at all.)
FWIW, I've already rigged things up so my build fails if "go vet"
complains about anything, and that has caught a few errors.
(Incidentally, I discovered yesterday that the compiler complains if
you ignore the return value from 'append()'. So there's something
sorta like precedent for this.)
Greg
--
Greg Ward
http://www.gerg.ca<gr...@gerg.ca <javascript:>> @gergdotca
--