-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
argOrder_checker.go
98 lines (84 loc) · 2.37 KB
/
argOrder_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package checkers
import (
"go/ast"
"go/types"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astp"
"github.com/go-toolsmith/typep"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "argOrder"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects suspicious arguments order"
info.Before = `strings.HasPrefix("#", userpass)`
info.After = `strings.HasPrefix(userpass, "#")`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&argOrderChecker{ctx: ctx})
})
}
type argOrderChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
func (c *argOrderChecker) VisitExpr(expr ast.Expr) {
call := astcast.ToCallExpr(expr)
// For now only handle functions of 2 args.
// TODO(Quasilyte): generalize the algorithm and add more patterns.
if len(call.Args) != 2 {
return
}
calledExpr := astcast.ToSelectorExpr(call.Fun)
obj, ok := c.ctx.TypesInfo.ObjectOf(astcast.ToIdent(calledExpr.X)).(*types.PkgName)
if !ok || !isStdlibPkg(obj.Imported()) {
return
}
x := call.Args[0]
y := call.Args[1]
switch calledExpr.Sel.Name {
case "HasPrefix", "HasSuffix", "Contains", "TrimPrefix", "TrimSuffix", "Split":
if obj.Name() != "bytes" && obj.Name() != "strings" {
return
}
if c.isConstLiteral(x) && !c.isConstLiteral(y) {
c.warn(call)
}
}
}
func (c *argOrderChecker) isConstLiteral(x ast.Expr) bool {
if c.ctx.TypesInfo.Types[x].Value != nil {
return true
}
// Also permit byte slices.
switch x := x.(type) {
case *ast.CallExpr:
// Handle `[]byte("abc")` as well.
if len(x.Args) != 1 || !astp.IsBasicLit(x.Args[0]) {
return false
}
typ, ok := c.ctx.TypesInfo.TypeOf(x.Fun).(*types.Slice)
return ok && typep.HasUint8Kind(typ.Elem())
case *ast.CompositeLit:
// Check if it's a const byte slice.
typ, ok := c.ctx.TypesInfo.TypeOf(x).(*types.Slice)
if !ok || !typep.HasUint8Kind(typ.Elem()) {
return false
}
for _, elt := range x.Elts {
if !astp.IsBasicLit(elt) {
return false
}
}
return true
default:
return false
}
}
func (c *argOrderChecker) warn(call *ast.CallExpr) {
fixed := astcopy.CallExpr(call)
fixed.Args[0], fixed.Args[1] = fixed.Args[1], fixed.Args[0]
c.ctx.Warn(call, "probably meant `%s`", fixed)
}