golang/go

cmd/vet: duplicate struct field tag check does not handle XMLName

Open

#18.256 aperta il 8 dic 2016

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)Go (19.008 fork)batch import
AnalysisNeedsFixhelp wanted

Metriche repository

Star
 (133.883 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

In Go 1.8beta1 a new check to detect duplicate names used in struct field tags was introduced to go vet (CL 16704). This check produces false positive warnings when XML attributes and the special XMLName field name are used:

  • When fields are encoded as XML attributes, a warning is produced when an attribute reuses a name previously used for an element.
type Foo struct {
    First int `xml:"a"
    NoDup int `xml:"a,attr"` // warning about reuse of "a"
}
  • When XMLName is used to set the name of the enclosing struct element, it is treated as a regular struct field.
type Bar struct {
    XMLName xml.Name `xml:"a"`
    NoDup   int      `xml:"a"` // warning about reuse of "a"
}

Both cases are addressed in CL 34070.

The original implementation does not take into account the specific behavior of encoding/xml regarding the XMLName field, and the fix only does so in order to eliminate false positive warnings.

No warning is currently generated for a struct field with XMLName field:

type Foo struct {
    Bar int `xml:"a"`
    Baz struct {
        XMLName xml.Name `xml:"a"`
    }
}

As go vet generates a warning about other naming collisions in tags, it should generate a warning in this case as well.

I am currently working on an implementation.

EDIT: added detailed description of original problem addressed by CL 34070

Guida contributor