Skip to content

Commit 8541da4

Browse files
feat: add check HTTP status code in IfStmt (#37)
feat: add check HTTP status code in IfStmt
1 parent 577e8ab commit 8541da4

File tree

3 files changed

+1539
-0
lines changed

3 files changed

+1539
-0
lines changed

pkg/analyzer/analyzer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
5555
(*ast.CallExpr)(nil),
5656
(*ast.BasicLit)(nil),
5757
(*ast.CompositeLit)(nil),
58+
(*ast.IfStmt)(nil),
5859
}
5960

6061
insp.Preorder(filter, func(node ast.Node) {
@@ -157,6 +158,28 @@ func run(pass *analysis.Pass) (interface{}, error) {
157158
}
158159
}
159160
}
161+
162+
case *ast.IfStmt:
163+
binaryExpr, ok := n.Cond.(*ast.BinaryExpr)
164+
if !ok {
165+
return
166+
}
167+
168+
selectorExpr, ok := binaryExpr.X.(*ast.SelectorExpr)
169+
if !ok {
170+
return
171+
}
172+
173+
if selectorExpr.Sel.Name != "StatusCode" {
174+
return
175+
}
176+
177+
basicLit, ok := binaryExpr.Y.(*ast.BasicLit)
178+
if !ok {
179+
return
180+
}
181+
182+
checkHTTPStatusCode(pass, basicLit)
160183
}
161184
})
162185

pkg/analyzer/internal/template/test-httpstatuscode.go.tmpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,31 @@ func _() {
2929
}
3030
}
3131
{{ end -}}
32+
33+
{{ range $key, $value := .Mapping }}
34+
func _() error {
35+
resp, err := http.DefaultClient.Do(&http.Request{})
36+
if err != nil {
37+
return err
38+
}
39+
defer func() { _ = resp.Body.Close() }()
40+
if resp.StatusCode == {{ $key }} { // want `"{{ quoteMeta $key }}" can be replaced by {{ quoteMeta $value }}`
41+
return nil
42+
}
43+
return nil
44+
}
45+
{{ end -}}
46+
47+
{{ range $key, $value := .Mapping }}
48+
func _() error {
49+
resp, err := http.DefaultClient.Do(&http.Request{})
50+
if err != nil {
51+
return err
52+
}
53+
defer func() { _ = resp.Body.Close() }()
54+
if resp.StatusCode == {{ $value }} {
55+
return nil
56+
}
57+
return nil
58+
}
59+
{{ end -}}

0 commit comments

Comments
 (0)