-
Notifications
You must be signed in to change notification settings - Fork 6
False positive for table tests #4
Comments
This is exactly how I write tests as well and I'm seeing the same false positive effect. EDIT: I was looking for a fix for this but I'm not sure what a feasible solution might be. In fact even running the linter on itself ( go run main.go -- main.go
main.go:314:44: Using the variable on range scope "src" in function literal
main.go:324:38: Using the variable on range scope "src" in function literal
Found 2 lint problems; failing.
exit status 1 Both lines are related to variables used in a function ( I'm not too familiar with the Shorter example to test: package main
func main() {
for i := range make([]int, 1) {
f := func() int { return i }
f()
}
} This will work since package main
func main() {
for i := range make([]int, 1) {
func() int { return i }()
}
} |
We also should take into account call of |
Sorry, I don't have any solutions for this. |
I have a slightly different opinion on this: for _, tc := range cases {
tc := tc
t.Run(tc.description, func(t *testing.T) { is preferred atm because it acts as a constant reminder of https://github.com/golang/go/wiki/CommonMistakes . Consistency in code and treatment of the language is a feature, not a bug, and special-casing this quirk - regardless of what your feelings on this irritation may be - is actually harmful to developers. Where it's now idiomatic to explicitly test for TL;DR: there is a huge developer value in embracing consistency because the mental overhead for consistent application of rules should be preferred to exotic understanding of language quirks. (I can't wait for this implementation detail to actually be handled by the language, but until then...) |
@jirfag this is actually not correct as
|
It is okay that linters produce false positives from time to time, but we should be able to explicitly disable the warning with some comment, e.g. |
if you're using golangci-lint add this line to your issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
exclude:
- Using the variable on range scope `tc` in function literal |
Thanks for your suggestion. After tried, I have done with below one. issues:
exclude:
- Using the variable on range scope .* in function literal |
* Integrate golangci-lint to easily extend linting capabilities later - that's why separate fmt, vet, etc. targets are dropped and all are run through umbrella lint target, golangci-lint. * Additionally enabled scopelint and fixed one issue - it's disabled for test files because it gives false positive - kyoh86/scopelint#4. * Misspell on go files are also run by golangci-lint, that's why `ALL_SRC_AND_DOC` is simplified to `ALL_DOC`. Fixes #342
The linter returns false positives when being run on the following test code:
In this case since the testing closure is executed immediately in the same iteration, the mistake discussed here: https://github.com/golang/go/wiki/CommonMistakes is not relevant.
More information can be found on golangci/golangci-lint#281.
The text was updated successfully, but these errors were encountered: