MCPcopy Create free account
hub / github.com/google/go-github / checkAndReportInvalidTypesForOmitzero

Function checkAndReportInvalidTypesForOmitzero

tools/structfield/structfield.go:166–236  ·  view source on GitHub ↗
(structName, tagType, goFieldName string, fieldType ast.Expr, tokenPos token.Pos, pass *analysis.Pass)

Source from the content-addressed store, hash-verified

164}
165
166func checkAndReportInvalidTypesForOmitzero(structName, tagType, goFieldName string, fieldType ast.Expr, tokenPos token.Pos, pass *analysis.Pass) bool {
167 switch ft := fieldType.(type) {
168 case *ast.StarExpr:
169 // Check for *[]T where T is builtin - should be []T
170 if arrType, ok := ft.X.(*ast.ArrayType); ok {
171 if ident, ok := arrType.Elt.(*ast.Ident); ok && isBuiltinType(ident.Name) {
172 const msg = "change the %q field type to %q in the struct %q"
173 pass.Reportf(tokenPos, msg, goFieldName, "[]"+ident.Name, structName)
174 } else if starExpr, ok := arrType.Elt.(*ast.StarExpr); ok {
175 // Check for *[]*T - should be []*T
176 if ident, ok := starExpr.X.(*ast.Ident); ok {
177 const msg = "change the %q field type to %q in the struct %q"
178 pass.Reportf(tokenPos, msg, goFieldName, "[]*"+ident.Name, structName)
179 }
180 } else {
181 checkStructArrayType(structName, goFieldName, arrType, tokenPos, pass)
182 }
183 return true
184 }
185 // Check for *int and other pointers to builtin types
186 if ident, ok := ft.X.(*ast.Ident); ok {
187 if isBuiltinType(ident.Name) {
188 const msg = `the %q field in struct %q uses "omitzero" with a primitive type; remove "omitzero" and use only "omitempty" for pointer primitive types"`
189 pass.Reportf(tokenPos, msg, goFieldName, structName)
190 return true
191 }
192 }
193 // Check for *map - should be map
194 if _, ok := ft.X.(*ast.MapType); ok {
195 const msg = "change the %q field type to %q in the struct %q"
196 pass.Reportf(tokenPos, msg, goFieldName, exprToString(ft.X), structName)
197 return true
198 }
199 return true
200 case *ast.MapType:
201 return true
202 case *ast.ArrayType:
203 if tagType == "url" {
204 const msg = "the %q field in struct %q uses unsupported omitzero tag for URL tags"
205 pass.Reportf(tokenPos, msg, goFieldName, structName)
206 return true
207 }
208 checkStructArrayType(structName, goFieldName, ft, tokenPos, pass)
209 return true
210 case *ast.Ident:
211 if obj := pass.TypesInfo.ObjectOf(ft); obj != nil {
212 switch obj.Type().Underlying().(type) {
213 case *types.Struct:
214 // For Struct - should be *Struct
215 const msg = "change the %q field type to %q in the struct %q"
216 pass.Reportf(tokenPos, msg, goFieldName, "*"+ft.Name, structName)
217 return true
218 case *types.Basic:
219 if tagType == "url" {
220 const msg = "the %q field in struct %q uses unsupported omitzero tag for URL tags"
221 pass.Reportf(tokenPos, msg, goFieldName, structName)
222 return true
223 }

Callers 1

checkGoFieldTypeFunction · 0.85

Calls 3

isBuiltinTypeFunction · 0.85
checkStructArrayTypeFunction · 0.85
exprToStringFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…