mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Motivation: add a check for a common issue I see humans and AI agents making, so that we don't have to waste time on it in code reviews. Resolves #42635 Note: This lint check has been mostly AI generated. I don't think it needs a thorough review because it is not production code and not even test code. Any issues will be obvious from usage by contributors. Add a custom go/analysis analyzer that detects map[T]bool variables used as sets (where only the literal `true` is ever assigned) and suggests using map[T]struct{} instead, which is the idiomatic Go approach for sets — zero memory for values and unambiguous semantics. The analyzer minimizes false positives by: - Only flagging when ALL indexed assignments use the literal `true` - Skipping variables initialized from function calls (unknown source) - Skipping variables reassigned from unknown sources - Skipping function parameters and exported package-level variables - Skipping range loop variables Integrated as an incremental linter (new/changed code only) to avoid breaking existing code. Running this check on our whole codebase flags valid cases: ``` cmd/fleet/serve.go:306:2: map[string]bool used as a set; consider map[string]struct{} instead (setboolcheck) allowedHostIdentifiers := map[string]bool{ ^ cmd/fleetctl/fleetctl/generate_gitops.go:189:3: map[string]bool used as a set; consider map[string]struct{} instead (setboolcheck) handled := make(map[string]bool, len(renames)*2) ^ cmd/fleetctl/fleetctl/generate_gitops.go:1593:2: map[uint]bool used as a set; consider map[uint]struct{} instead (setboolcheck) m := make(map[uint]bool, len(ids)) ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Added a new code analyzer to detect maps used as boolean sets and recommend more efficient alternatives for better performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Scott Gress <scottmgress@gmail.com> Co-authored-by: Scott Gress <scott@fleetdm.com>
27 lines
674 B
Go
27 lines
674 B
Go
// Package gclplugin provides the golangci-lint module plugin entry point.
|
|
package gclplugin
|
|
|
|
import (
|
|
"github.com/fleetdm/fleet/v4/tools/ci/setboolcheck"
|
|
"github.com/golangci/plugin-module-register/register"
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
func init() {
|
|
register.Plugin("setboolcheck", New)
|
|
}
|
|
|
|
// New returns the golangci-lint plugin for the setboolcheck analyzer.
|
|
func New(_ any) (register.LinterPlugin, error) {
|
|
return &plugin{}, nil
|
|
}
|
|
|
|
type plugin struct{}
|
|
|
|
func (p *plugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
|
|
return []*analysis.Analyzer{setboolcheck.Analyzer}, nil
|
|
}
|
|
|
|
func (p *plugin) GetLoadMode() string {
|
|
return register.LoadModeTypesInfo
|
|
}
|