This repository was archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflagName_checker.go
56 lines (48 loc) · 1.55 KB
/
flagName_checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package checkers
import (
"go/ast"
"go/constant"
"strings"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcast"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "flagName"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects flag names with whitespace"
info.Before = `b := flag.Bool(" foo ", false, "description")`
info.After = `b := flag.Bool("foo", false, "description")`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&flagNameChecker{ctx: ctx})
})
}
type flagNameChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
func (c *flagNameChecker) VisitExpr(expr ast.Expr) {
call := astcast.ToCallExpr(expr)
switch qualifiedName(call.Fun) {
case "flag.Bool", "flag.Duration", "flag.Float64", "flag.String",
"flag.Int", "flag.Int64", "flag.Uint", "flag.Uint64":
c.checkFlagName(call, call.Args[0])
case "flag.BoolVar", "flag.DurationVar", "flag.Float64Var", "flag.StringVar",
"flag.IntVar", "flag.Int64Var", "flag.UintVar", "flag.Uint64Var":
c.checkFlagName(call, call.Args[1])
}
}
func (c *flagNameChecker) checkFlagName(call *ast.CallExpr, arg ast.Expr) {
cv := c.ctx.TypesInfo.Types[arg].Value
if cv == nil {
return // Non-constant name
}
name := constant.StringVal(cv)
if strings.Contains(name, " ") {
c.warnWhitespace(call, name)
}
}
func (c *flagNameChecker) warnWhitespace(cause ast.Node, name string) {
c.ctx.Warn(cause, "flag name %q contains whitespace", name)
}