Skip to content

Commit 62653fb

Browse files
committed
Simplify return statements in tryReadGoDirective
This makes it easier to reason about what is returned and would have avoided the bug with variable shadowing.
1 parent d30b736 commit 62653fb

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ func getDepMode() DependencyInstallerMode {
269269

270270
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
271271
func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
272-
version := ""
273-
found := false
274272
if depMode == GoGetWithModules {
275273
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
276274
goMod, err := os.ReadFile("go.mod")
@@ -279,14 +277,13 @@ func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
279277
} else {
280278
matches := versionRe.FindSubmatch(goMod)
281279
if matches != nil {
282-
found = true
283280
if len(matches) > 1 {
284-
version = string(matches[1])
281+
return string(matches[1]), true
285282
}
286283
}
287284
}
288285
}
289-
return version, found
286+
return "", false
290287
}
291288

292289
// Returns the appropriate ModMode for the current project

0 commit comments

Comments
 (0)