Skip to content

Commit

Permalink
Suggest strings.ContainsFunc instead of strings.IndexFunc with Go 1.21+
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Aug 20, 2023
1 parent a3fb7ac commit 0cd4d75
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
10 changes: 9 additions & 1 deletion simple/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
return
}

var opts []report.Option
var r ast.Expr
switch funIdent.Name {
case "IndexRune":
Expand All @@ -366,6 +367,12 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
X: pkgIdent,
Sel: &ast.Ident{Name: "Contains"},
}
case "IndexFunc":
r = &ast.SelectorExpr{
X: pkgIdent,
Sel: &ast.Ident{Name: "ContainsFunc"},
}
opts = append(opts, report.MinimumStdlibVersion(21))
default:
return
}
Expand All @@ -381,9 +388,10 @@ func CheckStringsContains(pass *analysis.Pass) (interface{}, error) {
}
}

report.Report(pass, node, fmt.Sprintf("should use %s instead", report.Render(pass, r)),
opts = append(opts,
report.FilterGenerated(),
report.Fixes(edit.Fix(fmt.Sprintf("simplify use of %s", report.Render(pass, call.Fun)), edit.ReplaceWithNode(pass.Fset, node, r))))
report.Report(pass, node, fmt.Sprintf("should use %s instead", report.Render(pass, r)), opts...)
}
code.Preorder(pass, fn, (*ast.BinaryExpr)(nil))
return nil, nil
Expand Down
6 changes: 5 additions & 1 deletion simple/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ func TestAll(t *testing.T) {
"S1000": {{Dir: "example.com/CheckSingleCaseSelect"}},
"S1001": {{Dir: "example.com/CheckLoopCopy"}},
"S1002": {{Dir: "example.com/CheckIfBoolCmp"}},
"S1003": {{Dir: "example.com/CheckStringsContains"}},
"S1003": {
{Dir: "example.com/CheckStringsContains"},
{Dir: "example.com/CheckStringsContains_go120", Version: "1.20"},
{Dir: "example.com/CheckStringsContains_go121", Version: "1.21"},
},
"S1004": {{Dir: "example.com/CheckBytesCompare"}},
"S1005": {
{Dir: "example.com/CheckUnnecessaryBlank"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg

import (
"bytes"
"strings"
)

func fn() {
_ = bytes.IndexFunc(nil, func(r rune) bool { return false }) != -1
_ = strings.IndexFunc("", func(r rune) bool { return false }) != -1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg

import (
"bytes"
"strings"
)

func fn() {
_ = bytes.IndexFunc(nil, func(r rune) bool { return false }) != -1 //@ diag(`bytes.ContainsFunc`)
_ = strings.IndexFunc("", func(r rune) bool { return false }) != -1 //@ diag(`strings.ContainsFunc`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg

import (
"bytes"
"strings"
)

func fn() {
_ = bytes.ContainsFunc(nil, func(r rune) bool { return false }) //@ diag(`bytes.ContainsFunc`)
_ = strings.ContainsFunc("", func(r rune) bool { return false }) //@ diag(`strings.ContainsFunc`)
}

0 comments on commit 0cd4d75

Please sign in to comment.