Skip to content

Commit 388561f

Browse files
committed
Propose explicitreturn linter
1 parent 452544a commit 388561f

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ require (
107107
mvdan.cc/gofumpt v0.3.1
108108
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
109109
mvdan.cc/unparam v0.0.0-20220706161116-678bad134442
110+
tildegit.org/indigo/explicitreturn v0.0.0-20220802220915-02f24a7ea1f1
110111
)
111112

112113
require (

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/explicitreturn.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"golang.org/x/tools/go/analysis"
5+
"tildegit.org/indigo/explicitreturn/pkg/analyzer"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewExplicitReturn() *goanalysis.Linter {
11+
a := analyzer.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

pkg/lint/lintersdb/manager.go

+6
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
388388
WithLoadForGoAnalysis().
389389
WithURL("https://github.com/GaijinEntertainment/go-exhaustruct"),
390390

391+
linter.NewConfig(golinters.NewExplicitReturn()).
392+
WithSince("v1.48.0").
393+
WithPresets(linter.PresetStyle).
394+
WithLoadForGoAnalysis().
395+
WithURL("https://tildegit.org/indigo/explicitreturn"),
396+
391397
linter.NewConfig(golinters.NewExportLoopRef()).
392398
WithSince("v1.28.0").
393399
WithPresets(linter.PresetBugs).

test/testdata/explicitreturn.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//golangcitest:args -Eexplicitreturn
2+
package testdata
3+
4+
// This is the sort of thing we want to discourage because this will implicitly return x and y
5+
func SimpleNakedReturn() (x int, y string) {
6+
return // ERROR `implicit return found in function SimpleNakedReturn`
7+
}
8+
9+
// If your function doesn't return anything, the linter is happy to let you keep your naked return
10+
// ...even if it is redundant here
11+
func IgnorableProcedure() {
12+
return
13+
}
14+
15+
// Or even no return statement at all
16+
func AnotherIgnorableProcedure() {}
17+
18+
// If you simply return the expected values in your return statement, everyone's happy
19+
func SimpleFunction() (x int, y string) {
20+
return x, y // Return values are as foretold in the function signature
21+
}
22+
23+
// If you have multiple return paths in your function, the linter will only catch the offensive ones
24+
func MultipleReturnPaths() (x int, y string) {
25+
if true {
26+
return x, y // All good here, but...
27+
}
28+
return // ERROR `implicit return found in function MultipleReturnPaths`
29+
}
30+
31+
// Also works on anonymous functions!
32+
var x = func() (x int, y string) { return } // ERROR `implicit return found in anonymous function`
33+
34+
// But we don't require the number of elements in the return statement to match the number in the function signature
35+
func NestedGoodAnonymousFunction() (x int, y string) {
36+
var f = func() (x int, y string) { return x, y } // This is fine
37+
return f() // Your compiler will catch most of the stupid stuff you could try here
38+
}

0 commit comments

Comments
 (0)