rules

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Categories

func Categories() []string

Categories returns all categories from the global registry

func ConfigureAll

func ConfigureAll(cfg *core.Config) error

ConfigureAll configures all rules in the global registry

func Count

func Count() int

Count returns the rule count from the global registry

func HonorsSuppression added in v1.4.2

func HonorsSuppression(r Rule) bool

HonorsSuppression reports whether inline suppression comments apply to the given rule. All rules honor suppression unless they opt out explicitly via the SuppressionExempt interface.

func Register

func Register(rule Rule)

Register adds a rule to the global registry and fails fast on duplicate registration. Package init functions cannot recover from an invalid rule registry, while Registry.Register remains available for dynamic callers.

func ResetState added in v1.4.2

func ResetState(list []Rule)

ResetState clears the accumulated state of every stateful rule in the list.

Types

type BaseRule

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

BaseRule provides common functionality for rules

func NewBaseRule

func NewBaseRule(name, category, description string, severity core.Severity) *BaseRule

NewBaseRule creates a new base rule

func (*BaseRule) Category

func (r *BaseRule) Category() string

Category returns the rule category

func (*BaseRule) Configure

func (r *BaseRule) Configure(settings map[string]any) error

Configure sets rule settings

func (*BaseRule) CreateViolation

func (r *BaseRule) CreateViolation(file string, line int, message string) *core.Violation

CreateViolation creates a new violation for this rule

func (*BaseRule) DefaultSeverity

func (r *BaseRule) DefaultSeverity() core.Severity

DefaultSeverity returns the default severity

func (*BaseRule) Description

func (r *BaseRule) Description() string

Description returns the rule description

func (*BaseRule) GetBoolSetting

func (r *BaseRule) GetBoolSetting(key string, defaultVal bool) bool

GetBoolSetting retrieves a bool setting

func (*BaseRule) GetFloat64Setting

func (r *BaseRule) GetFloat64Setting(key string, defaultVal float64) float64

GetFloat64Setting retrieves a float64 setting

func (*BaseRule) GetIntSetting

func (r *BaseRule) GetIntSetting(key string, defaultVal int) int

GetIntSetting retrieves an int setting

func (*BaseRule) GetSetting

func (r *BaseRule) GetSetting(key string) (any, bool)

GetSetting retrieves a setting value

func (*BaseRule) GetStringSetting

func (r *BaseRule) GetStringSetting(key, defaultVal string) string

GetStringSetting retrieves a string setting

func (*BaseRule) Name

func (r *BaseRule) Name() string

Name returns the rule name

type GoProjectRule added in v1.4.2

type GoProjectRule interface {
	Rule
	AnalyzeGoProject(ctx *core.GoProjectContext) ([]*core.Violation, error)
	RequiresSSA() bool
}

GoProjectRule is an optional package-level analysis interface for rules that need shared typed syntax or SSA. Implementations still satisfy Rule for API compatibility, but their AnalyzeFile method is not called by the check flow.

type Registry

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

Registry holds all registered rules

func GlobalRegistry

func GlobalRegistry() *Registry

GlobalRegistry returns the global registry instance

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new rule registry

func (*Registry) All

func (r *Registry) All() []Rule

All returns all registered rules

func (*Registry) Categories

func (r *Registry) Categories() []string

Categories returns all category names

func (*Registry) ConfigureAll

func (r *Registry) ConfigureAll(cfg *core.Config) error

ConfigureAll configures all rules from config. Every rule is configured exactly once with the category settings overlaid by its own settings, so that rule settings extend the category ones instead of replacing them, and so that a rule never keeps settings from a previously analyzed config.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the total number of registered rules

func (*Registry) CountByCategory

func (r *Registry) CountByCategory() map[string]int

CountByCategory returns the number of rules per category

func (*Registry) Get

func (r *Registry) Get(name string) (Rule, bool)

Get returns a rule by name

func (*Registry) GetByCategory

func (r *Registry) GetByCategory(category string) []Rule

GetByCategory returns all rules in a category

func (*Registry) GetEnabled

func (r *Registry) GetEnabled(cfg *core.Config) []Rule

GetEnabled returns rules that are enabled according to config

func (*Registry) Register

func (r *Registry) Register(rule Rule) error

Register adds a rule to the registry

type Rule

type Rule interface {
	// Metadata
	Name() string
	Category() string
	Description() string
	DefaultSeverity() core.Severity

	// Configuration
	Configure(settings map[string]any) error

	// Analysis - rules implement the ones they need
	AnalyzeFile(ctx *core.FileContext) []*core.Violation
}

Rule is the interface that all rules must implement

func All

func All() []Rule

All returns all rules from the global registry

func Get

func Get(name string) (Rule, bool)

Get returns a rule from the global registry

func GetByCategory

func GetByCategory(category string) []Rule

GetByCategory returns rules by category from the global registry

func GetEnabled

func GetEnabled(cfg *core.Config) []Rule

GetEnabled returns enabled rules from the global registry

type RuleInfo

type RuleInfo struct {
	Name        string
	Category    string
	Description string
	Severity    core.Severity
}

RuleInfo contains metadata about a rule for display purposes. Auto-fix availability is not part of it: fixers live in pkg/fix (fix.DefaultRegistry), which the display layer consults directly.

func GetRuleInfo

func GetRuleInfo(r Rule) RuleInfo

GetRuleInfo extracts info from a rule

type StatefulRule added in v1.4.2

type StatefulRule interface {
	Rule
	ResetState()
}

StatefulRule is an optional interface for rules that accumulate state across files (cross-file analysis). The check flow resets them before analyzing a project root, so that findings never depend on a previously analyzed root.

type SuppressionExempt added in v1.4.2

type SuppressionExempt interface {
	SuppressionExempt() bool
}

SuppressionExempt is an optional interface for rules whose findings must not be silenced by inline comments (nolint:<rule> / <rule>: safe). Policy rules that forbid a pattern unconditionally implement it and return true.

Directories

Path Synopsis
Package helpers provides shared utilities for rule implementations.
Package helpers provides shared utilities for rule implementations.
Package rulestest builds throwaway Go modules for rule tests, so that a test can state the source it cares about and get a loaded project back.
Package rulestest builds throwaway Go modules for rule tests, so that a test can state the source it cares about and get a loaded project back.

Jump to

Keyboard shortcuts

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