Skip to content

Commit 42b4658

Browse files
committed
cmd/go: improve go vet documentation
- restore and rework cmd/vet/doc.go, which was clobbered during the vet-lite switch. - document go vet -vettool=prog flag and how to run an alternative checker. - make 'go vet -help' show how to list vet tool's flags. Example: $ go vet -help usage: go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages] Run 'go help vet' for details. Run 'go tool vet help' for the vet tool's flags. $ go vet -vettool=~/bin/myvet -help usage: go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages] Run 'go help vet' for details. Run '~/bin/myvet help' for the vet tool's flags. Updates #28840 Change-Id: Ieb79dfe29e1df074f865bc9a9d47b44199675d7d Reviewed-on: https://go-review.googlesource.com/c/147018 Reviewed-by: Daniel Martí <[email protected]> Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 3c92bdc commit 42b4658

File tree

6 files changed

+111
-23
lines changed

6 files changed

+111
-23
lines changed

src/cmd/go/alldocs.go

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/vet/vet.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,26 @@ import (
1616
var CmdVet = &base.Command{
1717
Run: runVet,
1818
CustomFlags: true,
19-
UsageLine: "go vet [-n] [-x] [build flags] [vet flags] [packages]",
19+
UsageLine: "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]",
2020
Short: "report likely mistakes in packages",
2121
Long: `
2222
Vet runs the Go vet command on the packages named by the import paths.
2323
2424
For more about vet and its flags, see 'go doc cmd/vet'.
2525
For more about specifying packages, see 'go help packages'.
26+
For a list of checkers and their flags, see 'go tool vet help'.
27+
For details of a specific checker such as 'printf', see 'go tool vet help printf'.
2628
2729
The -n flag prints commands that would be executed.
2830
The -x flag prints commands as they are executed.
2931
32+
The -vettool=prog flag selects a different analysis tool with alternative
33+
or additional checks.
34+
For example, the 'shadow' analyzer can be built and run using these commands:
35+
36+
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
37+
go vet -vettool=$(which shadow)
38+
3039
The build flags supported by go vet are those that control package resolution
3140
and execution, such as -n, -x, -v, -tags, and -toolexec.
3241
For more about these flags, see 'go help build'.
@@ -38,7 +47,7 @@ See also: go fmt, go fix.
3847
func runVet(cmd *base.Command, args []string) {
3948
modload.LoadTests = true
4049

41-
vetFlags, pkgArgs := vetFlags(cmd.Usage, args)
50+
vetFlags, pkgArgs := vetFlags(vetUsage, args)
4251

4352
work.BuildInit()
4453
work.VetFlags = vetFlags

src/cmd/go/internal/vet/vetflag.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,21 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) {
166166
}
167167
return args, nil
168168
}
169+
170+
var vetUsage func()
171+
172+
func init() { vetUsage = usage } // break initialization cycle
173+
174+
func usage() {
175+
fmt.Fprintf(os.Stderr, "usage: %s\n", CmdVet.UsageLine)
176+
fmt.Fprintf(os.Stderr, "Run 'go help %s' for details.\n", CmdVet.LongName())
177+
178+
// This part is additional to what (*Command).Usage does:
179+
cmd := "go tool vet"
180+
if vetTool != "" {
181+
cmd = vetTool
182+
}
183+
fmt.Fprintf(os.Stderr, "Run '%s -help' for the vet tool's flags.\n", cmd)
184+
185+
os.Exit(2)
186+
}

src/cmd/go/testdata/script/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ stderr 'Run ''go help mod'' for usage.'
3434
! go vet -h
3535
stderr 'usage: go vet'
3636
stderr 'Run ''go help vet'' for details'
37+
stderr 'Run ''go tool vet -help'' for the vet tool''s flags'
3738

3839
# Earlier versions of Go printed a large document here, instead of these two
3940
# lines.

src/cmd/vet/doc.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
7+
Vet examines Go source code and reports suspicious constructs, such as Printf
8+
calls whose arguments do not align with the format string. Vet uses heuristics
9+
that do not guarantee all reports are genuine problems, but it can find errors
10+
not caught by the compilers.
11+
12+
Vet is normally invoked through the go command.
13+
This command vets the package in the current directory:
14+
15+
go vet
16+
17+
whereas this one vets the packages whose path is provided:
18+
19+
go vet my/project/...
20+
21+
Use "go help packages" to see other ways of specifying which packages to vet.
22+
23+
Vet's exit code is non-zero for erroneous invocation of the tool or if a
24+
problem was reported, and 0 otherwise. Note that the tool does not
25+
check every possible problem and depends on unreliable heuristics,
26+
so it should be used as guidance only, not as a firm indicator of
27+
program correctness.
28+
29+
To list the available checks, run "go tool vet help":
30+
31+
asmdecl report mismatches between assembly files and Go declarations
32+
assign check for useless assignments
33+
atomic check for common mistakes using the sync/atomic package
34+
bools check for common mistakes involving boolean operators
35+
buildtag check that +build tags are well-formed and correctly located
36+
cgocall detect some violations of the cgo pointer passing rules
37+
composites check for unkeyed composite literals
38+
copylocks check for locks erroneously passed by value
39+
httpresponse check for mistakes using HTTP responses
40+
loopclosure check references to loop variables from within nested functions
41+
lostcancel check cancel func returned by context.WithCancel is called
42+
nilfunc check for useless comparisons between functions and nil
43+
printf check consistency of Printf format strings and arguments
44+
shift check for shifts that equal or exceed the width of the integer
45+
stdmethods check signature of methods of well-known interfaces
46+
structtag check that struct field tags conform to reflect.StructTag.Get
47+
tests check for common mistaken usages of tests and examples
48+
unmarshal report passing non-pointer or non-interface values to unmarshal
49+
unreachable check for unreachable code
50+
unsafeptr check for invalid conversions of uintptr to unsafe.Pointer
51+
unusedresult check for unused results of calls to some functions
52+
53+
For details and flags of a particular check, such as printf, run "go tool vet help printf".
54+
55+
By default, all checks are performed.
56+
If any flags are explicitly set to true, only those tests are run.
57+
Conversely, if any flag is explicitly set to false, only those tests are disabled.
58+
Thus -printf=true runs the printf check,
59+
and -printf=false runs all checks except the printf check.
60+
61+
For information on writing a new check, see golang.org/x/tools/go/analysis.
62+
63+
Core flags:
64+
65+
-c=N
66+
display offending line plus N lines of surrounding context
67+
-json
68+
emit analysis diagnostics (and errors) in JSON format
69+
70+
*/
71+
package main

src/cmd/vet/main.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// The vet command is a driver for static checkers conforming to
2-
// the golang.org/x/tools/go/analysis API. Run it using 'go vet'.
3-
//
4-
// For a tool capable of running standalone, use a multichecker-based
5-
// tool such as golang.org/x/tools/go/analysis/cmd/vet.
61
package main
72

83
import (
@@ -31,21 +26,6 @@ import (
3126
"golang.org/x/tools/go/analysis/passes/unusedresult"
3227
)
3328

34-
// Legacy vet had the concept of "experimental" checkers.
35-
36-
// There was exactly one, shadow, and it had to be explicitly
37-
// enabled by the -shadow flag, which would of course disable
38-
// all the other tristate flags, requiring the -all flag (which
39-
// is now a no-op) to reenable them.
40-
//
41-
// The shadow analyzer has been removed from the suite,
42-
// but can be run using these additional commands:
43-
// $ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
44-
// $ go vet -vettool=$(which shadow)
45-
// Alternatively, one could build a multichecker containing all
46-
// the desired checks (vet's suite + shadow) and run it in a
47-
// single "go vet" command.
48-
4929
func main() {
5030
unitchecker.Main(
5131
asmdecl.Analyzer,

0 commit comments

Comments
 (0)