Skip to content

Commit 2fd1791

Browse files
authored
Align threshold linters with sibling analyzer behavior (#40740)
1 parent 7f30df9 commit 2fd1791

5 files changed

Lines changed: 95 additions & 1 deletion

File tree

pkg/linters/excessivefuncparams/excessivefuncparams.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"golang.org/x/tools/go/analysis/passes/inspect"
1010

1111
"github.com/github/gh-aw/pkg/linters/internal/astutil"
12+
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
13+
"github.com/github/gh-aw/pkg/linters/internal/nolint"
1214
)
1315

1416
// DefaultMaxParams is the default maximum number of parameters allowed in a function declaration.
@@ -36,6 +38,7 @@ func run(pass *analysis.Pass) (any, error) {
3638
if err != nil {
3739
return nil, err
3840
}
41+
noLintLinesByFile := nolint.BuildLineIndex(pass, "excessivefuncparams")
3942

4043
nodeFilter := []ast.Node{
4144
(*ast.FuncDecl)(nil),
@@ -49,6 +52,10 @@ func run(pass *analysis.Pass) (any, error) {
4952
if fn.Type == nil || fn.Type.Params == nil {
5053
return
5154
}
55+
position := pass.Fset.PositionFor(fn.Name.Pos(), false)
56+
if filecheck.IsTestFile(position.Filename) {
57+
return
58+
}
5259

5360
params := 0
5461
for _, field := range fn.Type.Params.List {
@@ -60,6 +67,9 @@ func run(pass *analysis.Pass) (any, error) {
6067
}
6168

6269
if params > maxParams {
70+
if nolint.HasDirective(position, noLintLinesByFile) {
71+
return
72+
}
6373
pass.ReportRangef(
6474
fn.Name,
6575
"%s has %d parameters (limit: %d); consider using an options struct",

pkg/linters/excessivefuncparams/testdata/src/a/a.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ func fewParams(a, b, c int) {}
77
// tooManyParams has more than 8 parameters and should be flagged.
88
func tooManyParams(a, b, c, d, e, f, g, h, i int) { // want `tooManyParams has 9 parameters \(limit: 8\); consider using an options struct`
99
}
10+
11+
//nolint:excessivefuncparams
12+
func suppressedTooManyParams(a, b, c, d, e, f, g, h, i int) {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package a
2+
3+
import "testing"
4+
5+
func tooManyParamsTestHelper(
6+
t *testing.T,
7+
a, b, c, d, e, f, g, h int,
8+
) {
9+
_ = t
10+
}

pkg/linters/largefunc/largefunc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/github/gh-aw/pkg/linters/internal/astutil"
1212
"github.com/github/gh-aw/pkg/linters/internal/filecheck"
13+
"github.com/github/gh-aw/pkg/linters/internal/nolint"
1314
)
1415

1516
// DefaultMaxLines is the default maximum number of lines allowed in a function body.
@@ -37,6 +38,7 @@ func run(pass *analysis.Pass) (any, error) {
3738
if err != nil {
3839
return nil, err
3940
}
41+
noLintLinesByFile := nolint.BuildLineIndex(pass, "largefunc")
4042

4143
nodeFilter := []ast.Node{
4244
(*ast.FuncDecl)(nil),
@@ -63,7 +65,8 @@ func run(pass *analysis.Pass) (any, error) {
6365
return
6466
}
6567

66-
if filecheck.IsTestFile(pass.Fset.Position(body.Pos()).Filename) {
68+
position := pass.Fset.PositionFor(reportNode.Pos(), false)
69+
if filecheck.IsTestFile(position.Filename) {
6770
return
6871
}
6972

@@ -73,6 +76,9 @@ func run(pass *analysis.Pass) (any, error) {
7376
lines := end.Line - start.Line - 1
7477

7578
if lines > maxLines {
79+
if nolint.HasDirective(position, noLintLinesByFile) {
80+
return
81+
}
7682
pass.ReportRangef(
7783
reportNode,
7884
"%s is %d lines long (limit: %d); consider breaking it up",

pkg/linters/largefunc/testdata/src/a/a.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,68 @@ func largeFunc() { // want `largeFunc is 61 lines long \(limit: 60\); consider b
7171
_ = 60
7272
_ = 61
7373
}
74+
75+
//nolint:largefunc
76+
func suppressedLargeFunc() {
77+
_ = 1
78+
_ = 2
79+
_ = 3
80+
_ = 4
81+
_ = 5
82+
_ = 6
83+
_ = 7
84+
_ = 8
85+
_ = 9
86+
_ = 10
87+
_ = 11
88+
_ = 12
89+
_ = 13
90+
_ = 14
91+
_ = 15
92+
_ = 16
93+
_ = 17
94+
_ = 18
95+
_ = 19
96+
_ = 20
97+
_ = 21
98+
_ = 22
99+
_ = 23
100+
_ = 24
101+
_ = 25
102+
_ = 26
103+
_ = 27
104+
_ = 28
105+
_ = 29
106+
_ = 30
107+
_ = 31
108+
_ = 32
109+
_ = 33
110+
_ = 34
111+
_ = 35
112+
_ = 36
113+
_ = 37
114+
_ = 38
115+
_ = 39
116+
_ = 40
117+
_ = 41
118+
_ = 42
119+
_ = 43
120+
_ = 44
121+
_ = 45
122+
_ = 46
123+
_ = 47
124+
_ = 48
125+
_ = 49
126+
_ = 50
127+
_ = 51
128+
_ = 52
129+
_ = 53
130+
_ = 54
131+
_ = 55
132+
_ = 56
133+
_ = 57
134+
_ = 58
135+
_ = 59
136+
_ = 60
137+
_ = 61
138+
}

0 commit comments

Comments
 (0)