Skip to content

Commit f749ab6

Browse files
committed
Add Warn for omitempty
1 parent 2f10a08 commit f749ab6

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/analysis/optionalfields/analyzer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ func (a *analyzer) checkField(pass *analysis.Pass, field *ast.Field, markersAcce
126126
// (such as to be able to marshal an empty object with all possible options shown)
127127
// and when this is desired, there is an option to ignore the omitempty tag.
128128
func (a *analyzer) checkFieldOmitEmpty(pass *analysis.Pass, field *ast.Field, fieldName string, jsonTags extractjsontags.FieldTagInfo) {
129-
if a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyIgnore {
130-
return
131-
}
132-
133-
if !jsonTags.OmitEmpty {
129+
switch {
130+
case jsonTags.OmitEmpty, a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyIgnore:
131+
// Nothing to do, either we have omitempty, or we are ignoring it.
132+
case a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicySuggestFix:
134133
pass.Report(analysis.Diagnostic{
135134
Pos: field.Pos(),
136135
Message: fmt.Sprintf("field %s is optional and should be omitempty", fieldName),
@@ -146,6 +145,8 @@ func (a *analyzer) checkFieldOmitEmpty(pass *analysis.Pass, field *ast.Field, fi
146145
},
147146
},
148147
})
148+
case a.omitEmptyPolicy == config.OptionalFieldsOmitEmptyPolicyWarn:
149+
pass.Reportf(field.Pos(), "field %s is optional and should be omitempty", fieldName)
149150
}
150151
}
151152

pkg/config/linters_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type OptionalFieldsOmitEmpty struct {
178178
// policy determines whether the linter should require omitempty for all optional fields.
179179
// Valid values are "SuggestFix" and "Ignore".
180180
// When set to "SuggestFix", the linter will suggest adding the `omitempty` tag when an optional field does not have it.
181+
// When set to "Warn", the linter will emit a warning if the field does not have the `omitempty` tag.
181182
// When set to "Ignore", and optional field missing the `omitempty` tag will be ignored.
182183
// Note, when set to "Ignore", and a field does not have the `omitempty` tag, this may affect whether the field should be a pointer or not.
183184
Policy OptionalFieldsOmitEmptyPolicy `json:"policy"`
@@ -212,6 +213,9 @@ const (
212213
// OptionalFieldsOmitEmptyPolicySuggestFix indicates that the linter will emit a warning if the field does not have omitempty, and suggest a fix.
213214
OptionalFieldsOmitEmptyPolicySuggestFix OptionalFieldsOmitEmptyPolicy = "SuggestFix"
214215

216+
// OptionalFieldsOmitEmptyPolicyWarn indicates that the linter will emit a warning if the field does not have omitempty.
217+
OptionalFieldsOmitEmptyPolicyWarn OptionalFieldsOmitEmptyPolicy = "Warn"
218+
215219
// OptionalFieldsOmitEmptyPolicyIgnore indicates that the linter will ignore any field missing the omitempty tag.
216220
OptionalFieldsOmitEmptyPolicyIgnore OptionalFieldsOmitEmptyPolicy = "Ignore"
217221
)

pkg/validation/linters_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ func validateOptionFieldsOmitEmpty(oec config.OptionalFieldsOmitEmpty, fldPath *
125125
fieldErrors := field.ErrorList{}
126126

127127
switch oec.Policy {
128-
case "", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicySuggestFix:
128+
case "", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicyWarn, config.OptionalFieldsOmitEmptyPolicySuggestFix:
129129
default:
130-
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("policy"), oec.Policy, fmt.Sprintf("invalid value, must be one of %q, %q or omitted", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicySuggestFix)))
130+
fieldErrors = append(fieldErrors, field.Invalid(fldPath.Child("policy"), oec.Policy, fmt.Sprintf("invalid value, must be one of %q, %q, %q or omitted", config.OptionalFieldsOmitEmptyPolicyIgnore, config.OptionalFieldsOmitEmptyPolicyWarn, config.OptionalFieldsOmitEmptyPolicySuggestFix)))
131131
}
132132

133133
return fieldErrors

0 commit comments

Comments
 (0)