1// Copyright 2010 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 7Vet examines Go source code and reports suspicious constructs, such as Printf 8calls whose arguments do not align with the format string. Vet uses heuristics 9that do not guarantee all reports are genuine problems, but it can find errors 10not caught by the compilers. 11 12Vet is normally invoked using the go command by running "go vet": 13 14 go vet 15vets the package in the current directory. 16 17 go vet package/path/name 18vets the package whose path is provided. 19 20Use "go help packages" to see other ways of specifying which packages to vet. 21 22Vet's exit code is 2 for erroneous invocation of the tool, 1 if a 23problem was reported, and 0 otherwise. Note that the tool does not 24check every possible problem and depends on unreliable heuristics 25so it should be used as guidance only, not as a firm indicator of 26program correctness. 27 28By default the -all flag is set so all checks are performed. 29If any flags are explicitly set to true, only those tests are run. Conversely, if 30any flag is explicitly set to false, only those tests are disabled. Thus -printf=true 31runs the printf check, -printf=false runs all checks except the printf check. 32 33By default vet uses the object files generated by 'go install some/pkg' to typecheck the code. 34If the -source flag is provided, vet uses only source code. 35 36Available checks: 37 38Assembly declarations 39 40Flag: -asmdecl 41 42Mismatches between assembly files and Go function declarations. 43 44Useless assignments 45 46Flag: -assign 47 48Check for useless assignments. 49 50Atomic mistakes 51 52Flag: -atomic 53 54Common mistaken usages of the sync/atomic package. 55 56Boolean conditions 57 58Flag: -bool 59 60Mistakes involving boolean operators. 61 62Build tags 63 64Flag: -buildtags 65 66Badly formed or misplaced +build tags. 67 68Invalid uses of cgo 69 70Flag: -cgocall 71 72Detect some violations of the cgo pointer passing rules. 73 74Unkeyed composite literals 75 76Flag: -composites 77 78Composite struct literals that do not use the field-keyed syntax. 79 80Copying locks 81 82Flag: -copylocks 83 84Locks that are erroneously passed by value. 85 86HTTP responses used incorrectly 87 88Flag: -httpresponse 89 90Mistakes deferring a function call on an HTTP response before 91checking whether the error returned with the response was nil. 92 93Failure to call the cancelation function returned by WithCancel 94 95Flag: -lostcancel 96 97The cancelation function returned by context.WithCancel, WithTimeout, 98and WithDeadline must be called or the new context will remain live 99until its parent context is cancelled. 100(The background context is never cancelled.) 101 102Methods 103 104Flag: -methods 105 106Non-standard signatures for methods with familiar names, including: 107 Format GobEncode GobDecode MarshalJSON MarshalXML 108 Peek ReadByte ReadFrom ReadRune Scan Seek 109 UnmarshalJSON UnreadByte UnreadRune WriteByte 110 WriteTo 111 112Nil function comparison 113 114Flag: -nilfunc 115 116Comparisons between functions and nil. 117 118Printf family 119 120Flag: -printf 121 122Suspicious calls to functions in the Printf family, including any functions 123with these names, disregarding case: 124 Print Printf Println 125 Fprint Fprintf Fprintln 126 Sprint Sprintf Sprintln 127 Error Errorf 128 Fatal Fatalf 129 Log Logf 130 Panic Panicf Panicln 131The -printfuncs flag can be used to redefine this list. 132If the function name ends with an 'f', the function is assumed to take 133a format descriptor string in the manner of fmt.Printf. If not, vet 134complains about arguments that look like format descriptor strings. 135 136It also checks for errors such as using a Writer as the first argument of 137Printf. 138 139Range loop variables 140 141Flag: -rangeloops 142 143Incorrect uses of range loop variables in closures. 144 145Shadowed variables 146 147Flag: -shadow=false (experimental; must be set explicitly) 148 149Variables that may have been unintentionally shadowed. 150 151Shifts 152 153Flag: -shift 154 155Shifts equal to or longer than the variable's length. 156 157Struct tags 158 159Flag: -structtags 160 161Struct tags that do not follow the format understood by reflect.StructTag.Get. 162Well-known encoding struct tags (json, xml) used with unexported fields. 163 164Tests and documentation examples 165 166Flag: -tests 167 168Mistakes involving tests including functions with incorrect names or signatures 169and example tests that document identifiers not in the package. 170 171Unreachable code 172 173Flag: -unreachable 174 175Unreachable code. 176 177Misuse of unsafe Pointers 178 179Flag: -unsafeptr 180 181Likely incorrect uses of unsafe.Pointer to convert integers to pointers. 182A conversion from uintptr to unsafe.Pointer is invalid if it implies that 183there is a uintptr-typed word in memory that holds a pointer value, 184because that word will be invisible to stack copying and to the garbage 185collector. 186 187Unused result of certain function calls 188 189Flag: -unusedresult 190 191Calls to well-known functions and methods that return a value that is 192discarded. By default, this includes functions like fmt.Errorf and 193fmt.Sprintf and methods like String and Error. The flags -unusedfuncs 194and -unusedstringmethods control the set. 195 196Other flags 197 198These flags configure the behavior of vet: 199 200 -all (default true) 201 Enable all non-experimental checks. 202 -v 203 Verbose mode 204 -printfuncs 205 A comma-separated list of print-like function names 206 to supplement the standard list. 207 For more information, see the discussion of the -printf flag. 208 -shadowstrict 209 Whether to be strict about shadowing; can be noisy. 210 211Using vet directly 212 213For testing and debugging vet can be run directly by invoking 214"go tool vet" or just running the binary. Run this way, vet might not 215have up to date information for imported packages. 216 217 go tool vet source/directory/*.go 218vets the files named, all of which must be in the same package. 219 220 go tool vet source/directory 221recursively descends the directory, vetting each package it finds. 222 223*/ 224package main 225