Closed smoser closed 4 years ago
I'm seeing similar problems in code I can't share. I don't think there's an issue with the code.
The code has a function that has this signature:
func (*Obj) InQuery(func(*Query) error) error
Any use of this function's *Query pass-in variable, triggers a misleading scope lint error about the following literal, not the var in question.
I don't believe this function literal is immediately disqualified from scopelint's rules by itself, and it seems to be that way just due to this bug, everything that uses *Query
fails in this way.
Thanks,
Maybe this is a mistake in the analysis of AST. I'm planning a reimplementation using golang.org/x/tools/go/analysis. Once #11 is resolved, I think it will probably resolve this too.
Hi! I see you decided to address this by rewriting the linter completely. Is looppointer
ready to use as a replacement for scopelinter
?
Meanwhile, I'll post the minimal code to reproduce this bug here:
package test
func fn1() {
var i int
for _, i = range []int{} {
_ = i
}
}
func fn2() {
_ = func(b bool) {}
}
test.go:11:13: Using the variable on range scope "bool" in function literal
Found 1 lint problems; failing.
The key part is that range
is used with =
instead of :=
. After such a line, innocent code in the same function or even in a different function causes false positives. In this case, the file triggers a false positive in fn2
only if it follows fn1
; if you swap the two functions, the false positive goes away.
@kyoh86 ^^
This is minimized code to illustrate the issue; we ran into this in production code. Should we switch from scopelint
to looppointer
?
Thanks a lot in advance!
@feldgendler
Sorry, looppointer will have more false-positives.
If you want to switch, I recommend you exportloopref. I want to make it as accurately as possible.
I've hit a false positive with a confusing message as well.
When executed on the program below scopelint complains about hasData()'s use use of scanData:
A few different things can make this warning go away:
mytest.go below: