Documentation
¶
Overview ¶
Package rules implements the built-in pedagogical rules for go-idiomatic.
Every rule is a value exposing the Rule interface and also exposes an *analysis.Analyzer via Analyzer / Analyzers so it can be plugged into singlechecker, multichecker, or analysistest.Run.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Analyzer = newAnalyzer("goidiomatic", "pedagogical Go-idiom checks beyond golangci-lint", All())
Analyzer wraps every built-in rule as a single Analyzer suitable for singlechecker / multichecker.
Functions ¶
Types ¶
type Rule ¶
type Rule interface {
ID() string
Name() string
Description() string
Severity() Severity
Check(pass *analysis.Pass) ([]Finding, error)
}
Rule is a single pedagogical check.
var AnyOverEmptyInterface Rule = anyOverEmptyInterface{}
AnyOverEmptyInterface flags uses of interface{} and suggests any, but only when the build context's language version is Go 1.18 or newer. On older targets the suggestion would be a compile error, so the rule stays silent.
var ContextFirstArg Rule = contextFirstArg{}
ContextFirstArg flags functions that take context.Context as any parameter other than the first. The Go convention is that Context is always the first parameter, named ctx.
var ErrorsIsAs Rule = errorsIsAs{}
ErrorsIsAs flags two common anti-patterns:
- comparing err.Error() to a literal string
- comparing an error value to a sentinel with ==
Both should use errors.Is (or errors.As for type switches) so wrapped errors still match. The check is structural, augmented with type info when it is available.
var NoSleepForCoordination Rule = noSleepForCoordination{}
NoSleepForCoordination flags time.Sleep calls inside a function that also uses a channel operation or a context.Context. Sleeping as a way to wait for "the other thing" to happen is almost always a race.
var PreferRangeInt Rule = preferRangeInt{}
PreferRangeInt flags classic indexed loops that could be rewritten with range, for example for i := 0; i < len(xs); i++. It ignores countdowns and steps other than i++.