lintfix

package
v0.74.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 3 Imported by: 0

README

lintfix

Structured lint remediation database for Go projects using golangci-lint.

Overview

The lintfix package provides:

  • 📋 Remediation database - Embedded JSON database mapping lint rules to fixes
  • 🔧 Helper references - Links to mogo helper functions for code fixes
  • 📝 Nolint generators - Properly formatted nolint comments with documented reasons
  • 📚 Documentation - Version-specific caveats and best practices

Quick Start

import (
    "github.com/grokify/mogo/lintfix"
    "github.com/grokify/mogo/lintfix/gosec"
)

// Query the remediation database
db := lintfix.MustLoadRemediations()
fix := db.GetGosec("G120")
fmt.Println(fix.Remediation.Summary)
// "Use http.MaxBytesReader inline before parsing form data"

// Generate nolint comments
comment := gosec.NolintG117(gosec.CommonReasons.OAuthTokenResponse)
// "//nolint:gosec // G117: OAuth token response per RFC 6749"

Remediation Types

Type Description Example
code Add/modify code with helper functions G120: Use http.MaxBytesReader
nolint Add nolint annotation with reason G117: OAuth token response
refactor Broader code changes needed G101: Move secrets to env vars

Supported Linters

  • gosec - Security-focused rules (G101, G115, G117, G118, G120, G401, G501, G601, G703, G704)
  • staticcheck - Static analysis (SA1019, SA4006)
  • errcheck - Error handling

Nolint Generators

The gosec subpackage provides type-safe nolint comment generators:

gosec.NolintG101(reason)  // Hardcoded credentials (false positive)
gosec.NolintG115(reason)  // Integer overflow (bounded value)
gosec.NolintG117(reason)  // Secret in JSON response
gosec.NolintG118(reason)  // context.Background in goroutine
gosec.NolintG703(reason)  // Path traversal (validated input)
gosec.NolintG704(reason)  // SSRF (trusted URL)
Common Reasons

Pre-written reason strings for common scenarios:

gosec.CommonReasons.OAuthTokenResponse        // G117
gosec.CommonReasons.ShutdownHandler           // G118
gosec.CommonReasons.InputValidatedNoPathSep   // G703
gosec.CommonReasons.HttptestServer            // G704
gosec.CommonReasons.BoundedByValidation       // G115

Documentation

Adding New Rules

Edit remediations.json to add new rules:

{
  "linters": {
    "gosec": {
      "G999": {
        "name": "Rule name",
        "description": "What the rule detects",
        "severity": "high|medium|low",
        "category": "security|correctness|maintenance",
        "remediation": {
          "type": "code|nolint|refactor",
          "summary": "Brief fix description",
          "example": "Code example"
        }
      }
    }
  }
}

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.

func (*RuleFix) HasHelper

func (rf *RuleFix) HasHelper() bool

HasHelper returns true if this remediation has a helper function.

func (*RuleFix) String

func (rf *RuleFix) String() string

String returns a formatted description of the rule fix.

Directories

Path Synopsis
Package gosec provides helpers for generating nolint comments for gosec rules.
Package gosec provides helpers for generating nolint comments for gosec rules.

Jump to

Keyboard shortcuts

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