diff --git a/rule/string-format.go b/rule/string-format.go index 0e30ebf..3edd624 100644 --- a/rule/string-format.go +++ b/rule/string-format.go @@ -211,10 +211,14 @@ func (lintStringFormatRule) getCallName(call *ast.CallExpr) (callName string, ok if selector, ok := call.Fun.(*ast.SelectorExpr); ok { // Scoped function call scope, ok := selector.X.(*ast.Ident) - if !ok { - return "", false + if ok { + return scope.Name + "." + selector.Sel.Name, true + } + // Scoped function call inside structure + recv, ok := selector.X.(*ast.SelectorExpr) + if ok { + return recv.Sel.Name + "." + selector.Sel.Name, true } - return scope.Name + "." + selector.Sel.Name, true } return "", false diff --git a/test/string-format_test.go b/test/string-format_test.go index f58c10c..1d425c6 100644 --- a/test/string-format_test.go +++ b/test/string-format_test.go @@ -21,7 +21,11 @@ func TestStringFormat(t *testing.T) { []interface{}{ "s.Method3[2]", "!/^[Tt][Hh]/", - "must not start with 'th'"}}}) + "must not start with 'th'"}, + []interface{}{ + "s.Method4", // same as before, but called from a struct + "!/^[Ot][Tt]/", + "must not start with 'ot'"}}}) } func TestStringFormatArgumentParsing(t *testing.T) { diff --git a/testdata/string-format.go b/testdata/string-format.go index 69c3204..08e0fa2 100644 --- a/testdata/string-format.go +++ b/testdata/string-format.go @@ -18,6 +18,16 @@ func (s stringFormatMethods) Method3(a, b, c string) { } +type stringFormatMethodsInjected struct{} + +func (s stringFormatMethodsInjected) Method4(a, b, c string) { + +} + +type container struct { + s stringFormatMethodsInjected +} + func stringFormat() { stringFormatMethod1("This string is fine", "") stringFormatMethod1("this string is not capitalized", "") // MATCH /must start with a capital letter/ @@ -27,4 +37,9 @@ func stringFormat() { d: "This string is capitalized, but ends with a period."}) // MATCH /string literal doesn't match user defined regex /[^\.]$// s := stringFormatMethods{} s.Method3("", "", "This string starts with th") // MATCH /must not start with 'th'/ + + c := container{ + s: stringFormatMethods{}, + } + c.s.Method4("Other string starts with ot") // MATCH /must not start with 'ot'/ }