Skip to content

fix(httpstatuscode): replace spelling-based gating with type-aware detection#42009

Merged
pelikhan merged 4 commits into
mainfrom
copilot/fix-httpstatuscode-detection
Jun 28, 2026
Merged

fix(httpstatuscode): replace spelling-based gating with type-aware detection#42009
pelikhan merged 4 commits into
mainfrom
copilot/fix-httpstatuscode-detection

Conversation

Copilot AI commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

isHTTPStatusContext matched operands by hardcoded name spellings (status, statusCode, .StatusCode) with no type verification on the identifier path — causing false negatives for any other-named HTTP status field/variable and false positives for non-HTTP integers that happen to share a name.

Changes

httpstatuscode.go

  • Identifier path: Resolves the operand's type via pass.TypesInfo.Uses. For named integer types (type JobState int), requires both "http" and "status" in the type name (case-insensitive) before flagging — prevents false positives on HTTPVersion, HTTPMethod, and unrelated enums. For plain int, falls back to a broadened name list: status, statusCode, httpStatus.
  • Selector path: Accepts Status and HTTPStatus field names in addition to StatusCode (with the existing integer-type guard), fixing the silent entry.Status == 200 class of false negatives.
  • Extracted logic into three focused helpers: isHTTPStatusVarName, isHTTPStatusFieldName, isHTTPStatusTypeName.

Testdata

Case Before After
entry.Status == 200 (field Status int) silent flagged ✅
c.HTTPStatus == 404 (field HTTPStatus int) silent flagged ✅
httpStatus == 200 (param httpStatus int) silent flagged ✅
state == 200 where type JobState int flagged (false positive) silent ✅
code := resp.StatusCode; code == 404 silent silent (documented false negative — flow analysis required)

Copilot AI and others added 3 commits June 28, 2026 06:01
…tection

- Identifier path: resolve type via TypesInfo; for named integer types
  require both 'http' and 'status' in the type name to avoid false
  positives (e.g. type JobState int); for plain integers fall back to
  broadened name list (status, statusCode, httpStatus).
- Selector path: accept Status and HTTPStatus field names in addition to
  StatusCode, fixing false negatives for entry.Status and .HTTPStatus fields.
- Adds isHTTPStatusVarName, isHTTPStatusFieldName, isHTTPStatusTypeName helpers.
- Extends testdata with: Status/HTTPStatus field cases (flagged), JobState
  named-enum case (not flagged), and a documented false-negative for
  non-status-named locals.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
…tive cases

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix httpstatuscode linter for accurate context detection fix(httpstatuscode): replace spelling-based gating with type-aware detection Jun 28, 2026
Copilot AI requested a review from pelikhan June 28, 2026 06:06
@github-actions

Copy link
Copy Markdown
Contributor

Great work on this! 🎉 The type-aware detection upgrade to the httpstatuscode linter is a genuinely meaningful improvement — replacing the fragile hardcoded-name heuristic with pass.TypesInfo.Uses-based type resolution is the right architectural move.

The PR is tightly scoped to one linter, the description is thorough (the before/after table is especially helpful), and the testdata additions systematically cover every new true-positive and corrected false-positive case. This looks ready for review.

Generated by ✅ Contribution Check · 113.9 AIC · ⌖ 19.9 AIC · ⊞ 6K ·

@github-actions

Copy link
Copy Markdown
Contributor

PR Triage — Run §28315307719

Category bug
Risk low
Priority low
Score 30/100 — impact 15 · urgency 5 · quality 10
Action defer

⚠️ Draft PR. Upgrades httpstatuscode linter from spelling-based to type-aware detection, eliminating false positives/negatives (+131/-2, 2 files). Ready to re-evaluate once marked ready for review.

Generated by 🔧 PR Triage Agent · 82.5 AIC · ⌖ 10.6 AIC · ⊞ 5.4K ·

@pelikhan pelikhan marked this pull request as ready for review June 28, 2026 08:56
Copilot AI review requested due to automatic review settings June 28, 2026 08:56
@pelikhan pelikhan merged commit a0a0bd5 into main Jun 28, 2026
1 check passed
@pelikhan pelikhan deleted the copilot/fix-httpstatuscode-detection branch June 28, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens the httpstatuscode analyzer’s detection of “HTTP status code context” by moving from spelling-only matching to type-aware checks, reducing false positives/negatives when identifying comparisons/switches against magic HTTP status literals.

Changes:

  • Updated identifier handling to consult pass.TypesInfo.Uses and gate on integer types, using a stricter heuristic for named integer types via isHTTPStatusTypeName.
  • Broadened selector/field-name matching to include Status and HTTPStatus, and extracted the heuristics into focused helper functions.
  • Expanded analysistest coverage to exercise the new cases (field names Status/HTTPStatus, param httpStatus, and a non-HTTP named int enum).
Show a summary per file
File Description
pkg/linters/httpstatuscode/httpstatuscode.go Reworks status-context detection to be type-aware and expands field-name heuristics via helper functions.
pkg/linters/httpstatuscode/testdata/src/httpstatuscode/httpstatuscode.go Adds test cases for the new identifier/field heuristics and documents an intentional false negative.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (1)

pkg/linters/httpstatuscode/httpstatuscode.go:198

  • In the SelectorExpr path, the type-aware guard only checks “is integer”, but it does not apply the named-type heuristic used for *ast.Ident. This means a struct field like Status JobState (where type JobState int) will still be treated as an HTTP status context and get flagged when compared to 200/404, reintroducing the same false-positive class this PR is trying to eliminate for named int enums.

Consider mirroring the ident behavior: after confirming the selected field is an integer, if it’s a *types.Named then require isHTTPStatusTypeName(named.Obj().Name()); otherwise allow plain ints.

		if !isHTTPStatusFieldName(e.Sel.Name) {
			return false
		}
		if sel, ok := pass.TypesInfo.Selections[e]; ok {
			return isIntegerType(sel.Type())
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Low

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants