Closed kolyshkin closed 3 years ago
Similarly, for existing code in errorlint/testdata/src/allowed/allowed.go
, the following change results in a warning.
@@ -113,7 +114,9 @@ type CompressedFile struct {
}
func (c CompressedFile) Read(p []byte) (int, error) {
- n, err := c.reader.Read(p)
+ var n int
+ var err error
+ n, err = c.reader.Read(p)
if err == io.EOF {
return n, io.EOF
}
Happens because callExpr
in isAllowedErrorComparison
is nil
. This is where my debug abilities stop 🤡
The allowed error set is implemented by checking comparisons for whether an error is returned from a known safe function.
The current implementation uses the declaration of the error variable to see whether it comes from such a function. In your case, the declaration is a variable declaration without initialisation for which no logic is implemented yet.
We can fix this by not only checking the declaration, but also the last assignment to the error variable. Checking only the last assignment also requires understanding the control flow, which would make this linter way more complex than I am able to support. So I would propose an implementation which just checks all assigning expressions instead.
Will do some hacking when I have time, unless someone beats me to it :P
@kolyshkin I have pushed a fix to branch issue-15
. Could you please test whether this solves your issue?
I have done some more testing on my own projects and it seems to work well. Merged my changes :)
I have previously submitted #12 hoping it would fix to eliminate a bogus error in my code.
Today I found it did not.
The following code is fine (i.e. no warnings):
but if we modify it in this way:
we will have something like
and this code results in a warning:
Alas, I don't know internals good enough to try to fix it.