analyze

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package analyze provides the main errortype analyzer for detecting and enforcing consistent error type usage in Go programs.

The analyzer identifies several types of error handling issues:

  • Inconsistent pointer/value usage in function returns
  • Incorrect type assertions on error types
  • Misuse of errors.As with wrong pointer/value semantics
  • Switch statements with inconsistent error type handling

This package integrates with the golang.org/x/tools/go/analysis framework and depends on the internal/detect package for error type discovery.

Index

Constants

View Source
const (
	Name = "errortype"
	Doc  = `` /* 677-byte string literal not displayed */

	URL = "https://pkg.go.dev/fillmore-labs.com/errortype/analyzer"
)

Documentation constants.

View Source
const (
	// OptionCheckIs controls whether to check for `Is(error) bool` methods.
	OptionCheckIs Options = 1 << posOptionCheckIs
	// OptionCheckUnused flags unchecked results of `errors.As` calls.
	OptionCheckUnused = 1 << posOptionCheckUnused
	// OptionDeepIsCheck flags all unwrap methods in `Is` method checks, not only those on target.
	OptionDeepIsCheck = 1 << posOptionDeepIsCheck
	// OptionStyleCheck controls the target style check in `errors.As` calls.
	OptionStyleCheck = 1 << posOptionStyleCheck
	// OptionUncheckedAssert flags all unchecked asserts on errors.
	OptionUncheckedAssert = 1 << posOptionUncheckedAssert

	// DefaultOptions is the default configuration for error analysis.
	DefaultOptions = OptionCheckIs | OptionStyleCheck
)

Variables

View Source
var ErrResultMissing = errors.New("analyzer result missing")

ErrResultMissing is returned when the result from an analyzer is missing.

Functions

func AllReturns

func AllReturns(b inspector.Cursor) iter.Seq[*ast.ReturnStmt]

AllReturns iterates over all return statements within the AST tree of the node at the given inspector cursor.

It does not descend into nested function literals, so only return statements of the current function are considered.

Types

type Options

type Options uint8

Options represent configuration flags to control the behavior of style and correctness checks for errors.

func (Options) CheckIs added in v0.0.8

func (o Options) CheckIs() bool

CheckIs controls whether to check for `Is(error) bool` methods.

func (Options) CheckUnused added in v0.0.8

func (o Options) CheckUnused() bool

CheckUnused flags unchecked results of `errors.As` calls.

func (Options) DeepIsCheck added in v0.0.8

func (o Options) DeepIsCheck() bool

DeepIsCheck flags all unwrap methods in `Is` method checks, not only those on target.

func (*Options) SetOption added in v0.0.8

func (o *Options) SetOption(flag Options, v bool)

SetOption modifies the state of a specific option flag in the Options configuration based on the provided boolean value. The flag parameter should be a single option constant (e.g., OptionCheckIs, OptionStyleCheck).

func (Options) String added in v0.0.8

func (o Options) String() string

func (Options) StyleCheck added in v0.0.8

func (o Options) StyleCheck() bool

StyleCheck controls the target style check in `errors.As` calls.

func (Options) UncheckedAssert added in v0.0.8

func (o Options) UncheckedAssert() bool

UncheckedAssert flags all unchecked asserts on errors.

type Pass added in v0.0.8

type Pass struct {
	*analysis.Pass
	usage.ErrorUsage
	Options
}

Pass wraps an *analysis.Pass and tracks error usages within the analysis Pass. It contains a PropertyMap that associates error usages with their corresponding Usage information.

func NewPass added in v0.0.8

func NewPass(ap *analysis.Pass, opt Options) Pass

NewPass creates and returns a new Pass instance, initializing its errorUsages property map. It takes an *analysis.Pass as input and embeds it within the returned Pass.

func (Pass) ProcessAST added in v0.0.8

func (p Pass) ProcessAST(ctx context.Context, in *inspector.Inspector)

ProcessAST traverses the abstract syntax tree of the package being analyzed. It visits nodes relevant to error usage and dispatches each to its corresponding handler function.

func (Pass) ReportErrorf added in v0.0.8

func (p Pass) ReportErrorf(n analysis.Range, format string, args ...any)

ReportErrorf reports an internal ("should not happen") failure message.

type RunOptions added in v0.0.8

type RunOptions struct {
	Options

	DetectTypes *analysis.Analyzer

	// Suggest appends suggestions to a file
	Suggest string

	Context context.Context
	// contains filtered or unexported fields
}

RunOptions provide configurations for analysis passes, including type detection and AST-related behavior customization.

func DefaultRunOptions added in v0.0.8

func DefaultRunOptions() *RunOptions

DefaultRunOptions returns a RunOptions struct initialized with default values.

func (*RunOptions) Analyzer added in v0.0.8

func (o *RunOptions) Analyzer() *analysis.Analyzer

Analyzer creates an instance of the errortype analyzer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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