Skip to content

Commit d6c3fe3

Browse files
committed
refactor: improve check
1 parent d60a7b7 commit d6c3fe3

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

report.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,54 +102,68 @@ func diagnosticOSCreateTemp(ce *ast.CallExpr, fnInfo *FuncInfo) analysis.Diagnos
102102
return diagnostic
103103
}
104104

105-
func (a *analyzer) reportSelector(pass *analysis.Pass, se *ast.SelectorExpr, fnInfo *FuncInfo) {
105+
func (a *analyzer) reportSelector(pass *analysis.Pass, se *ast.SelectorExpr, fnInfo *FuncInfo) bool {
106106
if se.Sel == nil || !se.Sel.IsExported() {
107-
return
107+
return false
108108
}
109109

110110
ident, ok := se.X.(*ast.Ident)
111111
if !ok {
112-
return
112+
return false
113113
}
114114

115-
a.report(pass, se, ident.Name, se.Sel.Name, fnInfo)
115+
return a.report(pass, se, ident.Name, se.Sel.Name, fnInfo)
116116
}
117117

118-
func (a *analyzer) reportIdent(pass *analysis.Pass, ident *ast.Ident, fnInfo *FuncInfo) {
118+
func (a *analyzer) reportIdent(pass *analysis.Pass, ident *ast.Ident, fnInfo *FuncInfo) bool {
119119
if !ident.IsExported() {
120-
return
120+
return false
121121
}
122122

123123
if !slices.Contains(a.fieldNames, ident.Name) {
124-
return
124+
return false
125125
}
126126

127127
pkgName := getPkgNameFromType(pass, ident)
128128

129-
a.report(pass, ident, pkgName, ident.Name, fnInfo)
129+
return a.report(pass, ident, pkgName, ident.Name, fnInfo)
130130
}
131131

132132
//nolint:gocyclo // The complexity is expected by the number of cases to check.
133-
func (a *analyzer) report(pass *analysis.Pass, rg analysis.Range, origPkgName, origName string, fnInfo *FuncInfo) {
133+
func (a *analyzer) report(pass *analysis.Pass, rg analysis.Range, origPkgName, origName string, fnInfo *FuncInfo) bool {
134134
switch {
135135
case a.osMkdirTemp && origPkgName == osPkgName && origName == mkdirTempName:
136136
report(pass, rg, origPkgName, origName, tempDirName, fnInfo)
137137

138+
return true
139+
138140
case a.osTempDir && origPkgName == osPkgName && origName == tempDirName:
139141
report(pass, rg, origPkgName, origName, tempDirName, fnInfo)
140142

143+
return true
144+
141145
case a.osSetenv && origPkgName == osPkgName && origName == setenvName:
142146
report(pass, rg, origPkgName, origName, setenvName, fnInfo)
143147

148+
return true
149+
144150
case a.geGo124 && a.osChdir && origPkgName == osPkgName && origName == chdirName:
145151
report(pass, rg, origPkgName, origName, chdirName, fnInfo)
146152

153+
return true
154+
147155
case a.geGo124 && a.contextBackground && origPkgName == contextPkgName && origName == backgroundName:
148156
report(pass, rg, origPkgName, origName, contextName, fnInfo)
149157

158+
return true
159+
150160
case a.geGo124 && a.contextTodo && origPkgName == contextPkgName && origName == todoName:
151161
report(pass, rg, origPkgName, origName, contextName, fnInfo)
162+
163+
return true
152164
}
165+
166+
return false
153167
}
154168

155169
func report(pass *analysis.Pass, rg analysis.Range, origPkgName, origName, expectName string, fnInfo *FuncInfo) {

usetesting.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,13 @@ func (a *analyzer) check(pass *analysis.Pass, fn ast.Node, fnInfo *FuncInfo) {
145145
ast.Inspect(fn, func(n ast.Node) bool {
146146
switch v := n.(type) {
147147
case *ast.SelectorExpr:
148-
a.reportSelector(pass, v, fnInfo)
149-
return false
148+
return !a.reportSelector(pass, v, fnInfo)
150149

151150
case *ast.Ident:
152-
a.reportIdent(pass, v, fnInfo)
151+
return !a.reportIdent(pass, v, fnInfo)
153152

154153
case *ast.CallExpr:
155-
if a.reportCallExpr(pass, v, fnInfo) {
156-
return false
157-
}
154+
return !a.reportCallExpr(pass, v, fnInfo)
158155
}
159156

160157
return true

usetesting_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAnalyzer(t *testing.T) {
3434
{dir: "ossetenv/basic", options: map[string]string{"ossetenv": "true"}},
3535
{dir: "ossetenv/dot", options: map[string]string{"ossetenv": "true"}},
3636
{dir: "ossetenv/nottestfiles", options: map[string]string{"ossetenv": "true"}},
37-
{dir: "ossetenv/disable"},
37+
{dir: "ossetenv/disable", options: map[string]string{"ossetenv": "false"}},
3838

3939
{dir: "ostempdir/basic", options: map[string]string{"ostempdir": "true"}},
4040
{dir: "ostempdir/dot", options: map[string]string{"ostempdir": "true"}},

0 commit comments

Comments
 (0)