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
/
boolExprSimplify_checker.go
148 lines (132 loc) · 3.65 KB
/
boolExprSimplify_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package checkers
import (
"go/ast"
"go/token"
"github.com/go-critic/checkers/internal/lintutil"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/typep"
"golang.org/x/tools/go/ast/astutil"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "boolExprSimplify"
info.Tags = []string{"style", "experimental"}
info.Summary = "Detects bool expressions that can be simplified"
info.Before = `
a := !(elapsed >= expectElapsedMin)
b := !(x) == !(y)`
info.After = `
a := elapsed < expectElapsedMin
b := (x) == (y)`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForExpr(&boolExprSimplifyChecker{ctx: ctx})
})
}
type boolExprSimplifyChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
func (c *boolExprSimplifyChecker) VisitExpr(x ast.Expr) {
// TODO: avoid eager copy?
// Can't be stable until wasted copying is fixed.
y := c.simplifyBool(astcopy.Expr(x))
if !astequal.Expr(x, y) {
c.warn(x, y)
}
}
func (c *boolExprSimplifyChecker) simplifyBool(x ast.Expr) ast.Expr {
return astutil.Apply(x, nil, func(cur *astutil.Cursor) bool {
return c.doubleNegation(cur) ||
c.negatedEquals(cur) ||
c.invertComparison(cur) ||
c.combineChecks(cur) ||
true
}).(ast.Expr)
}
func (c *boolExprSimplifyChecker) doubleNegation(cur *astutil.Cursor) bool {
neg1 := lintutil.AsUnaryExprOp(cur.Node(), token.NOT)
neg2 := lintutil.AsUnaryExprOp(astutil.Unparen(neg1.X), token.NOT)
if !lintutil.IsNil(neg1) && !lintutil.IsNil(neg2) {
cur.Replace(astutil.Unparen(neg2.X))
return true
}
return false
}
func (c *boolExprSimplifyChecker) negatedEquals(cur *astutil.Cursor) bool {
x, ok := cur.Node().(*ast.BinaryExpr)
if !ok || x.Op != token.EQL {
return false
}
neg1 := lintutil.AsUnaryExprOp(x.X, token.NOT)
neg2 := lintutil.AsUnaryExprOp(x.Y, token.NOT)
if !lintutil.IsNil(neg1) && !lintutil.IsNil(neg2) {
x.X = neg1.X
x.Y = neg2.X
return true
}
return false
}
func (c *boolExprSimplifyChecker) invertComparison(cur *astutil.Cursor) bool {
neg := lintutil.AsUnaryExprOp(cur.Node(), token.NOT)
cmp := lintutil.AsBinaryExpr(astutil.Unparen(neg.X))
if lintutil.IsNil(neg) || lintutil.IsNil(cmp) {
return false
}
// Replace operator to its negated form.
switch cmp.Op {
case token.EQL:
cmp.Op = token.NEQ
case token.NEQ:
cmp.Op = token.EQL
case token.LSS:
cmp.Op = token.GEQ
case token.GTR:
cmp.Op = token.LEQ
case token.LEQ:
cmp.Op = token.GTR
case token.GEQ:
cmp.Op = token.LSS
default:
return false
}
cur.Replace(cmp)
return true
}
func (c *boolExprSimplifyChecker) combineChecks(cur *astutil.Cursor) bool {
or := lintutil.AsBinaryExprOp(cur.Node(), token.LOR)
lhs := lintutil.AsBinaryExpr(astutil.Unparen(or.X))
rhs := lintutil.AsBinaryExpr(astutil.Unparen(or.Y))
if !astequal.Expr(lhs.X, rhs.X) || !astequal.Expr(lhs.Y, rhs.Y) {
return false
}
safe := typep.SideEffectFree(c.ctx.TypesInfo, lhs.X) &&
typep.SideEffectFree(c.ctx.TypesInfo, lhs.Y)
if !safe {
return false
}
combTable := [...]struct {
x token.Token
y token.Token
result token.Token
}{
{token.GTR, token.EQL, token.GEQ},
{token.EQL, token.GTR, token.GEQ},
{token.LSS, token.EQL, token.LEQ},
{token.EQL, token.LSS, token.LEQ},
}
for _, comb := range &combTable {
if comb.x == lhs.Op && comb.y == rhs.Op {
lhs.Op = comb.result
cur.Replace(lhs)
return true
}
}
return false
}
func (c *boolExprSimplifyChecker) warn(cause, suggestion ast.Expr) {
c.SkipChilds = true
c.ctx.Warn(cause, "can simplify `%s` to `%s`", cause, suggestion)
}