### Integrate varnamelen into a Go analysis tool Source: https://context7.com/blizzy78/varnamelen/llms.txt Demonstrates how to instantiate the analyzer, configure it via flags, and incorporate it into a multichecker or a custom wrapper analyzer. ```go package main import ( "fmt" "go/ast" "github.com/blizzy78/varnamelen" "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis/multichecker" "golang.org/x/tools/go/analysis/passes/inspect" ) func main() { // Create varnamelen analyzer varnamelenAnalyzer := varnamelen.NewAnalyzer() // Configure via flags (programmatically set flag values) varnamelenAnalyzer.Flags.Set("maxDistance", "8") varnamelenAnalyzer.Flags.Set("minNameLength", "3") varnamelenAnalyzer.Flags.Set("checkReceiver", "true") varnamelenAnalyzer.Flags.Set("checkReturn", "true") varnamelenAnalyzer.Flags.Set("checkTypeParam", "true") varnamelenAnalyzer.Flags.Set("ignoreNames", "err,id,ok") varnamelenAnalyzer.Flags.Set("ignoreDecls", "w http.ResponseWriter,r *http.Request") varnamelenAnalyzer.Flags.Set("ignoreTypeAssertOk", "true") varnamelenAnalyzer.Flags.Set("ignoreMapIndexOk", "true") varnamelenAnalyzer.Flags.Set("ignoreChanRecvOk", "true") // Run with other analyzers in a multi-checker multichecker.Main( varnamelenAnalyzer, // Add other analyzers as needed ) } // Custom wrapper analyzer that includes varnamelen func CustomAnalyzer() *analysis.Analyzer { varnamelenAnalyzer := varnamelen.NewAnalyzer() return &analysis.Analyzer{ Name: "customlint", Doc: "Custom linter including varnamelen", Run: func(pass *analysis.Pass) (interface{}, error) { // Run varnamelen varnamelenAnalyzer.Run(pass) // Add custom analysis logic here return nil, nil }, Requires: []*analysis.Analyzer{ inspect.Analyzer, }, } } ``` -------------------------------- ### Build and Run Standalone Varnamelen CLI Source: https://context7.com/blizzy78/varnamelen/llms.txt Instructions for building and executing the varnamelen analyzer as a command-line tool. Demonstrates basic usage and various configuration flags for customizing analysis. ```bash # Build the standalone binary go build -o varnamelen ./cmd/ ``` ```bash # Basic usage - analyze current package ./varnamelen ./... ``` ```bash # Specify maximum distance (lines) for "small" scope ./varnamelen -maxDistance 8 ./... ``` ```bash # Specify minimum name length considered "long" ./varnamelen -minNameLength 4 ./... ``` ```bash # Enable checking of method receivers ./varnamelen -checkReceiver ./... ``` ```bash # Enable checking of named return values ./varnamelen -checkReturn ./... ``` ```bash # Enable checking of type parameters (generics) ./varnamelen -checkTypeParam ./... ``` ```bash # Ignore specific variable names ./varnamelen -ignoreNames "i,j,k,n" ./... ``` ```bash # Ignore specific declarations by name and type ./varnamelen -ignoreDecls "db *sql.DB,w http.ResponseWriter" ./... ``` ```bash # Ignore "ok" variables from type assertions ./varnamelen -ignoreTypeAssertOk ./... ``` ```bash # Ignore "ok" variables from map indexing ./varnamelen -ignoreMapIndexOk ./... ``` ```bash # Ignore "ok" variables from channel receives ./varnamelen -ignoreChanRecvOk ./... ``` ```bash # Combined configuration ./varnamelen \ -maxDistance 6 \ -minNameLength 3 \ -checkReceiver \ -checkReturn \ -ignoreNames "err,id" \ -ignoreDecls "w http.ResponseWriter,r *http.Request" \ -ignoreTypeAssertOk \ -ignoreMapIndexOk \ ./... ``` ```bash # Output in JSON format ./varnamelen -json ./... ``` -------------------------------- ### Build the standalone CLI utility Source: https://github.com/blizzy78/varnamelen/blob/master/README.md Compile the varnamelen command-line tool from the source code. ```bash go build -o varnamelen ./cmd/ ``` -------------------------------- ### Create New Analyzer with Default Settings Source: https://context7.com/blizzy78/varnamelen/llms.txt Instantiates the varnamelen analyzer with default configurations for max distance and minimum name length. This is the primary entry point for using the analyzer programmatically. ```go package main import ( "github.com/blizzy78/varnamelen" "golang.org/x/tools/go/analysis/singlechecker" ) func main() { // Create analyzer with default configuration: // - maxDistance: 5 (lines) // - minNameLength: 3 (characters) analyzer := varnamelen.NewAnalyzer() // Run as a standalone checker singlechecker.Main(analyzer) } ``` -------------------------------- ### Configure varnamelen in golangci-lint Source: https://github.com/blizzy78/varnamelen/blob/master/README.md Use this YAML configuration within your .golangci.yml file to customize analyzer behavior, such as scope distance and ignored declarations. ```yaml linters-settings: varnamelen: # The longest distance, in source lines, that is being considered a "small scope." (defaults to 5) # Variables used in at most this many lines will be ignored. max-distance: 5 # The minimum length of a variable's name that is considered "long." (defaults to 3) # Variable names that are at least this long will be ignored. min-name-length: 3 # Check method receivers. (defaults to false) check-receiver: false # Check named return values. (defaults to false) check-return: false # Check type parameters. (defaults to false) check-type-param: false # Ignore "ok" variables that hold the bool return value of a type assertion. (defaults to false) ignore-type-assert-ok: false # Ignore "ok" variables that hold the bool return value of a map index. (defaults to false) ignore-map-index-ok: false # Ignore "ok" variables that hold the bool return value of a channel receive. (defaults to false) ignore-chan-recv-ok: false # Optional list of variable names that should be ignored completely. (defaults to empty list) ignore-names: - err # Optional list of variable declarations that should be ignored completely. (defaults to empty list) # Entries must be in one of the following forms (see below for examples): # - for variables, parameters, named return values, method receivers, or type parameters: # ( can also be a pointer/slice/map/chan/...) # - for constants: const ignore-decls: - c echo.Context - t testing.T - f *foo.Bar - e error - i int - const C - T any - m map[string]int ``` -------------------------------- ### Configuring Ignore Declaration Patterns Source: https://context7.com/blizzy78/varnamelen/llms.txt Use ignore-decls to suppress warnings for specific variable name and type combinations. ```go package handlers import ( "net/http" "database/sql" ) // With ignore-decls: ["w http.ResponseWriter", "r *http.Request"] // these parameters won't trigger warnings: func HandleUser(w http.ResponseWriter, r *http.Request) { // w and r are used across many lines but ignored due to config if r.Method != http.MethodGet { w.WriteHeader(http.StatusMethodNotAllowed) return } // ... many more lines using w and r ... w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte(`{"status": "ok"}`)) } // With ignore-decls: ["db *sql.DB", "tx *sql.Tx"] func QueryUsers(db *sql.DB) ([]User, error) { // db is ignored due to config rows, err := db.Query("SELECT * FROM users") if err != nil { return nil, err } defer rows.Close() // ... many more lines using rows ... return users, nil } // With ignore-decls: ["const N", "const C"] const ( N = 100 // ignored - matches "const N" C = "config" // ignored - matches "const C" ) // With ignore-decls: ["T any", "K comparable", "V any"] func Map[T any, K comparable, V any](items []T, fn func(T) V) []V { // T, K, V are ignored due to config result := make([]V, len(items)) for i, item := range items { result[i] = fn(item) } return result } ``` -------------------------------- ### Automatically Ignored Go Patterns Source: https://context7.com/blizzy78/varnamelen/llms.txt Standard Go idioms like context, testing, and benchmark parameters are ignored by default without configuration. ```go package example import ( "context" "testing" ) // All these conventional declarations are automatically ignored: // ctx context.Context - standard context parameter func ProcessRequest(ctx context.Context, data []byte) error { // ctx is ignored - conventional Go pattern return ctx.Err() } // t *testing.T - standard test parameter func TestExample(t *testing.T) { // t is ignored - conventional Go pattern t.Log("test") } // b *testing.B - standard benchmark parameter func BenchmarkExample(b *testing.B) { // b is ignored - conventional Go pattern for i := 0; i < b.N; i++ { // work } } // f *testing.F - standard fuzz test parameter func FuzzExample(f *testing.F) { // f is ignored - conventional Go pattern f.Fuzz(func(t *testing.T, data []byte) {}) } // m *testing.M - standard TestMain parameter func TestMain(m *testing.M) { // m is ignored - conventional Go pattern m.Run() } // pb *testing.PB - standard parallel benchmark parameter func BenchmarkParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { // pb is ignored - conventional Go pattern for pb.Next() { // work } }) } // tb testing.TB - standard testing interface parameter func helperFunc(tb testing.TB) { // tb is ignored - conventional Go pattern tb.Helper() } ``` -------------------------------- ### Addressing Varnamelen Warnings Source: https://context7.com/blizzy78/varnamelen/llms.txt Varnamelen flags variables with short names that are used over a large scope. Renaming these variables to be more descriptive resolves the warnings. ```go // test.go - Code that triggers varnamelen warnings: package example func processData() { x := 123 // Line 4: variable 'x' is too short // ... many lines of code ... result := x * 2 // Line 15: x is used 11 lines after declaration i := 10 // Line 17: variable 'i' is too short // ... many lines of code ... for j := 0; j < i; j++ { // Line 28: i is used 11 lines after declaration // work } } // Output from varnamelen: // test.go:4:2: variable name 'x' is too short for the scope of its usage (varnamelen) // x := 123 // ^ // test.go:17:2: variable name 'i' is too short for the scope of its usage (varnamelen) // i := 10 // ^ // Fixed version with longer names: func processDataFixed() { multiplier := 123 // Descriptive name for wider scope // ... many lines of code ... result := multiplier * 2 iterations := 10 // Descriptive name for wider scope // ... many lines of code ... for j := 0; j < iterations; j++ { // 'j' is fine - used in small scope (loop only) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.