Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest strings.ContainsFunc instead of strings.IndexFunc with Go 1.21+ #1435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)
}