Skip to content

Commit

Permalink
analyzer/testdata: more test rules (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte authored Oct 11, 2021
1 parent 4e7154c commit c6056a8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
16 changes: 16 additions & 0 deletions analyzer/testdata/src/extra/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,19 @@ func _() {

}
}

func quotedStringSprint(s string) {
_ = fmt.Sprintf(`"%s"`, s) // want `\Quse %q instead of "%s" for quoted strings`
_ = fmt.Sprintf(`foo "%s" bar`, s) // want `\Quse %q instead of "%s" for quoted strings`

_ = fmt.Sprintf("\"%s\"", s) // want `\Quse %q instead of "%s" for quoted strings`
_ = fmt.Sprintf("foo \"%s\" bar", s) // want `\Quse %q instead of "%s" for quoted strings`

_ = fmt.Sprintf(`%q`, s)
_ = fmt.Sprintf(`foo %q bar`, s)

_ = fmt.Sprintf("%q", s)
_ = fmt.Sprintf("foo %q bar", s)

_ = fmt.Sprintf("%s", s)
}
5 changes: 5 additions & 0 deletions analyzer/testdata/src/extra/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@ func testRules(m dsl.Matcher) {
m.Match(`$f($*_)`).
Where(m["$$"].Type.Is("error") && m["$$"].Node.Parent().Is("ExprStmt")).
Report(`don't ignore the $f result`)

m.Match(`fmt.Sprintf($s, $*_)`).
Where(m["s"].Text.Matches("^`.*\"%s\".*`$") ||
m["s"].Text.Matches(`^".*\\"%s\\".*"$`)).
Report(`use %q instead of "%s" for quoted strings`)
}
71 changes: 71 additions & 0 deletions analyzer/testdata/src/gocritic/f1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocritic

import (
"bytes"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -278,6 +279,76 @@ func offBy1(xs []int, ys []string) {
var m map[int]int
// Not an error. Doesn't panic.
_ = m[len(m)]

var s string
var b []byte
{
start := strings.Index(s, "/")
_ = s[start:] // want `\QIndex() can return -1; maybe you wanted to do s[start+1:]`
}
{
start := bytes.Index(b, []byte("/"))
_ = b[start:] // want `\QIndex() can return -1; maybe you wanted to do b[start+1:]`
}
{
_ = s[strings.Index(s, "/"):] // want `\QIndex() can return -1; maybe you wanted to do Index()+1`
}
{
_ = b[bytes.Index(b, []byte("/")):] // want `\QIndex() can return -1; maybe you wanted to do Index()+1`
}
{
start := strings.Index(s, "/")
res := s[start:] // want `\QIndex() can return -1; maybe you wanted to do s[start+1:]`
sink(res)
}
{
start := bytes.Index(b, []byte("/"))
res := b[start:] // want `\QIndex() can return -1; maybe you wanted to do b[start+1:]`
sink(res)
}

{
start := strings.Index(s, "/") + 1
_ = s[start:]
}
{
start := bytes.Index(b, []byte("/")) + 1
_ = b[start:]
}
{
start := strings.Index(s, "/")
_ = s[start+1:]
}
{
start := bytes.Index(b, []byte("/"))
_ = b[start+1:]
}
{
_ = s[strings.Index(s, "/")+1:]
}
{
_ = b[bytes.Index(b, []byte("/"))+1:]
}
{
start := strings.Index(s, "/") + 1
res := s[start:]
sink(res)
}
{
start := bytes.Index(b, []byte("/")) + 1
res := b[start:]
sink(res)
}
{
start := strings.Index(s, "/")
res := s[start+1:]
sink(res)
}
{
start := bytes.Index(b, []byte("/"))
res := b[start+1:]
sink(res)
}
}

func wrapperFunc(s string) {
Expand Down
14 changes: 14 additions & 0 deletions analyzer/testdata/src/gocritic/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ func testRules(m dsl.Matcher) {
Where(m["s"].Type.Is(`[]$elem`) && m["s"].Pure).
Report(`index expr always panics; maybe you wanted $s[len($s)-1]?`)

m.Match(
`$i := strings.Index($s, $_); $_ := $slicing[$i:]`,
`$i := strings.Index($s, $_); $_ = $slicing[$i:]`,
`$i := bytes.Index($s, $_); $_ := $slicing[$i:]`,
`$i := bytes.Index($s, $_); $_ = $slicing[$i:]`).
Where(m["s"].Text == m["slicing"].Text).
Report(`Index() can return -1; maybe you wanted to do $s[$i+1:]`).
At(m["slicing"])

m.Match(
`$s[strings.Index($s, $_):]`,
`$s[bytes.Index($s, $_):]`).
Report(`Index() can return -1; maybe you wanted to do Index()+1`)

m.Match(`*flag.Bool($*_)`,
`*flag.Float64($*_)`,
`*flag.Duration($*_)`,
Expand Down

0 comments on commit c6056a8

Please sign in to comment.