Skip to content

Commit

Permalink
checkers: add Split handling to argOrder checker (go-critic#791)
Browse files Browse the repository at this point in the history
Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte authored Feb 2, 2019
1 parent e704e07 commit 34c1dc8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion checkers/argOrder_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *argOrderChecker) VisitExpr(expr ast.Expr) {
x := call.Args[0]
y := call.Args[1]
switch calledExpr.Sel.Name {
case "HasPrefix", "HasSuffix", "Contains", "TrimPrefix", "TrimSuffix":
case "HasPrefix", "HasSuffix", "Contains", "TrimPrefix", "TrimSuffix", "Split":
if obj.Name() != "bytes" && obj.Name() != "strings" {
return
}
Expand Down
20 changes: 20 additions & 0 deletions checkers/testdata/argOrder/negative_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@ func nonConstArgs(s1, s2 string, b1, b2 []byte) {
_ = bytes.HasPrefix([]byte(s1), b1)
}

func constOnlyArgs() {
_ = strings.HasPrefix("", "http://")
_ = bytes.HasPrefix([]byte{}, []byte("http://"))
_ = bytes.HasPrefix([]byte{}, []byte{'h', 't', 't', 'p', ':', '/', '/'})
_ = strings.Contains("", ":")
_ = bytes.Contains([]byte{}, []byte(":"))
_ = strings.TrimPrefix("", ":")
_ = bytes.TrimPrefix([]byte{}, []byte(":"))
_ = strings.TrimSuffix("", ":")
_ = bytes.TrimSuffix([]byte{}, []byte(":"))
_ = strings.Split("", "/")
_ = bytes.Split([]byte{}, []byte("/"))
}

func properArgsOrder(s string, b []byte) {
_ = strings.HasPrefix(s, "http://")
_ = bytes.HasPrefix(b, []byte("http://"))
_ = bytes.HasPrefix(b, []byte{'h', 't', 't', 'p', ':', '/', '/'})
_ = strings.Contains(s, ":")
_ = bytes.Contains(b, []byte(":"))
_ = strings.TrimPrefix(s, ":")
_ = bytes.TrimPrefix(b, []byte(":"))
_ = strings.TrimSuffix(s, ":")
_ = bytes.TrimSuffix(b, []byte(":"))
_ = strings.Split(s, "/")
_ = bytes.Split(b, []byte("/"))
}
5 changes: 5 additions & 0 deletions checkers/testdata/argOrder/positive_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func badArgOrder(s string, b []byte) {
_ = strings.TrimSuffix(":", s)
/*! probably meant `bytes.TrimSuffix(b, []byte(":"))` */
_ = bytes.TrimSuffix([]byte(":"), b)

/*! probably meant `strings.Split(s, "/")` */
_ = strings.Split("/", s)
/*! probably meant `bytes.Split(b, []byte("/"))` */
_ = bytes.Split([]byte("/"), b)
}

func argubleCases(s string) {
Expand Down

0 comments on commit 34c1dc8

Please sign in to comment.