Repository navigation
Description
Summary
The codebase has been systematically eliminating the syntactic package match antipattern — identifying a stdlib call by ident.Name == "<pkg>" instead of by package identity via pass.TypesInfo. The canonical helper astutil.IsPkgSelector(pass, sel, pkgPath) exists (pkg/linters/internal/astutil/astutil.go:75-94) and is proven in production (sortslice, rawloginlib, regexpcompileinfunction), with #40243 currently consolidating the last three CI-enforced holdouts (os/fmt/strings).
The two most recently added linters re-introduce the same antipattern:
| Linter | Site | Syntactic check |
|---|---|---|
fileclosenotdeferred |
pkg/linters/fileclosenotdeferred/fileclosenotdeferred.go:172 |
ident.Name != "os" (gates os.Open/Create/OpenFile) |
contextcancelnotdeferred |
pkg/linters/contextcancelnotdeferred/contextcancelnotdeferred.go:138 |
pkgIdent.Name != "context" (gates context.WithCancel/WithTimeout/WithDeadline) |
Defect class (same as the landed precedents)
- False positive (shadowing): a local variable named
osorcontext— e.g.os := newObjectStore(); os.Open(path)— is matched as the stdlib package, producing a spurious diagnostic. - False negative (alias import):
import osx "os"→osx.Open(...), orimport ctx "context"→ctx.WithCancel(...), escapes detection entirely.
Note contextcancelnotdeferred's "context" syntactic match is the exact class flagged and fixed for the sibling ctxbackground linter in #38789 (closed) — the same file family detected context.Background() syntactically there.
Why a separate issue from #40243
#40243 is explicitly scoped to the three remaining CI-enforced linters (os/fmt/strings holdouts: osexitinlibrary, fprintlnsprintf, errstringmatch). These two linters are newly landed and not in that scope, so they would otherwise slip through. Closing them completes the codebase-wide consolidation #40243 began and prevents the antipattern from regressing on every new linter.
Evidence
pkg/linters/fileclosenotdeferred/fileclosenotdeferred.go:166-176—isFileOpenCallmatchessel.X.(*ast.Ident)thenident.Name != "os"pkg/linters/contextcancelnotdeferred/contextcancelnotdeferred.go:132-147—isContextWithCancelCallmatchessel.X.(*ast.Ident)thenpkgIdent.Name != "context"pkg/linters/internal/astutil/astutil.go:75-94—IsPkgSelectorresolvesObjectOf→*types.PkgName→Imported().Path()- Precedent:
pkg/linters/rawloginlib/rawloginlib.go:58(migrated), andsprintferrdot.go:109— the newest format linter already usesIsPkgSelector(pass, sel, "fmt")correctly, showing the helper is the established convention these two missed.
Recommendation
Replace the ident.Name == "<pkg>" checks with astutil.IsPkgSelector(pass, sel, "os") / astutil.IsPkgSelector(pass, sel, "context") respectively (one-line change each; the *ast.SelectorExpr is already in hand at both sites).
Validation checklist
- Shadowing testdata: local
os/contextvariable does not trigger a diagnostic - Alias-import testdata:
osx "os"/ctx "context"calls are detected - Existing positive/negative cases unchanged
- No production violations introduced (
grepshows no aliasedos/contextimports inpkg/non-test today, so this is precision-hardening with zero churn)
Effort: small (two single-line changes + shadowing/alias testdata).
References: §27860925409
Generated by 🤖 Sergo - Serena Go Expert · 276.8 AIC · ⌖ 11.6 AIC · ⊞ 5.8K · ◷
- expires on Jun 26, 2026, 9:14 PM UTC-08:00
Activity
github-actions commentedon Jun 20, 2026
🍪 Issue Monster selected this for Copilot
I have identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.
If assignment succeeds, the Copilot coding agent will analyze the issue and create a pull request with the fix.
Om nom nom! 🍪
Copilot commentedon Jun 20, 2026
The agent encountered an error and was unable to start working on this issue: Please try again later, or contact support if the problem persists. (Request id: 1541:19C6D5:5B578DB:153C6E50:6A36D5DC)
github-actions commentedon Jun 27, 2026
This issue was automatically closed because it expired on 2026-06-27T05:14:09.959Z.