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

fix(1007): return Sel.Name as FuncName when selector is an CallExpr #1012

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
9 changes: 7 additions & 2 deletions rule/add-constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
onFailure: onFailure,
strLits: make(map[string]int),
strLitLimit: r.strLitLimit,
allowList: r.allowList,
allowList: r.allowList,
ignoreFunctions: r.ignoreFunctions,
structTags: make(map[*ast.BasicLit]struct{}),
}
Expand All @@ -72,7 +72,7 @@ type lintAddConstantRule struct {
onFailure func(lint.Failure)
strLits map[string]int
strLitLimit int
allowList allowList
allowList allowList
ignoreFunctions []*regexp.Regexp
structTags map[*ast.BasicLit]struct{}
}
Expand Down Expand Up @@ -127,6 +127,11 @@ func (*lintAddConstantRule) getFuncName(expr *ast.CallExpr) string {
switch prefix := f.X.(type) {
case *ast.Ident:
return prefix.Name + "." + f.Sel.Name
case *ast.CallExpr:
// If the selector is an CallExpr, like `fn().Info`, we return `.Info` as function name
if f.Sel != nil {
return "." + f.Sel.Name
}
}
case *ast.Ident:
return f.Name
Expand Down
2 changes: 1 addition & 1 deletion test/add-constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAddConstant(t *testing.T) {
"allowStrs": "\"\"",
"allowInts": "0,1,2",
"allowFloats": "0.0,1.0",
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc",
"ignoreFuncs": "os\\.(CreateFile|WriteFile|Chmod|FindProcess),\\.Println,ignoredFunc,\\.Info",
}}

testRule(t, "add-constant", &rule.AddConstantRule{}, &lint.RuleConfig{
Expand Down
15 changes: 15 additions & 0 deletions testdata/add-constant.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package fixtures

import (
"context"
"fmt"
"os"
)

type testLogger struct{}

func (l *testLogger) Info(ctx context.Context, msg string) {}

func getLogger() *testLogger {
return &testLogger{}
}

func test1007() {
getLogger().Info(context.Background(), "test1007")
getLogger().Info(context.Background(), "test1007")
getLogger().Info(context.Background(), "test1007")
}

func foo(a float32, b string, c any, d int) {
a = 1.0 // ignore
b = "ignore"
Expand Down
Loading