printf

package
v0.0.0-...-076a18f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2023 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Package printf defines an Analyzer that checks consistency of Printf format strings and arguments.

Analyzer printf

printf: check consistency of Printf format strings and arguments

The check applies to calls of the formatting functions such as fmt.Printf and fmt.Sprintf, as well as any detected wrappers of those functions.

In this example, the %d format operator requires an integer operand:

fmt.Printf("%d", "hello") // fmt.Printf format %d has arg "hello" of wrong type string

See the documentation of the fmt package for the complete set of format operators and their operand types.

To enable printf checking on a function that is not found by this analyzer's heuristics (for example, because control is obscured by dynamic method calls), insert a bogus call:

func MyPrintf(format string, args ...any) {
	if false {
		_ = fmt.Sprintf(format, args...) // enable printf checker
	}
	...
}

The -funcs flag specifies a comma-separated list of names of additional known formatting functions or methods. If the name contains a period, it must denote a specific function using one of the following forms:

dir/pkg.Function
dir/pkg.Type.Method
(*dir/pkg.Type).Method

Otherwise the name is interpreted as a case-insensitive unqualified identifier such as "errorf". Either way, if a listed name ends in f, the function is assumed to be Printf-like, taking a format string before the argument list. Otherwise it is assumed to be Print-like, taking a list of arguments with no format string.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:       "printf",
	Doc:        analysisutil.MustExtractDoc(doc, "printf"),
	URL:        "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/printf",
	Requires:   []*analysis.Analyzer{inspect.Analyzer},
	Run:        run,
	ResultType: reflect.TypeOf((*Result)(nil)),
	FactTypes:  []analysis.Fact{new(isWrapper)},
}

Functions

This section is empty.

Types

type Kind

type Kind int

Kind is a kind of fmt function behavior.

const (
	KindNone   Kind = iota // not a fmt wrapper function
	KindPrint              // function behaves like fmt.Print
	KindPrintf             // function behaves like fmt.Printf
	KindErrorf             // function behaves like fmt.Errorf
)

func (Kind) String

func (kind Kind) String() string

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result is the printf analyzer's result type. Clients may query the result to learn whether a function behaves like fmt.Print or fmt.Printf.

func (*Result) Kind

func (r *Result) Kind(fn *types.Func) Kind

Kind reports whether fn behaves like fmt.Print or fmt.Printf.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL