-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexlits.go
39 lines (30 loc) · 819 Bytes
/
hexlits.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
package consistent
import (
"go/ast"
"go/token"
"strings"
"golang.org/x/tools/go/analysis"
)
const (
hexLitsLower = "lower"
hexLitsUpper = "upper"
)
var hexLitsFlagAllowedValues = []string{flagIgnore, hexLitsLower, hexLitsUpper}
func checkHexLit(pass *analysis.Pass, lit *ast.BasicLit, mode string) {
if mode == flagIgnore {
return
}
if !hexLit(lit) {
return
}
val := lit.Value[2:]
switch {
case mode == hexLitsLower && strings.ToLower(val) != val:
reportf(pass, lit.Pos(), "use lowercase digits in hex literal")
case mode == hexLitsUpper && strings.ToUpper(val) != val:
reportf(pass, lit.Pos(), "use uppercase digits in hex literal")
}
}
func hexLit(lit *ast.BasicLit) bool {
return lit.Kind == token.INT && (strings.HasPrefix(lit.Value, "0x") || strings.HasPrefix(lit.Value, "0X"))
}