github-vet / github-vet/rangeloop-pointer-findings

petergtz/pegomock: pegomock/watch/watch.go; 36 LoC

Open
#8,120 0 comments 0 reactions 0 assignees View on GitHub
fresh small
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [petergtz/pegomock](https://www.github.com/petergtz/pegomock) at [pegomock/watch/watch.go](https://github.com/petergtz/pegomock/blob/ad2b30ac8fdf061e842e634e861426fcc55a4207/pegomock/watch/watch.go#L68-L103)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable lineParts used in defer or goroutine at line 85

[Click here to see the code in its original context.](https://github.com/petergtz/pegomock/blob/ad2b30ac8fdf061e842e634e861426fcc55a4207/pegomock/watch/watch.go#L68-L103)

Click here to show the 36 line(s) of Go which triggered the analyzer.

```go
for _, lineParts := range linesIn(wellKnownInterfaceListFile) {
lineCmd := kingpin.New("What should go in here", "And what should go in here")
destination := lineCmd.Flag("output", "Output file; defaults to mock__test.go.").Short('o').String()
nameOut := lineCmd.Flag("name", "Struct name of the generated code; defaults to the name of the interface prefixed with Mock").Default(filepath.Base(targetPath) + "_test").String()
packageOut := lineCmd.Flag("package", "Package of the generated code; defaults to the package from which pegomock was executed suffixed with _test").Default(filepath.Base(targetPath) + "_test").String()
selfPackage := lineCmd.Flag("self_package", "If set, the package this mock will be part of.").String()
lineArgs := lineCmd.Arg("args", "A (optional) Go package path + space-separated interface or a .go file").Required().Strings()

_, parseErr := lineCmd.Parse(lineParts)
if parseErr != nil {
fmt.Println("Error while trying to generate mock for line", join(lineParts, " "), ":", parseErr)
continue
}
defer func() {
err := recover()
if err != nil {
if updater.lastErrors[errorKey(*lineArgs)] != fmt.Sprint(err) {
fmt.Println("Error while trying to generate mock for", join(lineParts, " "), ":", err)
updater.lastErrors[errorKey(*lineArgs)] = fmt.Sprint(err)
}
}
}()

util.PanicOnError(util.ValidateArgs(*lineArgs))
sourceArgs, err := util.SourceArgs(*lineArgs)
util.PanicOnError(err)

generatedMockSourceCode, _ := filehandling.GenerateMockSourceCode(sourceArgs, *nameOut, *packageOut, *selfPackage, false, os.Stdout, false)
mockFilePath := filehandling.OutputFilePath(sourceArgs, ".", *destination)
hasChanged := util.WriteFileIfChanged(mockFilePath, generatedMockSourceCode)

if hasChanged || updater.lastErrors[errorKey(*lineArgs)] != "" {
fmt.Println("(Re)generated mock for", errorKey(*lineArgs), "in", mockFilePath)
}
delete(updater.lastErrors, errorKey(*lineArgs))
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: ad2b30ac8fdf061e842e634e861426fcc55a4207

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.