|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package modernize |
| 6 | + |
| 7 | +import ( |
| 8 | + "go/ast" |
| 9 | + "go/types" |
| 10 | + "reflect" |
| 11 | + "strconv" |
| 12 | + |
| 13 | + "golang.org/x/tools/go/analysis" |
| 14 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 15 | + "golang.org/x/tools/go/ast/inspector" |
| 16 | + "golang.org/x/tools/internal/astutil" |
| 17 | +) |
| 18 | + |
| 19 | +func checkOmitEmptyField(pass *analysis.Pass, info *types.Info, curField *ast.Field) { |
| 20 | + typ := info.TypeOf(curField.Type) |
| 21 | + _, ok := typ.Underlying().(*types.Struct) |
| 22 | + if !ok { |
| 23 | + // Not a struct |
| 24 | + return |
| 25 | + } |
| 26 | + tag := curField.Tag |
| 27 | + if tag == nil { |
| 28 | + // No tag to check |
| 29 | + return |
| 30 | + } |
| 31 | + // The omitempty tag may be used by other packages besides json, but we should only modify its use with json |
| 32 | + tagconv, _ := strconv.Unquote(tag.Value) |
| 33 | + match := omitemptyRegex.FindStringSubmatchIndex(tagconv) |
| 34 | + if match == nil { |
| 35 | + // No omitempty in json tag |
| 36 | + return |
| 37 | + } |
| 38 | + omitEmptyPos, err := astutil.PosInStringLiteral(curField.Tag, match[2]) |
| 39 | + if err != nil { |
| 40 | + return |
| 41 | + } |
| 42 | + omitEmptyEnd, err := astutil.PosInStringLiteral(curField.Tag, match[3]) |
| 43 | + if err != nil { |
| 44 | + return |
| 45 | + } |
| 46 | + removePos, removeEnd := omitEmptyPos, omitEmptyEnd |
| 47 | + |
| 48 | + jsonTag := reflect.StructTag(tagconv).Get("json") |
| 49 | + if jsonTag == ",omitempty" { |
| 50 | + // Remove the entire struct tag if json is the only package used |
| 51 | + if match[1]-match[0] == len(tagconv) { |
| 52 | + removePos = curField.Tag.Pos() |
| 53 | + removeEnd = curField.Tag.End() |
| 54 | + } else { |
| 55 | + // Remove the json tag if omitempty is the only field |
| 56 | + removePos, err = astutil.PosInStringLiteral(curField.Tag, match[0]) |
| 57 | + if err != nil { |
| 58 | + return |
| 59 | + } |
| 60 | + removeEnd, err = astutil.PosInStringLiteral(curField.Tag, match[1]) |
| 61 | + if err != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + pass.Report(analysis.Diagnostic{ |
| 67 | + Pos: curField.Tag.Pos(), |
| 68 | + End: curField.Tag.End(), |
| 69 | + Category: "omitzero", |
| 70 | + Message: "Omitempty has no effect on nested struct fields", |
| 71 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 72 | + { |
| 73 | + Message: "Remove redundant omitempty tag", |
| 74 | + TextEdits: []analysis.TextEdit{ |
| 75 | + { |
| 76 | + Pos: removePos, |
| 77 | + End: removeEnd, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + Message: "Replace omitempty with omitzero (behavior change)", |
| 83 | + TextEdits: []analysis.TextEdit{ |
| 84 | + { |
| 85 | + Pos: omitEmptyPos, |
| 86 | + End: omitEmptyEnd, |
| 87 | + NewText: []byte(",omitzero"), |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }}) |
| 92 | +} |
| 93 | + |
| 94 | +// The omitzero pass searches for instances of "omitempty" in a json field tag on a |
| 95 | +// struct. Since "omitempty" does not have any effect when applied to a struct field, |
| 96 | +// it suggests either deleting "omitempty" or replacing it with "omitzero", which |
| 97 | +// correctly excludes structs from a json encoding. |
| 98 | +func omitzero(pass *analysis.Pass) { |
| 99 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 100 | + info := pass.TypesInfo |
| 101 | + for curFile := range filesUsing(inspect, info, "go1.24") { |
| 102 | + for curStruct := range curFile.Preorder((*ast.StructType)(nil)) { |
| 103 | + for _, curField := range curStruct.Node().(*ast.StructType).Fields.List { |
| 104 | + checkOmitEmptyField(pass, info, curField) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments