tools/go/analysis
Daniel Martí 2de7f9bf82 go/analysis/passes/structtag: allow field tag shadowing
In the following piece of code:

	type T1 struct {
		Shadowed string `json:"foo"`
	}
	type T2 struct {
		T1
		Shadowing int `json:"foo"`
	}

encoding/json will encode T2 using T2.Shadowing, ignoring T2.Shadowed
entirely. This can be a useful feature to replace some of T1's fields
when encoding it. Moreover, this feature is already in use in the wild,
even though it's probably never been documented.

This started being a problem, as the structtag pass started walking
through embedded fields a few months ago. To keep it from complaining
about these useful shadowing cases, make it only see duplicate field tag
names if they are at the same embedding level, in which case no
shadowing is happening.

The old code indexed these tags by encoding key and name, using a
[2]string. The new code needs to add a level integer, so start declaring
named types for the map, and use methods to simplify the code further
below. We still use a map pointer, to avoid allocating on every single
struct definition.

Updates golang/go#30846.

Change-Id: Iae53228d4f8bd91584c59dcc982cb1300970bc8f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179360
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2019-06-02 11:28:58 +00:00
..
analysistest go/analysis: add an End field to Diagnostic 2019-05-30 17:14:27 +00:00
cmd/vet go/analysis/passes: fix bugs discovered in std 2019-05-09 15:32:22 +00:00
internal go/analysis: add an End field to Diagnostic 2019-05-30 17:14:27 +00:00
multichecker go/analysis/unitchecker: a main function for vet-lite tools 2018-11-15 16:00:22 +00:00
passes go/analysis/passes/structtag: allow field tag shadowing 2019-06-02 11:28:58 +00:00
singlechecker go/analysis/singlechecker: use Stderr in flag.Usage 2019-02-14 16:35:53 +00:00
unitchecker go/analysis/unitchecker: allow dash in file paths 2019-03-30 18:03:04 +00:00
analysis.go go/analysis: add an End field to Diagnostic 2019-05-30 17:14:27 +00:00
doc.go go/analysis: fix typos and update documentation 2019-01-11 18:03:38 +00:00
validate.go go/analysis: validate: report duplicates among analyzers (roots) 2018-10-11 19:55:00 +00:00