aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vet/testdata
AgeCommit message (Collapse)Author
2025-11-14std: fix printf("%q", int) mistakesAlan Donovan
For #72850 Change-Id: I07e64f05c82a34b1dadb9a72e16f5045e68cbd24 Reviewed-on: https://go-review.googlesource.com/c/go/+/720642 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-10-10cmd/go: unify "go fix" and "go vet"Alan Donovan
This change unifies the fix and vet subcommands; they use the same run function, action graph, and external tool (-vettool for go vet and -fixtool for go fix). go fix runs the tool with the -fix flag, whereas although go vet also supports -fix, it is not the default. The two tools have different (overlapping) suites of analyzers. The high-level parts are fully parameterized over the vet/fix distinction; the lower-level parts (the action graph) continue to use only the "vet" terminology. The cmd/{vet,fix} executable is referred to as the "tool". The tool is generally invoked in -json mode, regardless of whether -json was requested, so that the tool produces a cacheable JSON blob on stdout. When the go user did not request -json, this blob is parsed and printed to stderr by logic in the go vet command. (Formerly the tool would print diagnostics to stderr, but this interacts poorly with the build cache.) go fix's legacy -fix=fixer,... flag is now a no-op that prints a warning that the flag is obsolete. The unitchecker's -c=n flag (to display n lines of context around each diagnostic) is reimplemented in go vet based on the JSON information, to avoid reliance on the stderr output of the tool. cmd/fix is added to dist's prebuilt set of tools since go fix cannot build it dynamically (though ideally it would). Updates #71859 For #75432 Change-Id: I0a84746720b59d05d662ed57826747c5598dca44 Reviewed-on: https://go-review.googlesource.com/c/go/+/700795 Reviewed-by: Michael Matloob <matloob@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Michael Matloob <matloob@golang.org> TryBot-Bypass: Alan Donovan <adonovan@google.com>
2025-08-13all: update vendored dependencies [generated]Dmitri Shuralyov
The tree has opened for Go 1.26 development. This is a time to update all golang.org/x/... module versions that contribute packages to the std and cmd modules in the standard library to latest master versions. For #36905. [git-generate] go install golang.org/x/build/cmd/updatestd@latest go install golang.org/x/tools/cmd/bundle@latest updatestd -goroot=$(pwd) -branch=master # Update a cmd/vet test case. patch <<EOF --- src/cmd/vet/testdata/assign/assign.go +++ src/cmd/vet/testdata/assign/assign.go @@ -18 +18 @@ func (s *ST) SetX(x int, ch chan int) { - x = x // ERROR "self-assignment of x to x" + x = x // ERROR "self-assignment of x" @@ -20 +20 @@ func (s *ST) SetX(x int, ch chan int) { - s.x = s.x // ERROR "self-assignment of s.x to s.x" + s.x = s.x // ERROR "self-assignment of s.x" @@ -22 +22 @@ func (s *ST) SetX(x int, ch chan int) { - s.l[0] = s.l[0] // ERROR "self-assignment of s.l.0. to s.l.0." + s.l[0] = s.l[0] // ERROR "self-assignment of s.l.0." EOF Change-Id: I3fc77d49fa7b47803d363287910b0e37bedefb60 Reviewed-on: https://go-review.googlesource.com/c/go/+/694536 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
2025-04-23cmd/vet: add hostport analyzerAlan Donovan
+ test, release note Fixes #28308 Change-Id: I190e2fe513eeb6b90b0398841f67bf52510b5f59 Reviewed-on: https://go-review.googlesource.com/c/go/+/667596 Auto-Submit: Alan Donovan <adonovan@google.com> Commit-Queue: Alan Donovan <adonovan@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-04-01cmd/vendor: update golang.org/x/tools to v0.31.1-0.20250328151535-a857356d5cc5Alan Donovan
Also, sys@v0.31.1. Updates #18022 Change-Id: I15a6d1979cc1e71d3065bc50f09dc8d3f6c6cdc0 Reviewed-on: https://go-review.googlesource.com/c/go/+/661518 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Commit-Queue: Alan Donovan <adonovan@google.com>
2025-02-12vet: add regression test for printf checker regressionAlan Donovan
Updates golang/go#68796 Change-Id: I5bfa80216deff76fd9aabe9882f93fbfaebef957 Reviewed-on: https://go-review.googlesource.com/c/go/+/610737 Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org>
2025-02-12all: update vendored dependencies [generated]Dmitri Shuralyov
The tree has opened for Go 1.25 development. This is a time to update all golang.org/x/... module versions that contribute packages to the std and cmd modules in the standard library to latest master versions. For #36905. [git-generate] go install golang.org/x/build/cmd/updatestd@latest go install golang.org/x/tools/cmd/bundle@latest updatestd -goroot=$(pwd) -branch=master # Update a cmd/vet test case. cat <<EOF | patch diff --git a/src/cmd/vet/testdata/print/print.go b/src/cmd/vet/testdata/print/print.go index a2ad0f1298..fffe571163 100644 --- a/src/cmd/vet/testdata/print/print.go +++ b/src/cmd/vet/testdata/print/print.go @@ -200,8 +200,8 @@ func PrintfTests() { // Bad argument reorderings. Printf("%[xd", 3) // ERROR "Printf format %\[xd is missing closing \]" Printf("%[x]d x", 3) // ERROR "Printf format has invalid argument index \[x\]" - Printf("%[3]*s x", "hi", 2) // ERROR "Printf format has invalid argument index \[3\]" - _ = fmt.Sprintf("%[3]d x", 2) // ERROR "Sprintf format has invalid argument index \[3\]" + Printf("%[3]*s x", "hi", 2) // ERROR "Printf format %\[3\]\*s reads arg #3, but call has 2 args" + _ = fmt.Sprintf("%[3]d x", 2) // ERROR "Sprintf format %\[3\]d reads arg #3, but call has 1 arg" Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // ERROR "Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*" Printf("%[0]s x", "arg1") // ERROR "Printf format has invalid argument index \[0\]" Printf("%[0]d x", 1) // ERROR "Printf format has invalid argument index \[0\]" EOF # Temporarily hold x/net back to leave out CL 643780 because it's # causing an import cycle in net/http's generated h2_bundle.go. cd src sed -i '' 's|"golang.org/x/net/internal/httpcommon"||' net/http/h2_bundle.go go get golang.org/x/net@v0.34.1-0.20250123000230-c72e89d6a9e4 # version before CL 643780 go mod tidy go mod vendor go generate net/http Change-Id: I91967ceb797bbc741af024cd2d2dba29dc558384 Reviewed-on: https://go-review.googlesource.com/c/go/+/648735 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
2024-05-03cmd/vet: add stdversion analyzerAlan Donovan
+ release note, test Fixes #46136 Change-Id: Ib157c5343dde379f8d6f67bdfa64b3f6a6432bab Reviewed-on: https://go-review.googlesource.com/c/go/+/582556 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org>
2024-04-02cmd: update vendored x/tools to 904c6baAlan Donovan
This fixes one of the many obstacles to enabling types.Alias by updating vet. The 'loopclosure' checker (formerly 'rangeloop') no longer reports any findings with go1.22, so the test needed work to ensure that it still runs on files with go1.21 semantics. Updates #65294 Change-Id: I987ff529d4e165b56b7241563c6003e63bf92bb1 Reviewed-on: https://go-review.googlesource.com/c/go/+/575315 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-10-03cmd: add a new analyzer for check missing values after appendcui fliter
If there is no second parameter added during append, there will be no prompt when executing go vet. Add an analyzer to detect this situation Update #60448 Change-Id: If9848835424f310c54e3e9377aaaad4a1516871a Reviewed-on: https://go-review.googlesource.com/c/go/+/498416 Run-TryBot: shuang cui <imcusg@gmail.com> Run-TryBot: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Tim King <taking@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
2023-05-22cmd/vet: add slog checkerJonathan Amsterdam
Add the slog static analysis pass to `go vet`. Vendor in golang.org/x/tools@master to pick up the pass. Tweak a test in slog to avoid triggering the vet check. Change-Id: I55ceac9a4e6876c8385897784542761ea0af2481 Reviewed-on: https://go-review.googlesource.com/c/go/+/496156 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-05-09cmd: go get golang.org/x/tools@8f7fb01dd429 and revendorRuss Cox
go get golang.org/x/tools@8f7fb01dd429 # CL 493619 go mod tidy go mod vendor The goal is to set up for importing the bisect command, for use in tests, in a follow-up CL. This also updates x/sys and x/net, including in std, because x/tools now depends on newer versions of those. Change-Id: I24c283cc165464d9c873ba7a9a4e75a9d02919b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/493596 Reviewed-by: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2023-01-30cmd/vet: add directive analyzerRuss Cox
For #56986, add the new directive analyzer that catches misplaced //go:debug lines. Ran 'go mod vendor' after adding the import in vet to bring in the vendored files. A followup CL will enable it by default in 'go test'. Change-Id: I12c46e292b31bdbf5ceb86ba4474545e78a83a47 Reviewed-on: https://go-review.googlesource.com/c/go/+/462201 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Auto-Submit: Russ Cox <rsc@golang.org>
2021-08-12[dev.typeparams] all: merge master (46fd547) into dev.typeparamsMatthew Dempsky
Conflicts: - src/go/types/check_test.go CL 324730 on dev.typeparams changed the directory paths in TestCheck, TestExamples, and TestFixedbugs and renamed checkFiles to testFiles; whereas CL 337529 on master added a new test case just above them and that used checkFiles. Merge List: + 2021-08-12 46fd547d89 internal/goversion: update Version to 1.18 + 2021-08-12 5805efc78e doc/go1.17: remove draft notice + 2021-08-12 39634e7dae CONTRIBUTORS: update for the Go 1.17 release + 2021-08-12 095bb790e1 os/exec: re-enable LookPathTest/16 + 2021-08-11 dea23e9ca8 src/make.*: make --no-clean flag a no-op that prints a warning + 2021-08-11 d4c0ed26ac doc/go1.17: linker passes -I to extld as -Wl,--dynamic-linker + 2021-08-10 1f9c9d8530 doc: use "high address/low address" instead of "top/bottom" + 2021-08-09 f1dce319ff cmd/go: with -mod=vendor, don't panic if there are duplicate requirements + 2021-08-09 7aeaad5c86 runtime/cgo: when using msan explicitly unpoison cgoCallers + 2021-08-08 507cc341ec doc: add example for conversion from slice expressions to array ptr + 2021-08-07 891547e2d4 doc/go1.17: fix a typo introduced in CL 335135 + 2021-08-06 8eaf4d16bc make.bash: do not overwrite GO_LDSO if already set + 2021-08-06 63b968f4f8 doc/go1.17: clarify Modules changes + 2021-08-06 70546f6404 runtime: allow arm64 SEH to be called if illegal instruction + 2021-08-05 fd45e267c2 runtime: warn that KeepAlive is not an unsafe.Pointer workaround + 2021-08-04 6e738868a7 net/http: speed up and deflake TestCancelRequestWhenSharingConnection + 2021-08-02 8a7ee4c51e io/fs: don't use absolute path in DirEntry.Name doc + 2021-07-31 b8ca6e59ed all: gofmt + 2021-07-30 b7a85e0003 net/http/httputil: close incoming ReverseProxy request body + 2021-07-29 70fd4e47d7 runtime: avoid possible preemption when returning from Go to C + 2021-07-28 9eee0ed439 cmd/go: fix go.mod file name printed in error messages for replacements + 2021-07-28 b39e0f461c runtime: don't crash on nil pointers in checkptrAlignment + 2021-07-27 7cd10c1149 cmd/go: use .mod instead of .zip to determine if version has go.mod file + 2021-07-27 c8cf0f74e4 cmd/go: add missing flag in UsageLine + 2021-07-27 7ba8e796c9 testing: clarify T.Name returns a distinct name of the running test + 2021-07-27 33ff155970 go/types: preserve untyped constants on the RHS of a shift expression + 2021-07-26 840e583ff3 runtime: correct variable name in comment + 2021-07-26 bfbb288574 runtime: remove adjustTimers counter + 2021-07-26 9c81fd53b3 cmd/vet: add missing copyright header Change-Id: Ia80604d24c6f4205265683024e3100769cf32065
2021-08-10[dev.typeparams] cmd: update vendored golang.org/x/tools to 337cebd2c151Cherry Mui
Update vendored golang.org/x/tools repo to pick up CL 339250 for assembly function check for register ABI. This is done with cd GOROOT/cmd go get golang.org/x/tools@master go mod tidy go mod vendor Update cmd/vet tests as the error ouput changes in CL 301949. The error message now includes full package-qualified name. Change-Id: I52dc7223aee9e011214254488bacf02dc5b4c2ef Reviewed-on: https://go-review.googlesource.com/c/go/+/341149 Trust: Cherry Mui <cherryyz@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2021-07-26cmd/vet: add missing copyright header180909
Change-Id: I78942dde77547f91daebe763328f52b4c476ddaf GitHub-Last-Rev: 423f1683fc7db8c1764383cf0a61c54ee21c06f2 GitHub-Pull-Request: golang/go#47334 Reviewed-on: https://go-review.googlesource.com/c/go/+/336434 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Than McIntosh <thanm@google.com>
2021-02-20cmd/vet: update buildtag check for //go:build linesRuss Cox
Brings in golang.org/x/tools@2363391a and adjusts, adds cmd/vet tests accordingly. Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild This brings in the new //go:build checks in cmd/vet. Change-Id: I8a9735cc014171691012b307ec30e94c81aadfe1 Reviewed-on: https://go-review.googlesource.com/c/go/+/240609 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
2019-11-05cmd/vet: add test for loading complex values with a single instructionKeith Randall
Actual fix will be submitted to x/tools and vendored. This is just an end-to-end test for vet after that is done. Update #35264 Change-Id: I1a63f607e7cfa7aafee23c2c081086c276d3c38c Reviewed-on: https://go-review.googlesource.com/c/go/+/204538 Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2019-10-19cmd/vet: re-vendor x/tools upstream for printf verb fixCaleb Spare
This pulls in the x/tools fix from https://go-review.googlesource.com/c/tools/+/202041 so that cmd/vet won't flag %x/%X verbs incorrectly for floating-point and complex types. Fixes #34993 Change-Id: I68d89a19d95fe6ad336e87d12d56f03556974086 Reviewed-on: https://go-review.googlesource.com/c/go/+/202083 Run-TryBot: Caleb Spare <cespare@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2019-02-20cmd/vet: make vet_test module-agnosticBryan C. Mills
vet_test currently uses a custom GOPATH for each test, but it turns out not to be necessary. Updates #30228 Change-Id: Id7a7bf6d759bd94adccf44e197be1728c2f23575 Reviewed-on: https://go-review.googlesource.com/c/163038 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-15cmd/vet: switch to x/tools/go/analysis implementationAlan Donovan
This change deletes the legacy implementation of vet, replacing it with a short main.go that merely selects the desired analyzers and calls into the "unitchecker" implementation vendored from golang.org/x/tools/go/analysis. Unlike the full vet checker (x/tools/go/analysis/cmd/vet), the 'lite' unitchecker cannot also be run standalone (as 'go tool vet' or cmd/vet); it must be invoked by 'go vet'. This design was chosen to avoid vendoring many additional dependencies into GOROOT, in particular go/packages. If go/packages should someday become part of the standard library, there will be considerable opportunity for simplification. This change also patches the vendored analysisflag package (by adding patch.go) so that it fully supports the build system's -V flag protocol. Also: - remove stale internal/unitchecker/ tree (belonged in https://go-review.googlesource.com/c/149778). - move vet legacy flags (-all, -v, -source, -tags) into analysisflags as all drivers will need them, not just unitchecker. I will upstream this change. A sampling of tests from the cmd/vet testsuite have been preserved as a smoke test, to ensure that each analyzer is being run, and for convenience when evaluating changes. Comprehensive tests for each analyzer live upstream in x/tools. The tests have been heavily reduced and reorganized so that they conform to the structure required by 'go vet'. Change-Id: I84b38caeef733e65deb95234b3b87b5f61046def Reviewed-on: https://go-review.googlesource.com/c/149609 Reviewed-by: Russ Cox <rsc@golang.org>
2018-11-13cmd/vet: rewrite method check to use go/typesDaniel Martí
Now that vet can rely on go/types, there's no reason to do extra work to avoid using it. The rewrite lets us get rid of the field list flattening code, as well as the slight verbosity that comes with go/printer. While at it, make the testdata/method.go expected errors be more specific, to make sure that we're not breaking the warnings that are printed. Finally, update whitelist/all.txt, since the reported errors now include qualified types. Change-Id: I760a1b3b1f60e4a478c9dc43bd7f584a8459593e Reviewed-on: https://go-review.googlesource.com/c/148919 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09Revert "cmd/vet: lostcancel: suppress the check in the main.main function"Brad Fitzpatrick
This reverts CL 148758 (commit 5e17ce22ece18571597ef9a214cdfcd461894e9c) Reason for revert: breaks the build. Change-Id: I6ed15b7b8f6b74d84edab9402ddf7ae87a0d0387 Reviewed-on: https://go-review.googlesource.com/c/148817 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-09cmd/vet: lostcancel: suppress the check in the main.main functionAlan Donovan
When main.main returns, the process exits, so there's no need to cancel contexts. This change was initially reviewed as https://go-review.googlesource.com/c/go/+/106915/4 but somehow I messed up and committed patchset 5, which was effectively empty. Change-Id: Ic4250eb6563af9bc734e429aafc7081ca7d0e012 Reviewed-on: https://go-review.googlesource.com/c/148758 Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09cmd/vet: fix printf false negative with nested pointersDaniel Martí
Pointers to compound objects (structs, slices, arrays, maps) are only followed by fmt if the pointer is at the top level of an argument. This is to minimise the chances of fmt running into loops. However, vet did not follow this rule. It likely doesn't help that fmt does not document that restriction well, which is being tracked in #28625. Updates #27672. Change-Id: Ie9bbd9b974eda5ab9a285986d207ef92fca4453e Reviewed-on: https://go-review.googlesource.com/c/147997 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09cmd/vet: fix some pointer false positives in printfDaniel Martí
fmt's godoc reads: For compound objects, the elements are printed using these rules, recursively, laid out like this: struct: {field0 field1 ...} array, slice: [elem0 elem1 ...] maps: map[key1:value1 key2:value2 ...] pointer to above: &{}, &[], &map[] That is, a pointer to a struct, array, slice, or map, can be correctly printed by fmt if the type pointed to can be printed without issues. vet was only following this rule for pointers to structs, omitting arrays, slices, and maps. Fix that, and add tests for all the combinations. Updates #27672. Change-Id: Ie61ebe1fffc594184f7b24d7dbf72d7d5de78309 Reviewed-on: https://go-review.googlesource.com/c/147758 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-10-11cmd/vet: detect non-pointer arguments for unmarshal and decodeIan Davis
Checks usage of Unmarshal and Decode functions in json, gob and xml packages to detect attempts to decode into non-pointer types. Fixes #27564 Change-Id: I07bbd5be82d61834ffde9af9937329d7fb1f05d0 Reviewed-on: https://go-review.googlesource.com/c/139997 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-10-06all: fix a bunch of misspellingsIgor Zhilianin
Change-Id: If2954bdfc551515403706b2cd0dde94e45936e08 GitHub-Last-Rev: d4cfc41a5504cf10befefdb881d4c45986a1d1f8 GitHub-Pull-Request: golang/go#28049 Reviewed-on: https://go-review.googlesource.com/c/140299 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-09-13cmd/vet: avoid internal error for implicitly declared type switch varsRobert Griesemer
For type switches using a short variable declaration of the form switch t := x.(type) { case T1: ... go/types doesn't declare the symbolic variable (t in this example) with the switch; thus such variables are not found in types.Info.Defs. Instead they are implicitly declared with each type switch case, and can be found in types.Info.Implicits. Adjust the shadowing code accordingly. Added a test case to verify that the issue is fixed, and a test case verifying that the shadowing code now considers implicitly declared variables introduces in type switch cases. While at it, also fixed the (internal) error reporting to provide more accurate information. Fixe #26725. Change-Id: If408ed9e692bf47c640f81de8f46bf5eb43415b0 Reviewed-on: https://go-review.googlesource.com/135117 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-23cmd/vet: check embedded field tags tooDaniel Martí
We can no longer use the field's position for the duplicate field tag warning - since we now check embedded tags, the positions may belong to copmletely different packages. Instead, keep track of the lowest field that's still part of the top-level struct type that we are checking. Finally, be careful to not repeat the independent struct field warnings when checking fields again because they are embedded into another struct. To do this, separate the duplicate tag value logic into a func that recurses into embedded fields on a per-encoding basis. Fixes #25593. Change-Id: I3bd6e01306d8ec63c0314d25e3136d5e067a9517 Reviewed-on: https://go-review.googlesource.com/115677 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-21cmd/vet: implement old TODO from testdata/print.goIan Lance Taylor
The code was fixed in CL 108559 but the testing TODO was not implemented. Updates #22936 Change-Id: I20a703260a181bbcf5f87609d6fb8221a182be1a Reviewed-on: https://go-review.googlesource.com/125038 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
2018-08-20cmd/vet: rewrite structtag using go/typesDaniel Martí
This lets us simplify the code considerably. For example, unquoting the tag is no longer necessary, and we can get the field name with a single method call. While at it, fix a typechecking error in testdata/structtag.go, which hadn't been caught since vet still skips past go/types errors in most cases. Using go/types will also let us expand the structtag check more easily if we want to, for example to allow it to check for duplicates in embedded fields. Finally, update one of the test cases to check for regressions when we output invalid tag strings. We also checked that these two changes to testdata/structtag.go didn't fail with the old structtag check. For #25593. Change-Id: Iea4906d0f30a67f36b28c21d8aa96251aae653f5 Reviewed-on: https://go-review.googlesource.com/115676 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Rob Pike <r@golang.org>
2018-08-17cmd/vet: don't suggest ... if it breaks a programDaniel Martí
It is possible to write a function that seems to wrap a print/printf call, but then doesn't. For example, if the string parameter we thought was the format is used as another argument. One option would be to make vet's print analysis smarter, to detect when format strings are indeed used like we initially suspected. However, I've opted for a simpler solution - check if the print/printf call is already using more than one variadic argument, in which case using an ellipsis in the last one would break the program: // too many arguments in call to fmt.Printf fmt.Printf(format, arg0, args...) Fixes #26979. Change-Id: I39371f1cec8483cfd2770a91670c1e80cbb9efdf Reviewed-on: https://go-review.googlesource.com/129575 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2018-07-23cmd/vet: if a function modifies its args, it's not a print wrapperIan Lance Taylor
Fixes #26486 Updates #26555 Change-Id: I402137b796e574e9b085ab54290d1b4ef73d3fcc Reviewed-on: https://go-review.googlesource.com/125039 Reviewed-by: Russ Cox <rsc@golang.org>
2018-06-28cmd/vet: fix ironic misuse of fmt.SprintfRuss Cox
Move badf helper into top-level function so that prints from buildtag.go are once again themselves printf-format-checked by vet. Also, fix implementation, which was missing a ... in the Sprintf call and produced messages like: /Users/rsc/x_test.go:1: +build comment must appear before package clause and be followed by a blank line%!(EXTRA []interface {}=[]) These were introduced in CL 111415. Change-Id: I000af3a4e01dc99fc79c9146aa68a71dace1460f Reviewed-on: https://go-review.googlesource.com/121300 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-06-12cmd/vet: use vet-specific export data to record detected printf wrappersRuss Cox
This CL takes advantage of the ability to record vet-specific export data, added in CL 108558, to save information about observed printf wrappers. Then calls to those wrappers from other packages can be format-checked. This found a few real mistakes using previously-unrecognized printf wrappers in cmd/compile. It will no doubt find real mistakes in external code. Change-Id: I9c29c92d89bbdc984571a174a96e6054585e9cd4 Reviewed-on: https://go-review.googlesource.com/108559 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-05-29cmd/vet: avoid false positives with non-commentsDaniel Martí
vet's buildtag check looks for malformed build tag comments. Since these can appear in Go files as well as non-Go files (such as assembly files), it must read the file line by line instead of using go/token or go/ast directly. However, this method runs into false positives if there are any lines in the code that look like comments, but are not. For example: $ cat f.go package main const foo = ` //+build ignore ` $ go vet f.go ./f.go:3: +build comment must appear before package clause and be followed by a blank line This bug has been popping up more frequently since vet started being run with go test, so it is important to make the check as precise as possible. To avoid the false positive, when checking a Go file, cross-check that a line that looks like a comment actually corresponds to a comment in the go/ast syntax tree. Since vet already obtains the syntax trees for all the Go files, it checks, this change means very little extra work for the check. While at it, add a badf helper function to simplify the code that reports warnings in the buildtag check. Fixes #13533. Change-Id: I484a16da01363b409ec418c313634171bf85250b Reviewed-on: https://go-review.googlesource.com/111415 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-05-28cmd/vet: eliminate use of Perl in testsYury Smolsky
This change uses errorCheck and wantedErrors functions copied from the test/run.go to eliminate use of the test/errchk perl script. Tests' error messages that contained full filenames were changed to have base filenames because the errorCheck function processes output from "go vet" in the same way. Fixes #20032. Change-Id: Ieb7be67c2d7281b9648171c698398449b7e2d4dd Reviewed-on: https://go-review.googlesource.com/114176 Run-TryBot: Yury Smolsky <yury@smolsky.by> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-05-09cmd/vet: assume that no builtin funcs are pureDaniel Martí
That was the intention with the existing code, but it was buggy; builtin functions aren't treated as values by types.TypeAndVal. Thus, we should use the IsBuiltin method instead of IsValue. Teaching vet what builtin funcs are pure is already being tracked as a separate issue, #22851. While at it, also add a test with methods, just to be sure that the current logic doesn't break with that edge case either. Fixes #25303. Change-Id: Ic18402b22cceeabf76641c02f575b194b9a536cc Reviewed-on: https://go-review.googlesource.com/112177 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2018-05-07cmd/vet: recognise func type conversionsDaniel Martí
In hasSideEffects, vet has to be taught whether or not a CallExpr is an actual function call, or just a type conversion. The previous code knew how to differentiate fn(arg) from int(arg), but it incorrectly saw (func(T))(fn) as a func call. This edge case is slightly tricky, since the CallExpr.Fun has a func signature type, just like in func calls. However, the difference is that in this case the Fun is a type, not a value. This information is in types.TypeAndValue, so use it. Change-Id: I18bb8b23abbe7decc558b726ff2dc31fae2f13d6 Reviewed-on: https://go-review.googlesource.com/111416 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2018-05-04cmd/vet: %T is a formatting directive tooDaniel Martí
Some warnings were being missed, because vet's regex that finds formatting directives was missing the 'T' verb. Fixes #24646. Change-Id: I2f6f9ed19e7daf9a07175199f428a62e94799ea9 Reviewed-on: https://go-review.googlesource.com/111357 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Rob Pike <r@golang.org>
2018-05-04cmd/vet: better align print warnings with fmtDaniel Martí
fmt's %d, %x, and %X all accept pointer arguments. However, in cmd/vet's printVerbs table, they were defined as if they did not accept pointer arguments. This inconsistency with fmt did not manifest to users since the vet codebase worked around it. In particular, pointer arguments were usually allowed for verbs that accepted integers, as the *types.Pointer argument type case read the following: t&(argInt|argPointer) != 0 As a result, using the %q verb with a pointer resulted in a bug in cmd/vet: $ go run f.go %!q(*int=0xc000014140) $ go vet f.go [no warning] As documented, fmt's %q verb only accepts runes (integers), strings, and byte slices. It should not accept pointers, and it does not. But since vet mixed integers and pointers, it wasn't properly warning about the misuse of fmt. This patch surfaced another bug with fmt.Printf("%p", nil): $ go run f.go %!p(<nil>) $ go vet f.go [no warning] As documented, fmt's %p verb only accepts pointers, and untyped nil is not a valid pointer. But vet did not warn about it, which is another inconsistency with fmt's documented rules. Fix that too, with a test, also getting rid of the TODO associated with the code. As a result of those changes, fix a wrong use of the fmt format verbs in the standard library, now correctly spotted by vet. Fixes #25233. Change-Id: Id0ad31fbc25adfe1c46c6b6879b8d02b23633b3a Reviewed-on: https://go-review.googlesource.com/111284 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2018-04-27cmd/vet: remove "only" from error messageKevin Burke
If the vetted function supplies zero arguments, previously you would get an error message like this: Printf format %v reads arg #1, but call has only 0 args "has only 0 args" is an odd construction, and "has 0 args" sounds better. Getting rid of "only" in all cases simplifies the code and reads just as well. Change-Id: I4706dfe4a75f13bf4db9c0650e459ca676710752 Reviewed-on: https://go-review.googlesource.com/109457 Run-TryBot: Kevin Burke <kev@inburke.com> Run-TryBot: David Symonds <dsymonds@golang.org> Reviewed-by: David Symonds <dsymonds@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-15cmd/vet: fix panic in dead code checker on ill-formed switch statements.David Symonds
A switch statement without a tag requires case values to be bools, but the parser does not enforce that, so AST-walking code needs to take care. Change-Id: I7d9abbb0324314e02a37813c2d2f6adb0d6af5e7 Reviewed-on: https://go-review.googlesource.com/107375 Reviewed-by: Rob Pike <r@golang.org>
2018-04-04go/printer, gofmt: tuned table alignment for better resultsRobert Griesemer
The go/printer (and thus gofmt) uses a heuristic to determine whether to break alignment between elements of an expression list which is spread across multiple lines. The heuristic only kicked in if the entry sizes (character length) was above a certain threshold (20) and the ratio between the previous and current entry size was above a certain value (4). This heuristic worked reasonably most of the time, but also led to unfortunate breaks in many cases where a single entry was suddenly much smaller (or larger) then the previous one. The behavior of gofmt was sufficiently mysterious in some of these situations that many issues were filed against it. The simplest solution to address this problem is to remove the heuristic altogether and have a programmer introduce empty lines to force different alignments if it improves readability. The problem with that approach is that the places where it really matters, very long tables with many (hundreds, or more) entries, may be machine-generated and not "post-processed" by a human (e.g., unicode/utf8/tables.go). If a single one of those entries is overlong, the result would be that the alignment would force all comments or values in key:value pairs to be adjusted to that overlong value, making the table hard to read (e.g., that entry may not even be visible on screen and all other entries seem spaced out too wide). Instead, we opted for a slightly improved heuristic that behaves much better for "normal", human-written code. 1) The threshold is increased from 20 to 40. This disables the heuristic for many common cases yet even if the alignment is not "ideal", 40 is not that many characters per line with todays screens, making it very likely that the entire line remains "visible" in an editor. 2) Changed the heuristic to not simply look at the size ratio between current and previous line, but instead considering the geometric mean of the sizes of the previous (aligned) lines. This emphasizes the "overall picture" of the previous lines, rather than a single one (which might be an outlier). 3) Changed the ratio from 4 to 2.5. Now that we ignore sizes below 40, a ratio of 4 would mean that a new entry would have to be 4 times bigger (160) or smaller (10) before alignment would be broken. A ratio of 2.5 seems more sensible. Applied updated gofmt to all of src and misc. Also tested against several former issues that complained about this and verified that the output for the given examples is satisfactory (added respective test cases). Some of the files changed because they were not gofmt-ed in the first place. For #644. For #7335. For #10392. (and probably more related issues) Fixes #22852. Change-Id: I5e48b3d3b157a5cf2d649833b7297b33f43a6f6e
2018-02-27cmd/vet: type conversions never have side effectsDaniel Martí
Make the hasSideEffects func use type information to see if a CallExpr is a type conversion or not. In case it is, there cannot be any side effects. Now that vet always has type information, we can afford to use it here. Update the tests and remove the TODO there too. Change-Id: I74fdacf830aedf2371e67ba833802c414178caf1 Reviewed-on: https://go-review.googlesource.com/79536 Reviewed-by: Robert Griesemer <gri@golang.org>
2018-02-23cmd/vet: use type info to detect the atomic funcsDaniel Martí
Simply checking if a name is "atomic" isn't enough, as that might be a var or another imported package. Now that vet requires type information, we can do better. And add a simple regression test. Change-Id: Ibd2004428374e3628cd3cd0ffb5f37cedaf448ea Reviewed-on: https://go-review.googlesource.com/91795 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Robert Griesemer <gri@golang.org>
2018-02-21cmd/vet: warn on unkeyed struct pointer literalsDaniel Martí
We did warn on them in some cases, but not others. In particular, if one used a slice composite literal with struct pointer elements, and omitted the type of an element's composite literal, it would not get any warning even if it should get one. The issue is that typ.Underlying() can be of type *types.Pointer. Skip those levels of indirection before checking for a *types.Struct underlying type. isLocalType also needed a bit of tweaking to ignore dereferences. Perhaps that can be rewritten now that we have type info, but let's leave it for another time. Fixes #23539. Change-Id: I727a497284df1325b70d47a756519f5db1add25d Reviewed-on: https://go-review.googlesource.com/89715 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-02-12cmd/vet: teach asmdecl check about NOFRAMEAustin Clements
Change-Id: I3f71228e391f122f9cc5656ca6835fdf51a424b7 Reviewed-on: https://go-review.googlesource.com/92435 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
2018-02-06cmd/vet: unexported interface{} fields on %s are okDaniel Martí
For example, the following program is valid: type T struct { f interface{} } func main() { fmt.Printf("%s", T{"foo"}) // prints {foo} } Since the field is of type interface{}, we might have any value in it. For example, if we had T{3}, fmt would complain. However, not knowing what the type under the interface is, we must be conservative. However, as shown in #17798, we should issue an error if the field's type is statically known to implement the error or fmt.Stringer interfaces. In those cases, the user likely wanted the %s format to call those methods. Keep the vet error in those cases. While at it, add more field type test cases, such as custom error types, and interfaces that extend the error interface. Fixes #23563. Change-Id: I063885955555917c59da000391b603f0d6dce432 Reviewed-on: https://go-review.googlesource.com/90516 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>