You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// The field is not omitempty, and the zero value is valid, the field does not need to be a pointer.
185
185
a.handleFieldShouldNotBePointer(pass, field, fieldName, isPointer, "field %s is optional, without omitempty and allows the zero value. The field does not need to be a pointer.")
186
186
case!hasValidZeroValue:
187
-
// The zero value would not be accepted, so the field needs to have omitempty.
188
-
// Force the omitempty policy to suggest a fix. We can only get to this function when the policy is configured to Ignore.
189
-
// Since we absolutely have to add the omitempty tag, we can report it as a suggestion.
190
-
reportShouldAddOmitEmpty(pass, field, config.OptionalFieldsOmitEmptyPolicySuggestFix, fieldName, "field %s is optional and does not allow the zero value. It must have the omitempty tag.", jsonTags)
187
+
ifisStruct&&hasOmitZero {
188
+
// The struct field need not have omitempty tag if it does not have a valid zero value and has omitzero tag.
189
+
return
190
+
} else {
191
+
// The zero value would not be accepted, so the field needs to have omitempty.
192
+
// Force the omitempty policy to suggest a fix. We can only get to this function when the policy is configured to Ignore.
193
+
// Since we absolutely have to add the omitempty tag, we can report it as a suggestion.
194
+
reportShouldAddOmitEmpty(pass, field, config.OptionalFieldsOmitEmptyPolicySuggestFix, fieldName, "field %s is optional and does not allow the zero value. It must have the omitempty tag.", jsonTags)
195
+
}
191
196
// Once it has the omitempty tag, it will also need to be a pointer in some cases.
192
197
// Now handle it as if it had the omitempty already.
193
198
// We already handle the omitempty tag above, so force the `hasOmitEmpty` to true.
@@ -229,11 +234,16 @@ func (a *analyzer) handleFieldShouldNotBePointer(pass *analysis.Pass, field *ast
0 commit comments