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 ¶
const ( Name = "errortype" Doc = `` /* 677-byte string literal not displayed */ URL = "https://pkg.go.dev/fillmore-labs.com/errortype/analyzer" )
Documentation constants.
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 ¶
var ErrResultMissing = errors.New("analyzer result missing")
ErrResultMissing is returned when the result from an analyzer is missing.
Functions ¶
func AllReturns ¶
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
CheckIs controls whether to check for `Is(error) bool` methods.
func (Options) CheckUnused ¶ added in v0.0.8
CheckUnused flags unchecked results of `errors.As` calls.
func (Options) DeepIsCheck ¶ added in v0.0.8
DeepIsCheck flags all unwrap methods in `Is` method checks, not only those on target.
func (*Options) SetOption ¶ added in v0.0.8
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) StyleCheck ¶ added in v0.0.8
StyleCheck controls the target style check in `errors.As` calls.
func (Options) UncheckedAssert ¶ added in v0.0.8
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
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
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.
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.