Improve linter analyzer ergonomics: ctxbackground autofix, test-file parity, and range diagnostics#33541
Merged
Merged
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Review golang.org/x/tools for inconsistencies and improvements
Improve linter analyzer ergonomics: ctxbackground autofix, test-file parity, and range diagnostics
May 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves ergonomics and consistency across several go/analysis-based linters by standardizing _test.go handling, upgrading diagnostics to report source ranges (better editor highlighting), and enhancing ctxbackground with a targeted autofix.
Changes:
- Added a shared
filecheck.IsTestFile(...)helper and applied it across multiple analyzers to consistently skip_test.gofiles. - Migrated several analyzers from position-only diagnostics to range-based diagnostics via
pass.ReportRangef(...). - Reworked
ctxbackgroundto useinspector.WithStackfor traversal and added aSuggestedFixthat replacescontext.Background()with the in-scopecontext.Contextparameter; added testdata asserting_test.gois ignored.
Show a summary per file
| File | Description |
|---|---|
| pkg/linters/rawloginlib/rawloginlib.go | Uses shared _test.go detection and reports diagnostics over call expression ranges. |
| pkg/linters/osexitinlibrary/osexitinlibrary.go | Uses shared _test.go detection and reports diagnostics over call expression ranges. |
| pkg/linters/largefunc/largefunc.go | Switches large-function diagnostics to range-based reporting with refined highlight targets. |
| pkg/linters/internal/filecheck/filecheck.go | Introduces shared helper for identifying Go test files by filename. |
| pkg/linters/excessivefuncparams/excessivefuncparams.go | Updates diagnostics to use range-based reporting on the function name identifier. |
| pkg/linters/errstringmatch/errstringmatch.go | Uses shared _test.go detection and reports diagnostics over the matched call range. |
| pkg/linters/errormessage/errormessage.go | Uses shared _test.go detection and converts multiple reports to range-based diagnostics. |
| pkg/linters/ctxbackground/testdata/src/ctxbackground/ctxbackground_test.go | Adds fixture ensuring _test.go code is ignored by the analyzer. |
| pkg/linters/ctxbackground/ctxbackground.go | Refactors traversal to WithStack, skips _test.go, and adds a SuggestedFix to replace context.Background() with a context parameter. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 9/9 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This update addresses drift in our
golang.org/x/tools/go/analysisusage by tightening analyzer behavior and editor UX inpkg/linters. It focuses on the highest-leverage items from the module review:ctxbackgroundtraversal/fixability, consistent test-file handling, and range-based diagnostics.ctxbackground: single-pass traversal + autofixast.Inspectwalk withinspector.WithStackcall-level traversal to use the shared inspector pipeline end-to-end._test.goskip behavior to match analyzer suite conventions.SuggestedFixfor mechanical replacement ofcontext.Background()with the in-scopecontext.Contextparameter.Shared
_test.gofiltering helperpkg/linters/internal/filecheck.IsTestFile(...).ctxbackground,errormessage,errstringmatch,osexitinlibrary, andrawloginlibto the shared helper.Range-based diagnostics (
ReportRangef)largefuncexcessivefuncparamserrormessageerrstringmatchosexitinlibraryrawloginliblargefunc, range reporting highlights function declarations more precisely for named functions.Targeted analyzer testdata
ctxbackground_test.gofixture to assert test-file calls are ignored.