Skip to content

Commit 7fb55ea

Browse files
committed
Suggest strings.ContainsFunc instead of strings.IndexFunc with Go 1.21+
1 parent a3fb7ac commit 7fb55ea

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

simple/lint.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
349349
return
350350
}
351351

352+
opts := []report.Option{}
352353
var r ast.Expr
353354
switch funIdent.Name {
354355
case "IndexRune":
@@ -366,6 +367,12 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
366367
X: pkgIdent,
367368
Sel: &ast.Ident{Name: "Contains"},
368369
}
370+
case "IndexFunc":
371+
r = &ast.SelectorExpr{
372+
X: pkgIdent,
373+
Sel: &ast.Ident{Name: "ContainsFunc"},
374+
}
375+
opts = append(opts, report.MinimumStdlibVersion(21))
369376
default:
370377
return
371378
}
@@ -381,9 +388,10 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
381388
}
382389
}
383390

384-
report.Report(pass, node, fmt.Sprintf("should use %s instead", report.Render(pass, r)),
391+
opts = append(opts,
385392
report.FilterGenerated(),
386393
report.Fixes(edit.Fix(fmt.Sprintf("simplify use of %s", report.Render(pass, call.Fun)), edit.ReplaceWithNode(pass.Fset, node, r))))
394+
report.Report(pass, node, fmt.Sprintf("should use %s instead", report.Render(pass, r)), opts...)
387395
}
388396
code.Preorder(pass, fn, (*ast.BinaryExpr)(nil))
389397
return nil, nil

simple/lint_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ func TestAll(t *testing.T) {
1111
"S1000": {{Dir: "example.com/CheckSingleCaseSelect"}},
1212
"S1001": {{Dir: "example.com/CheckLoopCopy"}},
1313
"S1002": {{Dir: "example.com/CheckIfBoolCmp"}},
14-
"S1003": {{Dir: "example.com/CheckStringsContains"}},
14+
"S1003": {
15+
{Dir: "example.com/CheckStringsContains"},
16+
{Dir: "example.com/CheckStringsContains_go120", Version: "1.20"},
17+
{Dir: "example.com/CheckStringsContains_go121", Version: "1.21"},
18+
},
1519
"S1004": {{Dir: "example.com/CheckBytesCompare"}},
1620
"S1005": {
1721
{Dir: "example.com/CheckUnnecessaryBlank"},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
)
7+
8+
func fn() {
9+
_ = bytes.IndexFunc(nil, func(r rune) bool { return false }) != -1
10+
_ = strings.IndexFunc("", func(r rune) bool { return false }) != -1
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
)
7+
8+
func fn() {
9+
_ = bytes.IndexFunc(nil, func(r rune) bool { return false }) != -1 //@ diag(`bytes.ContainsFunc`)
10+
_ = strings.IndexFunc("", func(r rune) bool { return false }) != -1 //@ diag(`strings.ContainsFunc`)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
)
7+
8+
func fn() {
9+
_ = bytes.ContainsFunc(nil, func(r rune) bool { return false }) //@ diag(`bytes.ContainsFunc`)
10+
_ = strings.ContainsFunc("", func(r rune) bool { return false }) //@ diag(`strings.ContainsFunc`)
11+
}

0 commit comments

Comments
 (0)