Skip to content

Commit d0b0385

Browse files
committed
refactor: factorize createFuncInfo
1 parent 725e4d9 commit d0b0385

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

report.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// because [os.CreateTemp] takes 2 args.
1212
const nbArgCreateTemp = 2
1313

14-
func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo FuncInfo) bool {
14+
func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo *FuncInfo) bool {
1515
if !a.osCreateTemp {
1616
return false
1717
}
@@ -64,7 +64,7 @@ func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo
6464
return false
6565
}
6666

67-
func (a *analyzer) reportSelector(pass *analysis.Pass, sel *ast.SelectorExpr, fnInfo FuncInfo) {
67+
func (a *analyzer) reportSelector(pass *analysis.Pass, sel *ast.SelectorExpr, fnInfo *FuncInfo) {
6868
expr, ok := sel.X.(*ast.Ident)
6969
if !ok {
7070
return
@@ -77,7 +77,7 @@ func (a *analyzer) reportSelector(pass *analysis.Pass, sel *ast.SelectorExpr, fn
7777
a.report(pass, sel.Pos(), expr.Name, sel.Sel.Name, fnInfo)
7878
}
7979

80-
func (a *analyzer) reportIdent(pass *analysis.Pass, expr *ast.Ident, fnInfo FuncInfo) {
80+
func (a *analyzer) reportIdent(pass *analysis.Pass, expr *ast.Ident, fnInfo *FuncInfo) {
8181
if !slices.Contains(a.fieldNames, expr.Name) {
8282
return
8383
}
@@ -92,7 +92,7 @@ func (a *analyzer) reportIdent(pass *analysis.Pass, expr *ast.Ident, fnInfo Func
9292
}
9393

9494
//nolint:gocyclo // The complexity is expected by the cases to check.
95-
func (a *analyzer) report(pass *analysis.Pass, pos token.Pos, origPkgName, origName string, fnInfo FuncInfo) {
95+
func (a *analyzer) report(pass *analysis.Pass, pos token.Pos, origPkgName, origName string, fnInfo *FuncInfo) {
9696
switch {
9797
case a.osMkdirTemp && origPkgName == osPkgName && origName == mkdirTempName:
9898
report(pass, pos, origPkgName, origName, tempDirName, fnInfo)
@@ -114,7 +114,7 @@ func (a *analyzer) report(pass *analysis.Pass, pos token.Pos, origPkgName, origN
114114
}
115115
}
116116

117-
func report(pass *analysis.Pass, pos token.Pos, origPkgName, origName, expectName string, fnInfo FuncInfo) {
117+
func report(pass *analysis.Pass, pos token.Pos, origPkgName, origName, expectName string, fnInfo *FuncInfo) {
118118
pass.Reportf(
119119
pos,
120120
"%s.%s() could be replaced by %s.%s() in %s",

usetesting.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,16 @@ func (a *analyzer) checkFunc(pass *analysis.Pass, ft *ast.FuncType, block *ast.B
148148
return
149149
}
150150

151-
argName, ok := isTestFunction(ft.Params.List[0], testingPkgName)
152-
if !ok {
151+
fnInfo := checkTestFunctionSignature(ft.Params.List[0], testingPkgName, fnName)
152+
if fnInfo == nil {
153153
return
154154
}
155155

156-
fnInfo := FuncInfo{
157-
Name: fnName,
158-
ArgName: argName,
159-
}
160-
161156
checkStmts(a, pass, fnInfo, block.List)
162157
}
163158

164159
//nolint:funlen // The complexity is expected by the number of [ast.Stmt] variants.
165-
func (a *analyzer) checkStmt(pass *analysis.Pass, fnInfo FuncInfo, stmt ast.Stmt) {
160+
func (a *analyzer) checkStmt(pass *analysis.Pass, fnInfo *FuncInfo, stmt ast.Stmt) {
166161
if stmt == nil {
167162
return
168163
}
@@ -234,7 +229,7 @@ func (a *analyzer) checkStmt(pass *analysis.Pass, fnInfo FuncInfo, stmt ast.Stmt
234229
}
235230
}
236231

237-
func (a *analyzer) checkExpr(pass *analysis.Pass, fnInfo FuncInfo, exp ast.Expr) {
232+
func (a *analyzer) checkExpr(pass *analysis.Pass, fnInfo *FuncInfo, exp ast.Expr) {
238233
switch expr := exp.(type) {
239234
case *ast.BinaryExpr:
240235
a.checkExpr(pass, fnInfo, expr.X)
@@ -268,34 +263,42 @@ func (a *analyzer) checkExpr(pass *analysis.Pass, fnInfo FuncInfo, exp ast.Expr)
268263
}
269264
}
270265

271-
func checkStmts[T ast.Stmt](a *analyzer, pass *analysis.Pass, fnInfo FuncInfo, stmts []T) {
266+
func checkStmts[T ast.Stmt](a *analyzer, pass *analysis.Pass, fnInfo *FuncInfo, stmts []T) {
272267
for _, stmt := range stmts {
273268
a.checkStmt(pass, fnInfo, stmt)
274269
}
275270
}
276271

277-
func checkExprs(a *analyzer, pass *analysis.Pass, fnInfo FuncInfo, exprs []ast.Expr) {
272+
func checkExprs(a *analyzer, pass *analysis.Pass, fnInfo *FuncInfo, exprs []ast.Expr) {
278273
for _, expr := range exprs {
279274
a.checkExpr(pass, fnInfo, expr)
280275
}
281276
}
282277

283-
func isTestFunction(arg *ast.Field, pkgName string) (string, bool) {
278+
func checkTestFunctionSignature(arg *ast.Field, pkgName, fnName string) *FuncInfo {
284279
switch at := arg.Type.(type) {
285280
case *ast.StarExpr:
286281
if se, ok := at.X.(*ast.SelectorExpr); ok {
287-
argName := getTestArgName(arg, "<t/b>")
288-
289-
return argName, checkSelectorName(se, pkgName, "T", "B")
282+
return createFuncInfo(arg, "<t/b>", se, pkgName, fnName, "T", "B")
290283
}
291284

292285
case *ast.SelectorExpr:
293-
argName := getTestArgName(arg, "tb")
286+
return createFuncInfo(arg, "tb", at, pkgName, fnName, "TB")
287+
}
294288

295-
return argName, checkSelectorName(at, pkgName, "TB")
289+
return nil
290+
}
291+
292+
func createFuncInfo(arg *ast.Field, defaultName string, se *ast.SelectorExpr, pkgName, fnName string, selectorNames ...string) *FuncInfo {
293+
ok := checkSelectorName(se, pkgName, selectorNames...)
294+
if !ok {
295+
return nil
296296
}
297297

298-
return "", false
298+
return &FuncInfo{
299+
Name: fnName,
300+
ArgName: getTestArgName(arg, defaultName),
301+
}
299302
}
300303

301304
func checkSelectorName(se *ast.SelectorExpr, pkgName string, selectorNames ...string) bool {

usetesting_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func TestAnalyzer(t *testing.T) {
4949

5050
for _, test := range testCases {
5151
t.Run(test.dir, func(t *testing.T) {
52+
t.Parallel()
53+
5254
newAnalyzer := NewAnalyzer()
5355

5456
for k, v := range test.options {

0 commit comments

Comments
 (0)