Skip to content

Commit 08e1523

Browse files
committed
S1017: guard against redefnitions of the len builtin
(cherry picked from commit 5bc9f4e)
1 parent 0a83c9b commit 08e1523

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

simple/lint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ func CheckTrim(pass *analysis.Pass) (interface{}, error) {
942942
if !ok {
943943
return false
944944
}
945-
if fn, ok := call.Fun.(*ast.Ident); !ok || fn.Name != "len" {
945+
if !code.IsCallTo(pass, call, "len") {
946946
return false
947947
}
948948
if len(call.Args) != 1 {
@@ -1059,7 +1059,7 @@ func CheckTrim(pass *analysis.Pass) (interface{}, error) {
10591059
if fun != "HasPrefix" {
10601060
return
10611061
}
1062-
if fn, ok := index.Fun.(*ast.Ident); !ok || fn.Name != "len" {
1062+
if !code.IsCallTo(pass, index, "len") {
10631063
return
10641064
}
10651065
if len(index.Args) != 1 {

simple/testdata/src/trim/trim.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,17 @@ func fn() {
139139
println(id1)
140140
}
141141
}
142+
143+
func fn3() {
144+
const s1 = "a string value"
145+
146+
var id1 = "a string value"
147+
len := func(string) int { return 0 } // don't accept non-builtin definition of len
148+
if strings.HasPrefix(id1, s1) {
149+
id1 = id1[len(s1):]
150+
}
151+
152+
if strings.HasSuffix(id1, s1) {
153+
id1 = id1[:len(id1)-len(s1)]
154+
}
155+
}

0 commit comments

Comments
 (0)