errconst

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 7 Imported by: 0

README

yze-go-errconst

A yze analyzer (category errors) enforcing the gomatic Go standard that errors are sentinel constants (errs.Const): it flags errors.New(...) and fmt.Errorf(...) calls that do not wrap a cause with %w.

  • Rule: yze/errconst
  • Library: exports Analyzer and Registration for the yze aggregator and stickler runner.
  • Binary: cmd/yze-go-errconst runs it standalone (text/-json, and as a go vet -vettool).

The %w proxy (v1 behavior)

An fmt.Errorf call is allowed when its literal format string contains %w; every other fmt.Errorf is flagged. The %w token is the proxy for "this wraps an existing error rather than creating a new one". The analyzer deliberately does not trace the wrapped argument back to a constant sentinel: the convention permits adding context to either a sentinel or an injected cause, and an injected cause is by definition a non-sentinel error value, so requiring a sentinel argument would wrongly reject the permitted injected-cause case. Two consequences follow, both intentional and pinned by fixtures:

  • fmt.Errorf("%w: ...", cause) is allowed even when cause is a non-sentinel error value.
  • A const-held format string (rather than a string literal) is not inspected, so it escapes the check.

Built on the go-yze framework.

Documentation

Overview

Package errconst provides a go/analysis analyzer enforcing the gomatic Go standard that errors are sentinel constants (errs.Const), never created with errors.New or fmt.Errorf — fmt.Errorf is permitted only to wrap a cause with %w.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "errconst",
	Doc:      "reports errors.New and non-wrapping fmt.Errorf, which the gomatic Go standard forbids",
	Requires: []*analysis.Analyzer{inspect.Analyzer},
	Run:      run,
}

Analyzer reports disallowed error construction.

View Source
var Registration = goyze.Registration{
	Name:       "errconst",
	Categories: []goyze.Category{"errors"},
	URL:        "https://docs.gomatic.dev/yze/errconst",
	Analyzer:   Analyzer,
}

Registration declares this analyzer to the yze framework.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
yze-go-errconst command
Command yze-go-errconst runs the errconst analyzer as a standalone go/analysis checker (text and -json output, and usable as a `go vet -vettool`).
Command yze-go-errconst runs the errconst analyzer as a standalone go/analysis checker (text and -json output, and usable as a `go vet -vettool`).

Jump to

Keyboard shortcuts

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