This repository has been 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
/
underef_checker.go
127 lines (112 loc) · 2.91 KB
/
underef_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package checkers
import (
"go/ast"
"go/types"
"github.com/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astp"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "underef"
info.Tags = []string{"style"}
info.Params = lintpack.CheckerParams{
"skipRecvDeref": {
Value: true,
Usage: "whether to skip (*x).method() calls where x is a pointer receiver",
},
}
info.Summary = "Detects dereference expressions that can be omitted"
info.Before = `
(*k).field = 5
v := (*a)[5] // only if a is array`
info.After = `
k.field = 5
v := a[5]`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
c := &underefChecker{ctx: ctx}
c.skipRecvDeref = info.Params.Bool("skipRecvDeref")
return astwalk.WalkerForExpr(c)
})
}
type underefChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
skipRecvDeref bool
}
func (c *underefChecker) VisitExpr(expr ast.Expr) {
switch n := expr.(type) {
case *ast.SelectorExpr:
expr := lintutil.AsParenExpr(n.X)
if c.skipRecvDeref && c.isPtrRecvMethodCall(n.Sel) {
return
}
if expr, ok := expr.X.(*ast.StarExpr); ok {
if c.checkStarExpr(expr) {
c.warnSelect(n)
}
}
case *ast.IndexExpr:
expr := lintutil.AsParenExpr(n.X)
if expr, ok := expr.X.(*ast.StarExpr); ok {
if !c.checkStarExpr(expr) {
return
}
if c.checkArray(expr) {
c.warnArray(n)
}
}
}
}
func (c *underefChecker) isPtrRecvMethodCall(fn *ast.Ident) bool {
typ, ok := c.ctx.TypesInfo.TypeOf(fn).(*types.Signature)
if ok && typ != nil && typ.Recv() != nil {
_, ok := typ.Recv().Type().(*types.Pointer)
return ok
}
return false
}
func (c *underefChecker) underef(x *ast.ParenExpr) ast.Expr {
// If there is only 1 deref, can remove parenthesis,
// otherwise can remove StarExpr only.
dereferenced := x.X.(*ast.StarExpr).X
if astp.IsStarExpr(dereferenced) {
return &ast.ParenExpr{X: dereferenced}
}
return dereferenced
}
func (c *underefChecker) warnSelect(expr *ast.SelectorExpr) {
// TODO: add () to function output.
c.ctx.Warn(expr, "could simplify %s to %s.%s",
expr,
c.underef(expr.X.(*ast.ParenExpr)),
expr.Sel.Name)
}
func (c *underefChecker) warnArray(expr *ast.IndexExpr) {
c.ctx.Warn(expr, "could simplify %s to %s[%s]",
expr,
c.underef(expr.X.(*ast.ParenExpr)),
expr.Index)
}
// checkStarExpr checks if ast.StarExpr could be simplified.
func (c *underefChecker) checkStarExpr(expr *ast.StarExpr) bool {
typ, ok := c.ctx.TypesInfo.TypeOf(expr.X).Underlying().(*types.Pointer)
if !ok {
return false
}
switch typ.Elem().Underlying().(type) {
case *types.Pointer, *types.Interface:
return false
default:
return true
}
}
func (c *underefChecker) checkArray(expr *ast.StarExpr) bool {
typ, ok := c.ctx.TypesInfo.TypeOf(expr.X).(*types.Pointer)
if !ok {
return false
}
_, ok = typ.Elem().(*types.Array)
return ok
}