@@ -16,13 +16,6 @@ import (
16
16
"golang.org/x/tools/go/ast/inspector"
17
17
)
18
18
19
- type Mode int
20
-
21
- const (
22
- StandaloneMode Mode = iota
23
- GolangciLintMode
24
- )
25
-
26
19
const msgFormat = "avoid direct access to proto field %s, use %s instead"
27
20
28
21
func NewAnalyzer (cfg * Config ) * analysis.Analyzer {
@@ -35,7 +28,7 @@ func NewAnalyzer(cfg *Config) *analysis.Analyzer {
35
28
Doc : "Reports direct reads from proto message fields when getters should be used" ,
36
29
Flags : flags (cfg ),
37
30
Run : func (pass * analysis.Pass ) (any , error ) {
38
- _ , err := Run (pass , cfg )
31
+ err := Run (pass , cfg )
39
32
return nil , err
40
33
},
41
34
}
@@ -62,14 +55,13 @@ func flags(opts *Config) flag.FlagSet {
62
55
}
63
56
64
57
type Config struct {
65
- Mode Mode // Zero value is StandaloneMode.
66
58
SkipGeneratedBy []string
67
59
SkipFiles []string
68
60
SkipAnyGenerated bool
69
61
ReplaceFirstArgInAppend bool
70
62
}
71
63
72
- func Run (pass * analysis.Pass , cfg * Config ) ([] Issue , error ) {
64
+ func Run (pass * analysis.Pass , cfg * Config ) error {
73
65
skipGeneratedBy := make ([]string , 0 , len (cfg .SkipGeneratedBy )+ 3 )
74
66
// Always skip files generated by protoc-gen-go, protoc-gen-go-grpc and protoc-gen-grpc-gateway.
75
67
skipGeneratedBy = append (skipGeneratedBy , "protoc-gen-go" , "protoc-gen-go-grpc" , "protoc-gen-grpc-gateway" )
@@ -90,7 +82,7 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {
90
82
91
83
compile , err := glob .Compile (s )
92
84
if err != nil {
93
- return nil , fmt .Errorf ("invalid glob pattern: %w" , err )
85
+ return fmt .Errorf ("invalid glob pattern: %w" , err )
94
86
}
95
87
96
88
skipFilesGlobPatterns = append (skipFilesGlobPatterns , compile )
@@ -124,24 +116,16 @@ func Run(pass *analysis.Pass, cfg *Config) ([]Issue, error) {
124
116
125
117
ins := inspector .New (files )
126
118
127
- var issues []Issue
128
-
129
119
filter := NewPosFilter ()
130
120
ins .Preorder (nodeTypes , func (node ast.Node ) {
131
121
report := analyse (pass , filter , node , cfg )
132
122
if report == nil {
133
123
return
134
124
}
135
-
136
- switch cfg .Mode {
137
- case StandaloneMode :
138
- pass .Report (report .ToDiagReport ())
139
- case GolangciLintMode :
140
- issues = append (issues , report .ToIssue (pass .Fset ))
141
- }
125
+ pass .Report (report .ToDiagReport ())
142
126
})
143
127
144
- return issues , nil
128
+ return nil
145
129
}
146
130
147
131
func analyse (pass * analysis.Pass , filter * PosFilter , n ast.Node , cfg * Config ) * Report {
@@ -185,19 +169,6 @@ func analyse(pass *analysis.Pass, filter *PosFilter, n ast.Node, cfg *Config) *R
185
169
}
186
170
}
187
171
188
- // Issue is used to integrate with golangci-lint's inline auto fix.
189
- type Issue struct {
190
- Pos token.Position
191
- Message string
192
- InlineFix InlineFix
193
- }
194
-
195
- type InlineFix struct {
196
- StartCol int // zero-based
197
- Length int
198
- NewString string
199
- }
200
-
201
172
type Report struct {
202
173
node ast.Node
203
174
result * Result
@@ -225,27 +196,13 @@ func (r *Report) ToDiagReport() analysis.Diagnostic {
225
196
}
226
197
}
227
198
228
- func (r * Report ) ToIssue (fset * token.FileSet ) Issue {
229
- msg := fmt .Sprintf (msgFormat , r .result .From , r .result .To )
230
- return Issue {
231
- Pos : fset .Position (r .node .Pos ()),
232
- Message : msg ,
233
- InlineFix : InlineFix {
234
- StartCol : fset .Position (r .node .Pos ()).Column - 1 ,
235
- Length : len (r .result .From ),
236
- NewString : r .result .To ,
237
- },
238
- }
239
- }
240
-
241
199
func skipGeneratedFile (f * ast.File , prefixes []string , skipAny bool ) bool {
242
200
if len (f .Comments ) == 0 {
243
201
return false
244
202
}
245
203
firstComment := f .Comments [0 ].Text ()
246
204
247
- // https://golang.org/s/generatedcode
248
- if skipAny && strings .HasPrefix (firstComment , "Code generated" ) {
205
+ if skipAny && ast .IsGenerated (f ) {
249
206
return true
250
207
}
251
208
0 commit comments