Skip to content

Commit ca40633

Browse files
committed
feat: add defer linter
1 parent d03294f commit ca40633

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/Antonboom/nilnil v0.1.1
1010
github.com/BurntSushi/toml v1.2.0
1111
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
12+
github.com/GaijinEntertainment/go-defer v1.2.0
1213
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0
1314
github.com/OpenPeeDeeP/depguard v1.1.1
1415
github.com/alexkohler/prealloc v1.0.0

go.sum

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

pkg/golinters/defer.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package golinters
2+
3+
import (
4+
"github.com/GaijinEntertainment/go-defer/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewDefer() *goanalysis.Linter {
11+
a, err := analyzer.NewAnalyzer()
12+
if err != nil {
13+
linterLogger.Fatalf("defer configuration: %v", err)
14+
}
15+
16+
return goanalysis.NewLinter(
17+
a.Name,
18+
a.Doc,
19+
[]*analysis.Analyzer{a},
20+
nil,
21+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
22+
}

pkg/lint/lintersdb/manager.go

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

407+
linter.NewConfig(golinters.NewDefer()).
408+
WithSince("v1.51.0").
409+
WithPresets(linter.PresetStyle).
410+
WithLoadForGoAnalysis().
411+
WithURL("https://github.com/GaijinEntertainment/go-defer"),
412+
407413
linter.NewConfig(golinters.NewExportLoopRef()).
408414
WithSince("v1.28.0").
409415
WithPresets(linter.PresetBugs).

test/testdata/defer.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//golangcitest:args -Edefer
2+
package testdata
3+
4+
func funcReturnsInt() int {
5+
return 1
6+
}
7+
8+
func funcReturnsFunc() func() {
9+
return func() {
10+
}
11+
}
12+
13+
func funcReturnsErrAndFunc() (error, func()) {
14+
return nil, func() {
15+
}
16+
}
17+
18+
func funcDeferReturnGoodValue() {
19+
defer funcReturnsInt()
20+
}
21+
22+
func funcDeferReturnFunc() {
23+
defer funcReturnsFunc() // want "deferred call should not return a function"
24+
}
25+
26+
func funcDeferReturnErrAndFunc() {
27+
defer funcReturnsErrAndFunc() // want "deferred call should not return a function"
28+
}
29+
30+
func funcDeferAnonymousReturnFunc() {
31+
defer func() func() { // want "deferred call should not return a function"
32+
return func() {}
33+
}()
34+
}
35+
36+
func funcDeferAnonymousReturnErrAndFunc() {
37+
defer func() (error, func()) { // want "deferred call should not return a function"
38+
return nil, func() {}
39+
}()
40+
}

0 commit comments

Comments
 (0)