Documentation
¶
Overview ¶
Package lintfix provides a structured database of lint rule remediations for Go projects using golangci-lint.
This package serves as a "data overlay" that maps lint errors to:
- Remediation strategies (code fix, nolint annotation, refactor)
- Helper packages that provide actual fixes (within mogo)
- Pre-written nolint comments with proper documentation
- Example code and explanations
Usage ¶
Load the remediation database and query for specific rules:
db := lintfix.MustLoadRemediations()
fix := db.GetGosec("G120")
fmt.Println(fix.Remediation.Summary)
// "Use http.MaxBytesReader before parsing form data"
Remediation Types ¶
The database categorizes remediations into three types:
- "code": Fix by adding/changing code (e.g., LimitRequestBody for G120)
- "nolint": Fix by adding a nolint annotation with proper documentation
- "refactor": Fix requires broader code changes (e.g., removing hardcoded secrets)
Nolint Generators ¶
For rules that require nolint annotations, use the gosec subpackage:
comment := gosec.NolintG117(gosec.CommonReasons.OAuthTokenResponse) // Returns: "//nolint:gosec // G117: OAuth token response per RFC 6749"
Helper Package References ¶
Code-based remediations reference helper packages within mogo:
fix := db.GetGosec("G120")
fmt.Println(fix.Remediation.Package)
// "github.com/grokify/mogo/net/http/httputilmore"
fmt.Println(fix.Remediation.Function)
// "LimitRequestBody"
Supported Linters ¶
Currently supported:
- gosec: Security-focused linter
- staticcheck: Go static analysis
- errcheck: Error handling checks
Documentation ¶
For detailed guides including version-specific caveats, see: https://github.com/grokify/mogo/tree/main/docs/lintfix
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Remediation ¶
type Remediation struct {
Type string `json:"type"` // "code", "nolint", "refactor"
Summary string `json:"summary"`
Pattern string `json:"pattern,omitempty"`
Package string `json:"package,omitempty"`
Function string `json:"function,omitempty"`
Example string `json:"example,omitempty"`
Explanation string `json:"explanation,omitempty"`
When string `json:"when,omitempty"`
Avoid []string `json:"avoid,omitempty"`
Caveats []string `json:"caveats,omitempty"`
}
Remediation contains the actual fix information.
type RemediationDB ¶
type RemediationDB struct {
Version string `json:"version"`
Description string `json:"description"`
Linters map[string]map[string]*RuleFix `json:"linters"`
}
RemediationDB is the top-level structure for the remediation database.
func LoadRemediations ¶
func LoadRemediations() (*RemediationDB, error)
LoadRemediations loads and parses the embedded remediation database.
func MustLoadRemediations ¶
func MustLoadRemediations() *RemediationDB
MustLoadRemediations loads the remediation database or panics.
func (*RemediationDB) Get ¶
func (db *RemediationDB) Get(linter, code string) *RuleFix
Get retrieves a remediation by linter and rule code. Returns nil if not found.
func (*RemediationDB) GetGosec ¶
func (db *RemediationDB) GetGosec(code string) *RuleFix
GetGosec is a convenience method for getting gosec remediations.
func (*RemediationDB) GetStaticcheck ¶
func (db *RemediationDB) GetStaticcheck(code string) *RuleFix
GetStaticcheck is a convenience method for getting staticcheck remediations.
func (*RemediationDB) ListLinters ¶
func (db *RemediationDB) ListLinters() []string
ListLinters returns all linters in the database.
func (*RemediationDB) ListRules ¶
func (db *RemediationDB) ListRules(linter string) []string
ListRules returns all rule codes for a given linter.
type RuleFix ¶
type RuleFix struct {
Name string `json:"name"`
Description string `json:"description"`
Severity string `json:"severity,omitempty"`
Category string `json:"category,omitempty"`
Remediation *Remediation `json:"remediation"`
References []string `json:"references,omitempty"`
}
RuleFix contains remediation information for a specific lint rule.