@@ -60,7 +60,7 @@ func New(funcs ...Func) *analysis.Analyzer {
60
60
}
61
61
}
62
62
63
- return run (pass , mainModule , allFuncs )
63
+ return nil , run (pass , mainModule , allFuncs )
64
64
},
65
65
}
66
66
}
@@ -86,43 +86,34 @@ func flags(funcs *[]Func) flag.FlagSet {
86
86
return * fs
87
87
}
88
88
89
- func run (pass * analysis.Pass , mainModule string , funcs map [string ]Func ) ( _ any , err error ) {
89
+ func run (pass * analysis.Pass , mainModule string , funcs map [string ]Func ) error {
90
90
visit := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
91
- filter := []ast.Node {(* ast .CallExpr )(nil )}
92
91
93
- visit .Preorder (filter , func (node ast.Node ) {
94
- if err != nil {
95
- return // there is already an error.
96
- }
92
+ for node := range visit .PreorderSeq ((* ast .CallExpr )(nil )) {
93
+ call := node .(* ast.CallExpr )
97
94
98
- call , ok := node .( * ast. CallExpr )
95
+ callee , ok := typeutil . Callee ( pass . TypesInfo , call ).( * types. Func )
99
96
if ! ok {
100
- return
101
- }
102
-
103
- callee := typeutil .StaticCallee (pass .TypesInfo , call )
104
- if callee == nil {
105
- return
97
+ continue
106
98
}
107
99
108
100
fn , ok := funcs [cutVendor (callee .FullName ())]
109
101
if ! ok {
110
- return
102
+ continue
111
103
}
112
104
113
105
if len (call .Args ) <= fn .ArgPos {
114
- err = fmt .Errorf ("musttag: Func.ArgPos cannot be %d: %s accepts only %d argument(s)" , fn .ArgPos , fn .Name , len (call .Args ))
115
- return
106
+ return fmt .Errorf ("musttag: Func.ArgPos cannot be %d: %s accepts only %d argument(s)" , fn .ArgPos , fn .Name , len (call .Args ))
116
107
}
117
108
118
109
arg := call .Args [fn .ArgPos ]
119
110
if ident , ok := arg .(* ast.Ident ); ok && ident .Obj == nil {
120
- return // e.g. json.Marshal(nil)
111
+ continue // e.g. json.Marshal(nil)
121
112
}
122
113
123
114
typ := pass .TypesInfo .TypeOf (arg )
124
115
if typ == nil {
125
- return
116
+ continue
126
117
}
127
118
128
119
checker := checker {
@@ -132,13 +123,13 @@ func run(pass *analysis.Pass, mainModule string, funcs map[string]Func) (_ any,
132
123
imports : pass .Pkg .Imports (),
133
124
}
134
125
if checker .isValidType (typ , fn .Tag ) {
135
- return
126
+ continue
136
127
}
137
128
138
129
pass .Reportf (arg .Pos (), "the given struct should be annotated with the `%s` tag" , fn .Tag )
139
- })
130
+ }
140
131
141
- return nil , err
132
+ return nil
142
133
}
143
134
144
135
type checker struct {
@@ -176,7 +167,6 @@ func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
176
167
return c .parseStruct (typ .Elem ())
177
168
case * types.Map :
178
169
return c .parseStruct (typ .Elem ())
179
-
180
170
case * types.Named : // a struct of the named type.
181
171
pkg := typ .Obj ().Pkg ()
182
172
if pkg == nil {
@@ -190,10 +180,8 @@ func (c *checker) parseStruct(typ types.Type) (*types.Struct, bool) {
190
180
return nil , false
191
181
}
192
182
return styp , true
193
-
194
183
case * types.Struct : // an anonymous struct.
195
184
return typ , true
196
-
197
185
default :
198
186
return nil , false
199
187
}
@@ -208,15 +196,12 @@ func (c *checker) isValidStruct(styp *types.Struct, tag string) bool {
208
196
209
197
tagValue , ok := reflect .StructTag (styp .Tag (i )).Lookup (tag )
210
198
if ! ok {
211
- // tag is not required for embedded types.
212
199
if ! field .Embedded () {
213
- return false
200
+ return false // tag is not required for embedded types.
214
201
}
215
202
}
216
-
217
- // the field is explicitly ignored.
218
203
if tagValue == "-" {
219
- continue
204
+ continue // the field is explicitly ignored.
220
205
}
221
206
222
207
if ! c .isValidType (field .Type (), tag ) {
0 commit comments