16 "golang.org/x/tools/go/analysis"
17 "honnef.co/go/tools/code"
18 "honnef.co/go/tools/ir"
22 MsgInvalidHostPort = "invalid port or service name in host:port pair"
23 MsgInvalidUTF8 = "argument is not a valid UTF-8 encoded string"
24 MsgNonUniqueCutset = "cutset contains duplicate characters"
29 Instr ir.CallInstruction
37 func (c *Call) Invalid(msg string) {
38 c.invalids = append(c.invalids, msg)
41 type Argument struct {
50 func (arg *Argument) Invalid(msg string) {
51 arg.invalids = append(arg.invalids, msg)
54 type CallCheck func(call *Call)
56 func extractConsts(v ir.Value) []*ir.Const {
57 switch v := v.(type) {
60 case *ir.MakeInterface:
61 return extractConsts(v.X)
67 func ValidateRegexp(v Value) error {
68 for _, c := range extractConsts(v.Value) {
72 if c.Value.Kind() != constant.String {
75 s := constant.StringVal(c.Value)
76 if _, err := regexp.Compile(s); err != nil {
83 func ValidateTimeLayout(v Value) error {
84 for _, c := range extractConsts(v.Value) {
88 if c.Value.Kind() != constant.String {
91 s := constant.StringVal(c.Value)
92 s = strings.Replace(s, "_", " ", -1)
93 s = strings.Replace(s, "Z", "-", -1)
94 _, err := time.Parse(s, s)
102 func ValidateURL(v Value) error {
103 for _, c := range extractConsts(v.Value) {
107 if c.Value.Kind() != constant.String {
110 s := constant.StringVal(c.Value)
111 _, err := url.Parse(s)
113 return fmt.Errorf("%q is not a valid URL: %s", s, err)
119 func InvalidUTF8(v Value) bool {
120 for _, c := range extractConsts(v.Value) {
124 if c.Value.Kind() != constant.String {
127 s := constant.StringVal(c.Value)
128 if !utf8.ValidString(s) {
135 func UnbufferedChannel(v Value) bool {
136 // TODO(dh): this check of course misses many cases of unbuffered
137 // channels, such as any in phi or sigma nodes. We'll eventually
138 // replace this function.
140 if ct, ok := val.(*ir.ChangeType); ok {
143 mk, ok := val.(*ir.MakeChan)
147 if k, ok := mk.Size.(*ir.Const); ok && k.Value.Kind() == constant.Int {
148 if v, ok := constant.Int64Val(k.Value); ok && v == 0 {
155 func Pointer(v Value) bool {
156 switch v.Value.Type().Underlying().(type) {
157 case *types.Pointer, *types.Interface:
163 func ConvertedFromInt(v Value) bool {
164 conv, ok := v.Value.(*ir.Convert)
168 b, ok := conv.X.Type().Underlying().(*types.Basic)
172 if (b.Info() & types.IsInteger) == 0 {
178 func validEncodingBinaryType(pass *analysis.Pass, typ types.Type) bool {
179 typ = typ.Underlying()
180 switch typ := typ.(type) {
183 case types.Uint8, types.Uint16, types.Uint32, types.Uint64,
184 types.Int8, types.Int16, types.Int32, types.Int64,
185 types.Float32, types.Float64, types.Complex64, types.Complex128, types.Invalid:
188 return code.IsGoVersion(pass, 8)
193 for i := 0; i < n; i++ {
194 if !validEncodingBinaryType(pass, typ.Field(i).Type()) {
200 return validEncodingBinaryType(pass, typ.Elem())
201 case *types.Interface:
202 // we can't determine if it's a valid type or not
208 func CanBinaryMarshal(pass *analysis.Pass, v Value) bool {
209 typ := v.Value.Type().Underlying()
210 if ttyp, ok := typ.(*types.Pointer); ok {
211 typ = ttyp.Elem().Underlying()
213 if ttyp, ok := typ.(interface {
216 if _, ok := ttyp.(*types.Pointer); !ok {
221 return validEncodingBinaryType(pass, typ)
224 func RepeatZeroTimes(name string, arg int) CallCheck {
225 return func(call *Call) {
226 arg := call.Args[arg]
227 if k, ok := arg.Value.Value.(*ir.Const); ok && k.Value.Kind() == constant.Int {
228 if v, ok := constant.Int64Val(k.Value); ok && v == 0 {
229 arg.Invalid(fmt.Sprintf("calling %s with n == 0 will return no results, did you mean -1?", name))
235 func validateServiceName(s string) bool {
236 if len(s) < 1 || len(s) > 15 {
239 if s[0] == '-' || s[len(s)-1] == '-' {
242 if strings.Contains(s, "--") {
246 for _, r := range s {
247 if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') {
251 if r >= '0' && r <= '9' {
259 func validatePort(s string) bool {
260 n, err := strconv.ParseInt(s, 10, 64)
262 return validateServiceName(s)
264 return n >= 0 && n <= 65535
267 func ValidHostPort(v Value) bool {
268 for _, k := range extractConsts(v.Value) {
272 if k.Value.Kind() != constant.String {
275 s := constant.StringVal(k.Value)
276 _, port, err := net.SplitHostPort(s)
280 // TODO(dh): check hostname
281 if !validatePort(port) {
288 // ConvertedFrom reports whether value v was converted from type typ.
289 func ConvertedFrom(v Value, typ string) bool {
290 change, ok := v.Value.(*ir.ChangeType)
291 return ok && code.IsType(change.X.Type(), typ)
294 func UniqueStringCutset(v Value) bool {
295 for _, c := range extractConsts(v.Value) {
299 if c.Value.Kind() != constant.String {
302 s := constant.StringVal(c.Value)
308 for i, r := range rs[1:] {