1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00

fix: support method call from structures for string-format (#840)

* fix: support method call from structures for string-format

* fix: added test for MR
This commit is contained in:
Ricardo Martin Marcucci
2023-07-29 06:27:07 -03:00
committed by GitHub
parent 6a2c5b132e
commit a155d92342
3 changed files with 27 additions and 4 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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'/
}