Skip to content

Commit 28a433c

Browse files
committed
require format param name 'format' and no return values to reduce false-positives count
1 parent 5d407d7 commit 28a433c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pkg/analyzer/analyzer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
2626
inspector.Preorder(nodeFilter, func(node ast.Node) {
2727
funcDecl := node.(*ast.FuncDecl)
2828

29+
if res := funcDecl.Type.Results; res != nil && len(res.List) != 0 {
30+
return
31+
}
32+
2933
params := funcDecl.Type.Params.List
3034
if len(params) < 2 { // [0] must be format (string), [1] must be args (...interface{})
3135
return
@@ -40,6 +44,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
4044
return
4145
}
4246

47+
if formatParamNames := params[len(params)-2].Names; len(formatParamNames) == 0 || formatParamNames[len(formatParamNames)-1].Name != "format" {
48+
return
49+
}
50+
4351
argsParamType, ok := params[len(params)-1].Type.(*ast.Ellipsis)
4452
if !ok { // args are not ellipsis (...args)
4553
return

testdata/src/p/p.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func printfLikeButWithStrings(format string, args ...string) {}
88

99
func printfLikeButWithBadFormat(format int, args ...interface{}) {}
1010

11-
func secondArgIsNotEllipsis(arg1 string, arg2 int) {}
11+
func secondArgIsNotEllipsis(format string, arg int) {}
1212

1313
func printfLikeButWithExtraInterfaceMethods(format string, args ...interface {
1414
String() string
@@ -17,12 +17,14 @@ func printfLikeButWithExtraInterfaceMethods(format string, args ...interface {
1717

1818
func prinfLikeFuncf(format string, args ...interface{}) {}
1919

20+
func prinfLikeFuncWithReturnValue(format string, args ...interface{}) string {
21+
return ""
22+
}
23+
24+
func prinfLikeFuncWithAnotherFormatArgName(msg string, args ...interface{}) {}
25+
2026
func prinfLikeFunc(format string, args ...interface{}) {} // want "printf-like formatting function"
2127

2228
func prinfLikeFuncWithExtraArgs1(extraArg, format string, args ...interface{}) {} // want "printf-like formatting function"
2329

2430
func prinfLikeFuncWithExtraArgs2(extraArg int, format string, args ...interface{}) {} // want "printf-like formatting function"
25-
26-
func prinfLikeFuncWithReturnValue(format string, args ...interface{}) string { // want "printf-like formatting function"
27-
return ""
28-
}

0 commit comments

Comments
 (0)