...

Package defers

import "cmd/vendor/golang.org/x/tools/go/analysis/passes/defers"
Overview
Index

Overview ▾

Package defers defines an Analyzer that checks for common mistakes in defer statements.

Analyzer defers

defers: report common mistakes in defer statements

The defers analyzer reports a diagnostic when a defer statement would result in a non-deferred call to time.Since, as experience has shown that this is nearly always a mistake.

For example:

start := time.Now()
...
defer recordLatency(time.Since(start)) // error: call to time.Since is not deferred

The correct code is:

defer func() { recordLatency(time.Since(start)) }()

Index ▾

Package files

defers.go doc.go

Variables

Analyzer is the defers analyzer.

var Analyzer = &analysis.Analyzer{
    Name:     "defers",
    Requires: []*analysis.Analyzer{inspect.Analyzer},
    URL:      "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/defers",
    Doc:      analysisutil.MustExtractDoc(doc, "defers"),
    Run:      run,
}