timesleepnocontext: stop crossing non-go/defer FuncLit boundaries when attributing time.Sleep#42946
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
time.Sleep
There was a problem hiding this comment.
Pull request overview
This pull request updates the timesleepnocontext analyzer’s enclosing-scope walk so time.Sleep attribution does not cross ordinary callback FuncLit boundaries, avoiding incorrect “outer ctx” suggestions while preserving attribution for go/defer closures that share the outer lifetime.
Changes:
- Stop the enclosing walk at non-
go/defer*ast.FuncLitboundaries when that literal does not define its owncontext.Contextparameter. - Add
isGoOrDeferClosureto identifygo func(){...}()/defer func(){...}()closures during the walk. - Extend analyzer fixtures with “good callback” cases and an additional “bad defer” case.
Show a summary per file
| File | Description |
|---|---|
| pkg/linters/timesleepnocontext/timesleepnocontext.go | Updates enclosing-scope attribution semantics and introduces go/defer-closure detection helper. |
| pkg/linters/timesleepnocontext/testdata/src/timesleepnocontext/timesleepnocontext.go | Adds fixtures covering ordinary callbacks inside ctx-bearing functions and deferred closures that should still be flagged. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Low
|
@copilot please run the
|
…er fixtures Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
Addressed the open review threads in commit
Local |
🤖 PR Triage
Score breakdown: Impact 22 · Urgency 15 · Quality 16 CI running (1 check in progress). Targeted fix to
|
|
🎉 This pull request is included in a new release. Release: |
timesleepnocontextwas attributingtime.Sleepto the first outer ctx-bearing function across arbitraryFuncLitboundaries, which can misfire in callback closures and suggest the wrong cancellation source. This change preserves intended goroutine/defer behavior while preventing outer-ctx attribution for ordinary callbacks.Analyzer boundary semantics
pkg/linters/timesleepnocontext/timesleepnocontext.go.FuncLitwithout its owncontext.Contextparameter:goordefergo func(){...}()anddefer func(){...}()closures that share outer lifetime.AST parent-walk safety
Fixture coverage updates
testdata/src/timesleepnocontext/timesleepnocontext.gowith:http.HandleFunccallback nested in ctx function (time.Sleepshould not be flagged).register(func(){...})) nested in ctx function (no diagnostic).defer func(){ time.Sleep(...) }()still flagged).