@@ -3892,22 +3892,23 @@ func checkJSONTag(pass *analysis.Pass, field *ast.Field, tag string) {
3892
3892
//lint:ignore SA9003 TODO(dh): should we flag empty tags?
3893
3893
if len (tag ) == 0 {
3894
3894
}
3895
+ if i := strings .Index (tag , ",format:" ); i >= 0 {
3896
+ tag = tag [:i ]
3897
+ }
3895
3898
fields := strings .Split (tag , "," )
3896
3899
for _ , r := range fields [0 ] {
3897
3900
if ! unicode .IsLetter (r ) && ! unicode .IsDigit (r ) && ! strings .ContainsRune ("!#$%&()*+-./:<=>?@[]^_{|}~ " , r ) {
3898
3901
report .Report (pass , field .Tag , fmt .Sprintf ("invalid JSON field name %q" , fields [0 ]))
3899
3902
}
3900
3903
}
3901
- var co , cs , ci int
3904
+ options := make ( map [ string ] int )
3902
3905
for _ , s := range fields [1 :] {
3903
3906
switch s {
3904
- case "omitempty" :
3905
- co ++
3906
3907
case "" :
3907
3908
// allow stuff like "-,"
3908
3909
case "string" :
3909
- cs ++
3910
3910
// only for string, floating point, integer and bool
3911
+ options [s ]++
3911
3912
tset := typeutil .NewTypeSet (pass .TypesInfo .TypeOf (field .Type ))
3912
3913
if len (tset .Terms ) == 0 {
3913
3914
// TODO(dh): improve message, call out the use of type parameters
@@ -3924,20 +3925,23 @@ func checkJSONTag(pass *analysis.Pass, field *ast.Field, tag string) {
3924
3925
}
3925
3926
}
3926
3927
}
3927
- case "inline" :
3928
- ci ++
3928
+ case "omitzero" , "omitempty" , "nocase" , " inline" , "unknown " :
3929
+ options [ s ] ++
3929
3930
default :
3930
3931
report .Report (pass , field .Tag , fmt .Sprintf ("unknown JSON option %q" , s ))
3931
3932
}
3932
3933
}
3933
- if co > 1 {
3934
- report . Report ( pass , field . Tag , `duplicate JSON option "omitempty"` )
3935
- }
3936
- if cs > 1 {
3937
- report . Report ( pass , field . Tag , `duplicate JSON option "string"` )
3934
+ var duplicates [] string
3935
+ for option , n := range options {
3936
+ if n > 1 {
3937
+ duplicates = append ( duplicates , option )
3938
+ }
3938
3939
}
3939
- if ci > 1 {
3940
- report .Report (pass , field .Tag , `duplicate JSON option "inline"` )
3940
+ if len (duplicates ) > 0 {
3941
+ sort .Strings (duplicates )
3942
+ for _ , option := range duplicates {
3943
+ report .Report (pass , field .Tag , fmt .Sprintf ("duplicate JSON option %q" , option ))
3944
+ }
3941
3945
}
3942
3946
}
3943
3947
0 commit comments