...
Source file src/pkg/cmd/go/internal/vet/vet.go
1
2
3
4
5
6 package vet
7
8 import (
9 "cmd/go/internal/base"
10 "cmd/go/internal/load"
11 "cmd/go/internal/modload"
12 "cmd/go/internal/work"
13 "path/filepath"
14 )
15
16 var CmdVet = &base.Command{
17 Run: runVet,
18 CustomFlags: true,
19 UsageLine: "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]",
20 Short: "report likely mistakes in packages",
21 Long: `
22 Vet runs the Go vet command on the packages named by the import paths.
23
24 For more about vet and its flags, see 'go doc cmd/vet'.
25 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'.
28
29 The -n flag prints commands that would be executed.
30 The -x flag prints commands as they are executed.
31
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
39 The build flags supported by go vet are those that control package resolution
40 and execution, such as -n, -x, -v, -tags, and -toolexec.
41 For more about these flags, see 'go help build'.
42
43 See also: go fmt, go fix.
44 `,
45 }
46
47 func runVet(cmd *base.Command, args []string) {
48 modload.LoadTests = true
49
50 vetFlags, pkgArgs := vetFlags(vetUsage, args)
51
52 work.BuildInit()
53 work.VetFlags = vetFlags
54 if vetTool != "" {
55 var err error
56 work.VetTool, err = filepath.Abs(vetTool)
57 if err != nil {
58 base.Fatalf("%v", err)
59 }
60 }
61
62 pkgs := load.PackagesForBuild(pkgArgs)
63 if len(pkgs) == 0 {
64 base.Fatalf("no packages to vet")
65 }
66
67 var b work.Builder
68 b.Init()
69
70 root := &work.Action{Mode: "go vet"}
71 for _, p := range pkgs {
72 _, ptest, pxtest, err := load.TestPackagesFor(p, nil)
73 if err != nil {
74 base.Errorf("%v", err)
75 continue
76 }
77 if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil {
78 base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir)
79 continue
80 }
81 if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
82 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
83 }
84 if pxtest != nil {
85 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
86 }
87 }
88 b.Do(root)
89 }
90
View as plain text