Skip to content

Improve linter analyzer ergonomics: ctxbackground autofix, test-file parity, and range diagnostics#33541

Merged
pelikhan merged 4 commits into
mainfrom
copilot/go-fan-review-golang-org-x-tools
May 20, 2026
Merged

Improve linter analyzer ergonomics: ctxbackground autofix, test-file parity, and range diagnostics#33541
pelikhan merged 4 commits into
mainfrom
copilot/go-fan-review-golang-org-x-tools

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 20, 2026

This update addresses drift in our golang.org/x/tools/go/analysis usage by tightening analyzer behavior and editor UX in pkg/linters. It focuses on the highest-leverage items from the module review: ctxbackground traversal/fixability, consistent test-file handling, and range-based diagnostics.

  • ctxbackground: single-pass traversal + autofix

    • Replaced function-body ast.Inspect walk with inspector.WithStack call-level traversal to use the shared inspector pipeline end-to-end.
    • Added _test.go skip behavior to match analyzer suite conventions.
    • Added SuggestedFix for mechanical replacement of context.Background() with the in-scope context.Context parameter.
  • Shared _test.go filtering helper

    • Added pkg/linters/internal/filecheck.IsTestFile(...).
    • Switched duplicate filename suffix checks in ctxbackground, errormessage, errstringmatch, osexitinlibrary, and rawloginlib to the shared helper.
  • Range-based diagnostics (ReportRangef)

    • Migrated position-only reports to range-based reporting in:
      • largefunc
      • excessivefuncparams
      • errormessage
      • errstringmatch
      • osexitinlibrary
      • rawloginlib
    • In largefunc, range reporting highlights function declarations more precisely for named functions.
  • Targeted analyzer testdata

    • Added ctxbackground _test.go fixture to assert test-file calls are ignored.
pass.Report(analysis.Diagnostic{
    Pos:     call.Pos(),
    End:     call.End(),
    Message: "use the context.Context parameter instead of context.Background()",
    SuggestedFixes: []analysis.SuggestedFix{
        {
            Message: "Replace context.Background() with context parameter",
            TextEdits: []analysis.TextEdit{{
                Pos: call.Pos(), End: call.End(), NewText: []byte(ctxParamName),
            }},
        },
    },
})

Copilot AI linked an issue May 20, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits May 20, 2026 13:12
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
Copilot AI requested a review from pelikhan May 20, 2026 13:19
@pelikhan pelikhan marked this pull request as ready for review May 20, 2026 13:30
Copilot AI review requested due to automatic review settings May 20, 2026 13:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 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.go files.
  • Migrated several analyzers from position-only diagnostics to range-based diagnostics via pass.ReportRangef(...).
  • Reworked ctxbackground to use inspector.WithStack for traversal and added a SuggestedFix that replaces context.Background() with the in-scope context.Context parameter; added testdata asserting _test.go is 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

@pelikhan pelikhan merged commit 8c2c89f into main May 20, 2026
31 checks passed
@pelikhan pelikhan deleted the copilot/go-fan-review-golang-org-x-tools branch May 20, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go-fan] Go Module Review: golang.org/x/tools

3 participants