Skip to content

Commit 0435296

Browse files
committed
refactor: minor changes
1 parent d0b0385 commit 0435296

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

report.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo
2222

2323
switch fun := ce.Fun.(type) {
2424
case *ast.SelectorExpr:
25-
if fun.Sel.Name != createTempName {
25+
if fun.Sel == nil || fun.Sel.Name != createTempName {
2626
return false
2727
}
2828

@@ -31,15 +31,13 @@ func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo
3131
return false
3232
}
3333

34-
if expr.Name == osPkgName {
35-
if isFirstArgEmptyString(ce) {
36-
pass.Reportf(ce.Pos(),
37-
`%s.%s("", ...) could be replaced by %[1]s.%[2]s(%s.%s(), ...) in %s`,
38-
osPkgName, createTempName, fnInfo.ArgName, tempDirName, fnInfo.Name,
39-
)
34+
if expr.Name == osPkgName && isFirstArgEmptyString(ce) {
35+
pass.Reportf(ce.Pos(),
36+
`%s.%s("", ...) could be replaced by %[1]s.%[2]s(%s.%s(), ...) in %s`,
37+
osPkgName, createTempName, fnInfo.ArgName, tempDirName, fnInfo.Name,
38+
)
4039

41-
return true
42-
}
40+
return true
4341
}
4442

4543
case *ast.Ident:
@@ -49,49 +47,47 @@ func (a *analyzer) reportCallExpr(pass *analysis.Pass, ce *ast.CallExpr, fnInfo
4947

5048
pkgName := getPkgNameFromType(pass, fun)
5149

52-
if pkgName == osPkgName {
53-
if isFirstArgEmptyString(ce) {
54-
pass.Reportf(ce.Pos(),
55-
`%s.%s("", ...) could be replaced by %[1]s.%[2]s(%s.%s(), ...) in %s`,
56-
osPkgName, createTempName, fnInfo.ArgName, tempDirName, fnInfo.Name,
57-
)
50+
if pkgName == osPkgName && isFirstArgEmptyString(ce) {
51+
pass.Reportf(ce.Pos(),
52+
`%s.%s("", ...) could be replaced by %[1]s.%[2]s(%s.%s(), ...) in %s`,
53+
osPkgName, createTempName, fnInfo.ArgName, tempDirName, fnInfo.Name,
54+
)
5855

59-
return true
60-
}
56+
return true
6157
}
6258
}
6359

6460
return false
6561
}
6662

67-
func (a *analyzer) reportSelector(pass *analysis.Pass, sel *ast.SelectorExpr, fnInfo *FuncInfo) {
68-
expr, ok := sel.X.(*ast.Ident)
69-
if !ok {
63+
func (a *analyzer) reportSelector(pass *analysis.Pass, se *ast.SelectorExpr, fnInfo *FuncInfo) {
64+
if se.Sel == nil || !se.Sel.IsExported() {
7065
return
7166
}
7267

73-
if !sel.Sel.IsExported() {
68+
ident, ok := se.X.(*ast.Ident)
69+
if !ok {
7470
return
7571
}
7672

77-
a.report(pass, sel.Pos(), expr.Name, sel.Sel.Name, fnInfo)
73+
a.report(pass, se.Pos(), ident.Name, se.Sel.Name, fnInfo)
7874
}
7975

80-
func (a *analyzer) reportIdent(pass *analysis.Pass, expr *ast.Ident, fnInfo *FuncInfo) {
81-
if !slices.Contains(a.fieldNames, expr.Name) {
76+
func (a *analyzer) reportIdent(pass *analysis.Pass, ident *ast.Ident, fnInfo *FuncInfo) {
77+
if !ident.IsExported() {
8278
return
8379
}
8480

85-
if !expr.IsExported() {
81+
if !slices.Contains(a.fieldNames, ident.Name) {
8682
return
8783
}
8884

89-
pkgName := getPkgNameFromType(pass, expr)
85+
pkgName := getPkgNameFromType(pass, ident)
9086

91-
a.report(pass, expr.Pos(), pkgName, expr.Name, fnInfo)
87+
a.report(pass, ident.Pos(), pkgName, ident.Name, fnInfo)
9288
}
9389

94-
//nolint:gocyclo // The complexity is expected by the cases to check.
90+
//nolint:gocyclo // The complexity is expected by the number of cases to check.
9591
func (a *analyzer) report(pass *analysis.Pass, pos token.Pos, origPkgName, origName string, fnInfo *FuncInfo) {
9692
switch {
9793
case a.osMkdirTemp && origPkgName == osPkgName && origName == mkdirTempName:
@@ -131,8 +127,8 @@ func isFirstArgEmptyString(ce *ast.CallExpr) bool {
131127
return bl.Kind == token.STRING && bl.Value == `""`
132128
}
133129

134-
func getPkgNameFromType(pass *analysis.Pass, expr *ast.Ident) string {
135-
o := pass.TypesInfo.ObjectOf(expr)
130+
func getPkgNameFromType(pass *analysis.Pass, ident *ast.Ident) string {
131+
o := pass.TypesInfo.ObjectOf(ident)
136132

137133
if o == nil || o.Pkg() == nil {
138134
return ""

usetesting.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
9595

9696
a.geGo124 = a.isGoSupported(pass)
9797

98-
insp, _ := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
98+
insp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
99+
if !ok {
100+
return nil, nil
101+
}
99102

100103
nodeFilter := []ast.Node{
101104
(*ast.FuncDecl)(nil),

0 commit comments

Comments
 (0)