...
Run Format

Source file src/cmd/vet/doc.go

Documentation: cmd/vet

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

View as plain text