Skip to content

Commit 6b60cb8

Browse files
ssgregSergey Vilgelm
and
Sergey Vilgelm
authored
new nlreturn linter (#1267)
* new nlreturn linter * fix: import order Co-authored-by: Sergey Vilgelm <sergey.vilgelm@ibm.com>
1 parent 3aa04f5 commit 6b60cb8

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/spf13/cobra v1.0.0
4949
github.com/spf13/pflag v1.0.5
5050
github.com/spf13/viper v1.7.0
51+
github.com/ssgreg/nlreturn/v2 v2.0.1
5152
github.com/stretchr/testify v1.6.1
5253
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2
5354
github.com/tetafro/godot v0.4.8

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
359359
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
360360
github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM=
361361
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
362+
github.com/ssgreg/nlreturn/v2 v2.0.1 h1:+lm6xFjVuNw/9t/Fh5sIwfNWefiD5bddzc6vwJ1TvRI=
363+
github.com/ssgreg/nlreturn/v2 v2.0.1/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I=
362364
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
363365
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
364366
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/golinters/nlreturn.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/ssgreg/nlreturn/v2/pkg/nlreturn"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewNLReturn() *goanalysis.Linter {
11+
return goanalysis.NewLinter(
12+
"nlreturn",
13+
"nlreturn checks for a new line before return and branch statements to increase code clarity",
14+
[]*analysis.Analyzer{
15+
nlreturn.NewAnalyzer(),
16+
},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
301301
WithPresets(linter.PresetBugs).
302302
WithLoadForGoAnalysis().
303303
WithURL("https://github.com/ryanrolds/sqlclosecheck"),
304+
linter.NewConfig(golinters.NewNLReturn()).
305+
WithPresets(linter.PresetStyle).
306+
WithLoadForGoAnalysis().
307+
WithURL("https://github.com/ssgreg/nlreturn"),
304308
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
305309
linter.NewConfig(golinters.NewNoLintLint()).
306310
WithPresets(linter.PresetStyle).

test/testdata/nlreturn.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//args: -Enlreturn
2+
package testdata
3+
4+
func cha() {
5+
ch := make(chan interface{})
6+
ch1 := make(chan interface{})
7+
ch2 := make(chan interface{})
8+
9+
select {
10+
case <-ch:
11+
return
12+
13+
case <-ch1:
14+
{
15+
a := 1
16+
_ = a
17+
{
18+
a := 1
19+
_ = a
20+
return // ERROR "return with no blank line before"
21+
}
22+
23+
return
24+
}
25+
26+
return
27+
28+
case <-ch2:
29+
{
30+
a := 1
31+
_ = a
32+
return // ERROR "return with no blank line before"
33+
}
34+
return // ERROR "return with no blank line before"
35+
}
36+
}
37+
38+
func baz() {
39+
switch 0 {
40+
case 0:
41+
a := 1
42+
_ = a
43+
fallthrough // ERROR "fallthrough with no blank line before"
44+
case 1:
45+
a := 1
46+
_ = a
47+
break // ERROR "break with no blank line before"
48+
case 2:
49+
break
50+
}
51+
}
52+
53+
func foo() int {
54+
v := []int{}
55+
for range v {
56+
return 0
57+
}
58+
59+
for range v {
60+
for range v {
61+
return 0
62+
}
63+
return 0 // ERROR "return with no blank line before"
64+
}
65+
66+
o := []int{
67+
0, 1,
68+
}
69+
70+
return o[0]
71+
}
72+
73+
func bar() int {
74+
o := 1
75+
if o == 1 {
76+
if o == 0 {
77+
return 1
78+
}
79+
return 0 // ERROR "return with no blank line before"
80+
}
81+
82+
return o
83+
}
84+
85+
func main() {
86+
return
87+
}
88+
89+
func bugNoAssignSmthHandling() string {
90+
switch 0 {
91+
case 0:
92+
o := struct {
93+
foo string
94+
}{
95+
"foo",
96+
}
97+
return o.foo // ERROR "return with no blank line before"
98+
99+
case 1:
100+
o := struct {
101+
foo string
102+
}{
103+
"foo",
104+
}
105+
106+
return o.foo
107+
}
108+
109+
return ""
110+
}
111+
112+
func bugNoExprSmthHandling(string) {
113+
switch 0 {
114+
case 0:
115+
bugNoExprSmthHandling(
116+
"",
117+
)
118+
return // ERROR "return with no blank line before"
119+
120+
case 1:
121+
bugNoExprSmthHandling(
122+
"",
123+
)
124+
125+
return
126+
}
127+
}
128+
129+
func bugNoDeferSmthHandling(string) {
130+
switch 0 {
131+
case 0:
132+
defer bugNoDeferSmthHandling(
133+
"",
134+
)
135+
return // ERROR "return with no blank line before"
136+
137+
case 1:
138+
defer bugNoDeferSmthHandling(
139+
"",
140+
)
141+
142+
return
143+
}
144+
}
145+
146+
func bugNoGoSmthHandling(string) {
147+
switch 0 {
148+
case 0:
149+
go bugNoGoSmthHandling(
150+
"",
151+
)
152+
return // ERROR "return with no blank line before"
153+
154+
case 1:
155+
go bugNoGoSmthHandling(
156+
"",
157+
)
158+
159+
return
160+
}
161+
}

0 commit comments

Comments
 (0)