mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
fix(1007): return Sel.Name as FuncName when selector is an CallExpr (#1012)
Signed-off-by: lsytj0413 <511121939@qq.com>
This commit is contained in:
@@ -127,6 +127,11 @@ func (*lintAddConstantRule) getFuncName(expr *ast.CallExpr) string {
|
|||||||
switch prefix := f.X.(type) {
|
switch prefix := f.X.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
return prefix.Name + "." + f.Sel.Name
|
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:
|
case *ast.Ident:
|
||||||
return f.Name
|
return f.Name
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func TestAddConstant(t *testing.T) {
|
|||||||
"allowStrs": "\"\"",
|
"allowStrs": "\"\"",
|
||||||
"allowInts": "0,1,2",
|
"allowInts": "0,1,2",
|
||||||
"allowFloats": "0.0,1.0",
|
"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{
|
testRule(t, "add-constant", &rule.AddConstantRule{}, &lint.RuleConfig{
|
||||||
|
|||||||
15
testdata/add-constant.go
vendored
15
testdata/add-constant.go
vendored
@@ -1,10 +1,25 @@
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"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) {
|
func foo(a float32, b string, c any, d int) {
|
||||||
a = 1.0 // ignore
|
a = 1.0 // ignore
|
||||||
b = "ignore"
|
b = "ignore"
|
||||||
|
|||||||
Reference in New Issue
Block a user