Skip to content

Commit 7ef710a

Browse files
authored
fix(1007): return Sel.Name as FuncName when selector is an CallExpr (#1012)
Signed-off-by: lsytj0413 <[email protected]>
1 parent a2174a3 commit 7ef710a

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

rule/add-constant.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
5353
onFailure: onFailure,
5454
strLits: make(map[string]int),
5555
strLitLimit: r.strLitLimit,
56-
allowList: r.allowList,
56+
allowList: r.allowList,
5757
ignoreFunctions: r.ignoreFunctions,
5858
structTags: make(map[*ast.BasicLit]struct{}),
5959
}
@@ -72,7 +72,7 @@ type lintAddConstantRule struct {
7272
onFailure func(lint.Failure)
7373
strLits map[string]int
7474
strLitLimit int
75-
allowList allowList
75+
allowList allowList
7676
ignoreFunctions []*regexp.Regexp
7777
structTags map[*ast.BasicLit]struct{}
7878
}
@@ -127,6 +127,11 @@ func (*lintAddConstantRule) getFuncName(expr *ast.CallExpr) string {
127127
switch prefix := f.X.(type) {
128128
case *ast.Ident:
129129
return prefix.Name + "." + f.Sel.Name
130+
case *ast.CallExpr:
131+
// If the selector is an CallExpr, like `fn().Info`, we return `.Info` as function name
132+
if f.Sel != nil {
133+
return "." + f.Sel.Name
134+
}
130135
}
131136
case *ast.Ident:
132137
return f.Name

test/add-constant_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAddConstant(t *testing.T) {
1313
"allowStrs": "\"\"",
1414
"allowInts": "0,1,2",
1515
"allowFloats": "0.0,1.0",
16-
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc",
16+
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc,\\.Info",
1717
}}
1818

1919
testRule(t, "add-constant", &rule.AddConstantRule{}, &lint.RuleConfig{

testdata/add-constant.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
package fixtures
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
)
78

9+
type testLogger struct{}
10+
11+
func (l *testLogger) Info(ctx context.Context, msg string) {}
12+
13+
func getLogger() *testLogger {
14+
return &testLogger{}
15+
}
16+
17+
func test1007() {
18+
getLogger().Info(context.Background(), "test1007")
19+
getLogger().Info(context.Background(), "test1007")
20+
getLogger().Info(context.Background(), "test1007")
21+
}
22+
823
func foo(a float32, b string, c any, d int) {
924
a = 1.0 // ignore
1025
b = "ignore"

0 commit comments

Comments
 (0)