
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
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package checkers
|
|
|
|
import (
|
|
"go/ast"
|
|
|
|
"github.com/go-lintpack/lintpack"
|
|
"github.com/go-lintpack/lintpack/astwalk"
|
|
"github.com/go-toolsmith/astp"
|
|
)
|
|
|
|
func init() {
|
|
var info lintpack.CheckerInfo
|
|
info.Name = "initClause"
|
|
info.Tags = []string{"style", "opinionated", "experimental"}
|
|
info.Summary = "Detects non-assignment statements inside if/switch init clause"
|
|
info.Before = `if sideEffect(); cond {
|
|
}`
|
|
info.After = `sideEffect()
|
|
if cond {
|
|
}`
|
|
|
|
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
|
|
return astwalk.WalkerForStmt(&initClauseChecker{ctx: ctx})
|
|
})
|
|
}
|
|
|
|
type initClauseChecker struct {
|
|
astwalk.WalkHandler
|
|
ctx *lintpack.CheckerContext
|
|
}
|
|
|
|
func (c *initClauseChecker) VisitStmt(stmt ast.Stmt) {
|
|
initClause := c.getInitClause(stmt)
|
|
if initClause != nil && !astp.IsAssignStmt(initClause) {
|
|
c.warn(stmt, initClause)
|
|
}
|
|
}
|
|
|
|
func (c *initClauseChecker) getInitClause(x ast.Stmt) ast.Stmt {
|
|
switch x := x.(type) {
|
|
case *ast.IfStmt:
|
|
return x.Init
|
|
case *ast.SwitchStmt:
|
|
return x.Init
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (c *initClauseChecker) warn(stmt, clause ast.Stmt) {
|
|
name := "if"
|
|
if astp.IsSwitchStmt(stmt) {
|
|
name = "switch"
|
|
}
|
|
c.ctx.Warn(stmt, "consider to move `%s` before %s", clause, name)
|
|
}
|