-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolcmp.go
49 lines (43 loc) · 862 Bytes
/
boolcmp.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
package boolcmp
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
)
const doc = "boolcmp is ..."
// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "boolcmp",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{},
}
func run(pass *analysis.Pass) (any, error) {
for _, f := range pass.Files {
ast.Inspect(f, func(n ast.Node) bool {
if n == nil {
return false
}
switch x := n.(type) {
case *ast.BinaryExpr:
if isComparison(x.Op) {
if lhs := pass.TypesInfo.Types[x.X].Type; lhs != nil {
if lhs.Underlying().String() == "bool" {
pass.Reportf(x.Pos(), "bool value used in comparison")
}
}
}
}
return true
})
}
return nil, nil
}
func isComparison(op token.Token) bool {
switch op {
case token.EQL, token.NEQ:
return true
default:
return false
}
}