$ show GaijinEntertainment/go-exhaustruct
exhaustruct is a Go static analysis tool that finds struct literals with uninitialized fields. It operates as a standalone CLI tool or as an integrated linter within golangci-lint. The analyzer inspects all composite literals in Go source code, resolves their types using go/types, and reports missing field initializations based on configurable rules: comment directives (//exhaustruct:optional, :ignore, :enforce), regex patterns for type inclusion/exclusion, and various flags for allowing empty literals in specific contexts (returns, declarations, blank assignments). It supports both implicit (check all) and explicit (opt-in) modes, and provides automatic migration from deprecated struct tags to comment directives. The architecture is modular and highly crafted: the analyzer package orchestrates inspection, while internal packages handle caching, pattern matching, directive scanning, AST file parsing with correct handling of //line directives, and struct metadata resolution with promotion indexing for composite literals that name promoted fields (Go 1.27+). The codebase demonstrates exceptional attention to edge cases: concurrent access to shared caches, type parameter constraint resolution for generic structs, proper handling of embedded fields across packages, version-gated promotion resolution, and robust diagnostic generation with suggested fixes for tag migration. The implementation is production-ready with comprehensive test coverage including benchmarks, race detection, and dogfooding (running exhaustruct on its own code). The internal cache package provides a generic GetOrSet with once-only computation semantics that could be reused in other projects, and the pattern list offers full-string matching with alternation-aware longest match. The code is well-documented, with clear separation of concerns and thorough testing of all directive combinations and edge cases.
The LLM identified 4 files worth highlighting for reuse:
A generic, thread-safe cache with hit/miss tracking. Provides GetOrSet that computes a value once for each key and releases all concurrent callers with the same value. The implementation avoids holding locks during computation, using a pending map with channels for coordination. Handles panics in compute gracefully by releasing waiters. Could be extracted as a standalone library for any Go project needing concurrent caching with once-per-key semantics.
A collection of compiled regular expressions optimized for full-string matching. Compiles patterns with Longest() to handle alternations reliably. Provides MatchFullString and MatchFullStringExcept methods, the latter distinguishing patterns that match a specific target from broader patterns that also match an excepted string. Handles author-anchored patterns (^, $, \A, \z) correctly. Reusable for any project needing precise pattern matching with exclusion semantics.
Thread-safe scanner that extracts exhaustruct comment directives from Go source files. Correctly handles //line directives by using physical file positions for cache keys and lookups, so directives in generated code are found correctly. Supports both line and block comment forms, detects conflicts (multiple directives targeting the same line), and reports parse errors (unknown directives, duplicates). The scanner integrates with FileParser for on-demand parsing of dependency files, ensuring diagnostics belong to the owning package even when triggered by another package's resolution.
AST file parser with pluggable callbacks that ensures each file is processed only once. Provides PhysicalPosition, PhysicalLine, and PhysicalFilename utilities that ignore //line directives to return on-disk positions. Skips standard library files (identified by $GOROOT prefix or actual GOROOT path) to avoid unnecessary parsing. Thread-safe with concurrent file processing supported via read-write locks. Useful for any static analysis tool that needs to parse Go files with correct physical positioning.