Fix #324, relates #314 1. Update gocritic to the latest version 2. Use proper gocritic checkers repo, old repo was archived 3. Get enabled by default gocritic checks in sync with go-critic: don't enable performance, experimental and opinionated checks by default 4. Support of `enabled-tags` options for gocritic 5. Enable almost all gocritic checks for the project 6. Make rich debugging for gocritic 7. Meticulously validate gocritic checks config
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package checkers
|
|
|
|
import (
|
|
"go/ast"
|
|
|
|
"github.com/go-lintpack/lintpack"
|
|
"github.com/go-lintpack/lintpack/astwalk"
|
|
)
|
|
|
|
func init() {
|
|
var info lintpack.CheckerInfo
|
|
info.Name = "switchTrue"
|
|
info.Tags = []string{"style"}
|
|
info.Summary = "Detects switch-over-bool statements that use explicit `true` tag value"
|
|
info.Before = `
|
|
switch true {
|
|
case x > y:
|
|
}`
|
|
info.After = `
|
|
switch {
|
|
case x > y:
|
|
}`
|
|
|
|
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
|
|
return astwalk.WalkerForStmt(&switchTrueChecker{ctx: ctx})
|
|
})
|
|
}
|
|
|
|
type switchTrueChecker struct {
|
|
astwalk.WalkHandler
|
|
ctx *lintpack.CheckerContext
|
|
}
|
|
|
|
func (c *switchTrueChecker) VisitStmt(stmt ast.Stmt) {
|
|
if stmt, ok := stmt.(*ast.SwitchStmt); ok {
|
|
if qualifiedName(stmt.Tag) == "true" {
|
|
c.warn(stmt)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *switchTrueChecker) warn(cause *ast.SwitchStmt) {
|
|
if cause.Init == nil {
|
|
c.ctx.Warn(cause, "replace 'switch true {}' with 'switch {}'")
|
|
} else {
|
|
c.ctx.Warn(cause, "replace 'switch %s; true {}' with 'switch %s; {}'",
|
|
cause.Init, cause.Init)
|
|
}
|
|
}
|