88 "go/token"
99 "go/types"
1010 "strconv"
11+ "strings"
1112
1213 "golang.org/x/tools/go/analysis"
1314 "golang.org/x/tools/go/analysis/passes/inspect"
@@ -173,9 +174,24 @@ func extractStatusLiteral(expr *ast.BinaryExpr) (*ast.BasicLit, ast.Expr) {
173174func isHTTPStatusContext (pass * analysis.Pass , expr ast.Expr ) bool {
174175 switch e := expr .(type ) {
175176 case * ast.Ident :
176- return e .Name == "status" || e .Name == "statusCode"
177+ obj , ok := pass .TypesInfo .Uses [e ]
178+ if ! ok {
179+ return false
180+ }
181+ t := obj .Type ()
182+ if ! isIntegerType (t ) {
183+ return false
184+ }
185+ // For named integer types (custom enums/aliases), check whether the type
186+ // name itself indicates HTTP status to avoid false positives on non-HTTP
187+ // integer types (e.g. type JobState int).
188+ if named , isNamed := t .(* types.Named ); isNamed {
189+ return isHTTPStatusTypeName (named .Obj ().Name ())
190+ }
191+ // For plain integer types, fall back to variable name heuristic.
192+ return isHTTPStatusVarName (e .Name )
177193 case * ast.SelectorExpr :
178- if e .Sel .Name != "StatusCode" {
194+ if ! isHTTPStatusFieldName ( e .Sel .Name ) {
179195 return false
180196 }
181197 if sel , ok := pass .TypesInfo .Selections [e ]; ok {
@@ -194,6 +210,35 @@ func isHTTPStatusContext(pass *analysis.Pass, expr ast.Expr) bool {
194210 return false
195211}
196212
213+ // isHTTPStatusVarName returns true if a plain-integer variable/parameter name
214+ // suggests it holds an HTTP status code.
215+ func isHTTPStatusVarName (name string ) bool {
216+ switch name {
217+ case "status" , "statusCode" , "httpStatus" :
218+ return true
219+ }
220+ return false
221+ }
222+
223+ // isHTTPStatusFieldName returns true if a struct field name suggests HTTP status.
224+ // Accepts StatusCode, Status, and HTTPStatus to cover common response field spellings.
225+ func isHTTPStatusFieldName (name string ) bool {
226+ switch name {
227+ case "StatusCode" , "Status" , "HTTPStatus" :
228+ return true
229+ }
230+ return false
231+ }
232+
233+ // isHTTPStatusTypeName returns true if a named integer type's name indicates that
234+ // it represents an HTTP status code (e.g. HTTPStatusCode, HTTPStatus).
235+ // Both "http" and "status" must appear in the name (case-insensitive) to avoid
236+ // matching unrelated HTTP types such as HTTPVersion or HTTPMethod.
237+ func isHTTPStatusTypeName (name string ) bool {
238+ lower := strings .ToLower (name )
239+ return strings .Contains (lower , "http" ) && strings .Contains (lower , "status" )
240+ }
241+
197242func isIntegerType (t types.Type ) bool {
198243 basic , ok := t .Underlying ().(* types.Basic )
199244 return ok && basic .Info ()& types .IsInteger != 0
0 commit comments