...

Source file src/cmd/vendor/golang.org/x/tools/go/analysis/passes/printf/doc.go

Documentation: cmd/vendor/golang.org/x/tools/go/analysis/passes/printf

     1  // Copyright 2023 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  // Package printf defines an Analyzer that checks consistency
     6  // of Printf format strings and arguments.
     7  //
     8  // # Analyzer printf
     9  //
    10  // printf: check consistency of Printf format strings and arguments
    11  //
    12  // The check applies to calls of the formatting functions such as
    13  // [fmt.Printf] and [fmt.Sprintf], as well as any detected wrappers of
    14  // those functions.
    15  //
    16  // In this example, the %d format operator requires an integer operand:
    17  //
    18  //	fmt.Printf("%d", "hello") // fmt.Printf format %d has arg "hello" of wrong type string
    19  //
    20  // See the documentation of the fmt package for the complete set of
    21  // format operators and their operand types.
    22  //
    23  // To enable printf checking on a function that is not found by this
    24  // analyzer's heuristics (for example, because control is obscured by
    25  // dynamic method calls), insert a bogus call:
    26  //
    27  //	func MyPrintf(format string, args ...any) {
    28  //		if false {
    29  //			_ = fmt.Sprintf(format, args...) // enable printf checker
    30  //		}
    31  //		...
    32  //	}
    33  //
    34  // The -funcs flag specifies a comma-separated list of names of additional
    35  // known formatting functions or methods. If the name contains a period,
    36  // it must denote a specific function using one of the following forms:
    37  //
    38  //	dir/pkg.Function
    39  //	dir/pkg.Type.Method
    40  //	(*dir/pkg.Type).Method
    41  //
    42  // Otherwise the name is interpreted as a case-insensitive unqualified
    43  // identifier such as "errorf". Either way, if a listed name ends in f, the
    44  // function is assumed to be Printf-like, taking a format string before the
    45  // argument list. Otherwise it is assumed to be Print-like, taking a list
    46  // of arguments with no format string.
    47  package printf
    48  

View as plain text